parking-webapi/WebApi_data_value/Controllers/Violation_car_tableController.cs

275 lines
11 KiB
C#

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 WebApi_data_value.Models;
using WebApi_data_value.Services;
using WebApi_data_value.ViewModel;
namespace WebApi_data_value.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Violation_car_tableController : ControllerBase
{
private readonly SqlContext _context;
public Violation_car_tableController(SqlContext context)
{
_context = context;
}
#region
/// <summary>
/// 抓取所有違規車輛
/// </summary>
/// <returns></returns>
// GET: api/Violation_car_table
[HttpGet]
public async Task<ActionResult<IEnumerable<Violation_car_table>>> Getviolation_car_table()
{
var violation_car_table = await(from c in _context.violation_car_table
orderby c.create_data_time descending
select new Violation_car_table
{
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
violation_location_name=c.violation_location_name,
}).ToListAsync();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 透過區域名稱抓取違規車輛
/// </summary>
/// <param name="id">違規區域</param>
/// <returns></returns>
// GET: api/Violation_car_table/5
[HttpGet("violation_location_name-{id}")]
public async Task<IEnumerable<Single_violation_car_table>> GetViolation_car_table(string id)
{
var violation_car_table = await(from c in _context.violation_car_table
where c.violation_location_name == id
orderby c.create_data_time descending
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}
).ToListAsync();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 抓出指定違規車輛
/// </summary>
/// <param name="id">違規區域</param>
/// <param name="time">資料新增時間</param>
/// <returns></returns>
[HttpGet("location_nam-{id}-time-{time}")]
public async Task<ActionResult<Violation_car_table>> violation_location_name(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select new Violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
car_start_img = c.car_start_img,
car_end_img = c.car_end_img,
}).FirstAsync();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 更新指定違規車輛
/// </summary>
/// <param name="id">區域</param>
/// <param name="time">資料新增時間</param>
/// <returns></returns>
[HttpPut("location_nam-{id}-time-{time}")]
public async Task<IActionResult> PutViolation_car_table(string id, DateTime time,Violation_car_table violation_car_table)
{
var violation_car_table_1 = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table_1);
await _context.SaveChangesAsync();
_context.violation_car_table.Add(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region
/// <summary>
/// 獲取時間內的車輛
/// </summary>
/// <param name="id">位置名稱</param>
/// <param name="start_time">起始時間</param>
/// <param name="end_time">結束時間</param>
/// <returns></returns>
[HttpGet("location_name_1_-{id}-start_time-{start_time}-end_time-{end_time}")]
public async Task<IEnumerable<Single_violation_car_table>> GetYuntech_in_car_table_date(string id, DateTime start_time, DateTime end_time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time >= start_time
where c.create_data_time <= end_time
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}).ToListAsync();
return violation_car_table;
}
#endregion
#region
/// <summary>
/// 透過車牌號碼搜尋所有資料
/// </summary>
/// <param name="id">車牌號碼 ex:ABC4321</param>
/// <returns></returns>
[HttpGet("license_plate_number-{id}")]
public async Task<IEnumerable<Single_violation_car_table>> Getlicense_plate_number(string id)
{
var in_car_table = await (from c in _context.violation_car_table
where c.license_plate_number == id
orderby c.create_data_time descending
select new Single_violation_car_table
{
violation_location_name = c.violation_location_name,
license_plate_number = c.license_plate_number,
create_data_time = c.create_data_time,
}).ToListAsync();
return in_car_table;
}
#endregion
#region
/// <summary>
/// 尋找違規次數最多
/// </summary>
/// <returns></returns>
[HttpGet("recidivists")]
public async Task<IEnumerable<object>> GetRecidivists()
{
var plateNumberOccurrences = await _context.violation_car_table
.GroupBy(c => c.license_plate_number)
.Select(group => new
{
LicensePlateNumber = group.Key,
Occurrences = group.Count()
})
.OrderByDescending(x => x.Occurrences)
.ToListAsync();
return plateNumberOccurrences;
}
#endregion
#region
/// <summary>
/// 新增違規車輛
/// </summary>
/// <param name="violation_car_table"></param>
/// <returns></returns>
// POST: api/Violation_car_table
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Violation_car_table>> PostViolation_car_table(Violation_car_table violation_car_table)
{
if (_context.violation_car_table == null)
{
return Problem("Entity set 'SqlContext.violation_car_table' is null.");
}
violation_car_table.create_data_time= DateTime.Now;
_context.violation_car_table.Add(violation_car_table);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Violation_car_tableExists(violation_car_table.violation_location_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetViolation_car_table", new { id = violation_car_table.violation_location_name }, violation_car_table);
}
#endregion
#region
/// <summary>
/// 刪除違規車輛
/// </summary>
/// <param name="id"></param>
/// <param name="time"></param>
/// <returns></returns>
// DELETE: api/Violation_car_table/5
[HttpDelete("location_name-{id}-time-{time}")]
public async Task<IActionResult> DeleteViolation_car_table(string id, DateTime time)
{
var violation_car_table = await (from c in _context.violation_car_table
where c.violation_location_name == id
where c.create_data_time == time
select c).FirstAsync();
_context.violation_car_table.Remove(violation_car_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
private bool Violation_car_tableExists(string id)
{
return (_context.violation_car_table?.Any(e => e.violation_location_name == id)).GetValueOrDefault();
}
}
}