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

229 lines
9.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Globalization;
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.Authorization;
using WebApi_data_value.ViewModel;
namespace WebApi_data_value.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class Parking_spaces_instantController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_instantController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Parking_spaces_instant
/// <summary>
/// 獲取所有停車即時資料
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Parking_spaces_instant>>> Getparking_spaces_instant()
{
if (_context.parking_spaces_instant == null)
{
return NotFound();
}
return await _context.parking_spaces_instant.ToListAsync();
}
#endregion
#region
/// <summary>
/// 獲取單一停車區的即時資料
/// </summary>
[HttpGet("parking_space_area-{id}")]
public async Task<IEnumerable<Parking_spaces_instant>> parking_space_area(string id)
{
var parking_spaces_name = await (from c in _context.parking_spaces_instant
where c.parking_spaces_name == id
orderby c.data_create_time descending
select new Parking_spaces_instant
{
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,
}).ToArrayAsync();
return parking_spaces_name;
}
#endregion
#region
/// <summary>
/// 獲取車輛資訊
/// </summary>
[HttpGet("parking_space_area_name-{id}-time-{time}")]
public async Task<ActionResult<Parking_spaces_instant>> parking_space_area_car_data(string id, DateTime time)
{
var parking_spaces_name = await (from c in _context.parking_spaces_instant
where c.parking_spaces_name == id
where c.data_create_time == time
select new Parking_spaces_instant
{
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();
return parking_spaces_name;
}
#endregion
#region
// POST: api/Parking_spaces_instant
/// <summary>
/// 新增一筆停車資料
/// </summary>
[HttpPost]
public async Task<ActionResult<Parking_spaces_instant>> PostParking_spaces_instant(Parking_spaces_instant parking_spaces_instant)
{
if (_context.parking_spaces_instant == null)
{
return Problem("Entity set 'SqlContext.parking_spaces_instant' is null.");
}
parking_spaces_instant.data_create_time = DateTime.Now;
_context.parking_spaces_instant.Add(parking_spaces_instant);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Parking_spaces_instantExists(parking_spaces_instant.parking_spaces_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetParking_spaces_instant", new { id = parking_spaces_instant.parking_spaces_name }, parking_spaces_instant);
}
#endregion
#region
// DELETE: api/Parking_spaces_instant/5
/// <summary>
/// 透過車牌號碼刪除停車資料
/// </summary>
[HttpDelete("parking_space_name-{id}-time-{time}")]
public async Task<IActionResult> DeleteParking_spaces_instant(string id, DateTime time)
{
if (_context.parking_spaces_instant == null)
{
return NotFound();
}
//var parking_spaces_instant = await _context.parking_spaces_instant.FindAsync(id);
var parking_spaces_instant = await (from c in _context.parking_spaces_instant
where c.parking_spaces_name == id
where c.data_create_time == time
select new Parking_spaces_instant
{
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_instant == null)
{
return NotFound();
}
_context.parking_spaces_instant.Remove(parking_spaces_instant);
await _context.SaveChangesAsync();
return NoContent();
}
#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_instant
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_instant
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
private bool Parking_spaces_instantExists(string id)
{
return (_context.parking_spaces_instant?.Any(e => e.parking_spaces_name == id)).GetValueOrDefault();
}
}
}