using System;
using System.Collections.Generic;
using System.Linq;
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;
using Parking_space_WebAPI.ViewModel;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園外網")]
public class Parking_spaces_roiController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_roiController(SqlContext context)
{
_context = context;
}
#region 抓出所有區域ROI範圍資料
///
/// 抓出所有區域ROI範圍資料
/// (x1,y1為左上的點 x2,y2為右下的點)
/// roi_function 為 ROI功能 有(CAR_ROI_license_plate(車牌辨識範圍)、CAR_ROI_violation(違規區域)、CAR_ROI_normal(正常區域))
/// mode 為模式選擇 (pass(判斷車是進入或出去)、violation(判斷違規))
///
// GET: api/Parking_spaces_roi
[HttpGet]
public async Task>> Getparking_spaces_roi()
{
if (_context.parking_spaces_roi == null)
{
return NotFound();
}
return await _context.parking_spaces_roi.ToListAsync();
}
#endregion
#region 根據停車場名稱抓出ROI
// GET: api/Parking_spaces_roi/5
///
/// 根據停車場名稱抓出ROI
///
[HttpGet("{id}")]
public async Task> GetParking_spaces_roi(string id)
{
var parking_spaces_roi = await (from c in _context.parking_spaces_roi
where c.parking_spaces_name == id
select new Parking_spaces_roi
{
parking_spaces_name = c.parking_spaces_name,
cam_ip = c.cam_ip,
roi_x1 = c.roi_x1,
roi_y1 = c.roi_y1,
roi_x2 = c.roi_x2,
roi_y2 = c.roi_y2,
mode = c.mode,
roi_function = c.roi_function,
data_create_time = c.data_create_time,
}
).ToListAsync();
return parking_spaces_roi;
}
#endregion
#region 編輯ROI範圍
///
/// 編輯ROI範圍
///
// PUT: api/Parking_spaces_roi/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task PutParking_spaces_roi(string id, Parking_spaces_roi parking_spaces_roi)
{
var parking_spaces_roi_1 = await (from c in _context.parking_spaces_roi
where c.parking_spaces_name == parking_spaces_roi.parking_spaces_name
where c.roi_function == parking_spaces_roi.roi_function
where c.cam_ip == parking_spaces_roi.cam_ip
select new Parking_spaces_roi
{
parking_spaces_name = c.parking_spaces_name,
roi_function = c.roi_function,
roi_x1 = c.roi_x1,
roi_x2= c.roi_x2,
roi_y1=c.roi_y1,
roi_y2= c.roi_y2,
cam_ip = c.cam_ip,
mode = c.mode,
data_create_time= c.data_create_time,
}
).FirstAsync();
_context.parking_spaces_roi.Remove(parking_spaces_roi_1);
await _context.SaveChangesAsync();
parking_spaces_roi.data_create_time = DateTime.Now;
_context.parking_spaces_roi.Add(parking_spaces_roi);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region 新增ROI範圍
///
/// (x1,y1為左上的點 x2,y2為右下的點)
/// roi_function 為 ROI功能 有(CAR_ROI_license_plate(車牌辨識範圍)、CAR_ROI_violation(違規區域)、CAR_ROI_normal(正常區域))
/// mode 為模式選擇 (pass(判斷車是進入或出去)、violation(判斷違規))
///
// POST: api/Parking_spaces_roi
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task> PostParking_spaces_roi(Parking_spaces_roi parking_spaces_roi)
{
if (_context.parking_spaces_roi == null)
{
return Problem("Entity set 'SqlContext.parking_spaces_roi' is null.");
}
parking_spaces_roi.data_create_time = DateTime.Now;
_context.parking_spaces_roi.Add(parking_spaces_roi);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Parking_spaces_roiExists(parking_spaces_roi.parking_spaces_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetParking_spaces_roi", new { id = parking_spaces_roi.parking_spaces_name }, parking_spaces_roi);
}
#endregion
#region 刪除ROI範圍
///
/// 刪除ROI範圍
///
// DELETE: api/Parking_spaces_roi/5
[HttpDelete("{id}")]
public async Task DeleteParking_spaces_roi(string id)
{
if (_context.parking_spaces_roi == null)
{
return NotFound();
}
var parking_spaces_roi = await _context.parking_spaces_roi.FindAsync(id);
if (parking_spaces_roi == null)
{
return NotFound();
}
_context.parking_spaces_roi.Remove(parking_spaces_roi);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
private bool Parking_spaces_roiExists(string id)
{
return (_context.parking_spaces_roi?.Any(e => e.parking_spaces_name == id)).GetValueOrDefault();
}
}
}