using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Parking_space_WebAPI.Models;
using Parking_space_WebAPI.Services;
using Parking_space_WebAPI.Authorization;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園外網")]
public class Parking_spaces_cam_ptzController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_cam_ptzController(SqlContext context)
{
_context = context;
}
#region 根據停車場名稱獲取cam_ptz的資料
// GET: api/Parking_spaces_cam
///
/// 根據停車場名稱獲取cam_ptz的資料
///
[HttpGet("parking_spaces_name-{id}")]
public async Task> Getparking_spaces_cam(string id)
{
var parking_spaces_cam_ptz = await (from c in _context.parking_spaces_cam_ptz
where c.parking_spaces_name == id
select new Parking_spaces_cam_ptz
{
parking_spaces_name = c.parking_spaces_name,
mode = c.mode,
rtsp_url = c.rtsp_url,
http_port = c.http_port,
rtsp_port = c.rtsp_port,
img = c.img,
pan = c.pan,
titl = c.titl,
zoom = c.zoom,
}).ToListAsync();
return parking_spaces_cam_ptz;
}
#endregion
// PUT: api/Parking_spaces_cam_ptz/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("parking_spaces_name-{id}-IP-{ip}-port-{port}")]
public async Task PutParking_spaces_cam_ptz(string id, string ip, string port, Parking_spaces_cam_ptz parking_spaces_cam_ptz)
{
var parking_spaces_cam = await (from c in _context.parking_spaces_cam
where c.parking_spaces_name == id
where c.ip == ip
where c.port == port
select new Parking_spaces_cam
{
parking_spaces_name = c.parking_spaces_name,
ip = ip,
port = port,
url = c.url
}
).FirstAsync();
var url = parking_spaces_cam.url;
if (url != parking_spaces_cam_ptz.rtsp_url)
{
return BadRequest();
}
_context.Entry(parking_spaces_cam_ptz).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Parking_spaces_cam_ptzExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
#region 新增cam_ptz資料
// POST: api/Parking_spaces_cam
///
/// 新增cam_ptz資料
///
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task> PostParking_spaces_cam(Parking_spaces_cam_ptz parking_spaces_cam_ptz)
{
if (_context.parking_spaces_cam_ptz == null)
{
return Problem("Entity set 'SqlContext.parking_spaces_cam_ptz' is null.");
}
_context.parking_spaces_cam_ptz.Add(parking_spaces_cam_ptz);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
// DELETE: api/Parking_spaces_cam_ptz/5
[HttpDelete("{id}")]
public async Task DeleteParking_spaces_cam_ptz(string id)
{
if (_context.parking_spaces_cam_ptz == null)
{
return NotFound();
}
var parking_spaces_cam_ptz = await _context.parking_spaces_cam_ptz.FindAsync(id);
if (parking_spaces_cam_ptz == null)
{
return NotFound();
}
_context.parking_spaces_cam_ptz.Remove(parking_spaces_cam_ptz);
await _context.SaveChangesAsync();
return NoContent();
}
private bool Parking_spaces_cam_ptzExists(string id)
{
return (_context.parking_spaces_cam_ptz?.Any(e => e.rtsp_url == id)).GetValueOrDefault();
}
}
}