parking-webapi/WebApi_data_value/Controllers/Parking_spaces_historyController.cs
2024-02-09 16:44:45 +08:00

205 lines
8.3 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;
using WebApi_data_value.Authorization;
namespace WebApi_data_value.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class Parking_spaces_historyController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_historyController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Parking_spaces_history
/// <summary>
/// 獲取所有停車歷時資料
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Parking_spaces_history>>> Getparking_spaces_history()
{
if (_context.parking_spaces_history == null)
{
return NotFound();
}
return await _context.parking_spaces_history.ToListAsync();
}
#endregion
#region
/// <summary>
/// 獲取單一停車區的即時資料
/// </summary>
[HttpGet("parking_space_area-{id}")]
public async Task<IEnumerable<Single_Parking_spaces>> parking_space_area(string id)
{
var parking_spaces_name = await (from c in _context.parking_spaces_history
where c.parking_spaces_name == id
orderby c.license_plate_number descending
select new Single_Parking_spaces
{
parking_spaces_name = c.parking_spaces_name,
license_plate_number = c.license_plate_number,
car_img = c.car_img,
in_time = c.in_time,
out_time = c.out_time,
data_create_time = c.data_create_time,
}).ToListAsync();
return parking_spaces_name;
}
#endregion
#region
// POST: api/Parking_spaces_instant
/// <summary>
/// 新增一筆停車資料
/// </summary>
[HttpPost]
public async Task<ActionResult<Parking_spaces_history>> PostParking_spaces_history(Parking_spaces_history parking_spaces_history)
{
//if (_context.parking_spaces_history == null)
//{
// return Problem("Entity set 'SqlContext.parking_spaces_history' is null.");
//}
parking_spaces_history.data_create_time = DateTime.Now;
_context.parking_spaces_history.Add(parking_spaces_history);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Parking_spaces_historyExists(parking_spaces_history.parking_spaces_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetParking_spaces_history", new { id = parking_spaces_history.parking_spaces_name }, parking_spaces_history);
}
#endregion
#region
/// <summary>
/// 透過車牌號碼找出資料
/// </summary>
[HttpGet("license_plate_number-{id}")]
public async Task<IEnumerable<Single_Parking_spaces>> license_plate_number(string id)
{
var license_plate_number = await (from c in _context.parking_spaces_history
where c.license_plate_number == id
select new Single_Parking_spaces
{
parking_spaces_name = c.parking_spaces_name,
license_plate_number = c.license_plate_number,
car_img = c.car_img,
in_time = c.in_time,
out_time = c.out_time,
data_create_time = c.data_create_time,
}
).ToListAsync();
return license_plate_number;
}
#endregion
#region
/// <summary>
/// 透過時間找出資料
/// </summary>
[HttpGet("date-{id}")]
public async Task<IEnumerable<Single_Parking_spaces>> data_create_time(DateTime id)
{
//DateTime.TryParseExact( id, "yyyy/MM/dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out DateTime dateTime);
var date_data = await (from c in _context.parking_spaces_history
where c.data_create_time.Value.Date == id
select new Single_Parking_spaces
{
parking_spaces_name = c.parking_spaces_name,
license_plate_number = c.license_plate_number,
car_img = c.car_img,
in_time = c.in_time,
out_time = c.out_time,
data_create_time = c.data_create_time,
}).ToListAsync();
return date_data;
}
#endregion
#region
// PUT: api/Parking_spaces_total_table/5
/// <summary>
/// 更改歷史停車資料
/// </summary>
[HttpPut("license_plate_number-{id}-In_time-{date}")]
public async Task<IActionResult> Single_Parking_spaces(string id, string date, Parking_spaces_history parking_spaces_history)
{
if ((id != parking_spaces_history.license_plate_number) && (date != parking_spaces_history.in_time))
{
return BadRequest();
}
var parking_spaces_history_1 = await (from c in _context.parking_spaces_history
where c.license_plate_number == id
where c.in_time == date
select new Parking_spaces_history
{
parking_spaces_name = c.parking_spaces_name,
license_plate_number = c.license_plate_number,
car_img = c.car_img,
in_time = c.in_time,
out_time = c.out_time,
data_create_time = c.data_create_time,
}
).FirstAsync();
if (parking_spaces_history_1 == null)
{
return NotFound();
}
_context.parking_spaces_history.Remove(parking_spaces_history_1);
await _context.SaveChangesAsync();
parking_spaces_history.data_create_time = DateTime.Now;
_context.parking_spaces_history.Add(parking_spaces_history);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
private bool Parking_spaces_historyExists(string id)
{
return (_context.parking_spaces_history?.Any(e => e.parking_spaces_name == id)).GetValueOrDefault();
}
}
}