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 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_instantController : ControllerBase { private readonly SqlContext _context; public Parking_spaces_instantController(SqlContext context) { _context = context; } #region 獲取所有停車即時資料 // GET: api/Parking_spaces_instant /// /// 獲取所有停車即時資料 /// [HttpGet] public async Task>> Getparking_spaces_instant() { if (_context.parking_spaces_instant == null) { return NotFound(); } return await _context.parking_spaces_instant.ToListAsync(); } #endregion #region 獲取單一停車區的即時資料 /// /// 獲取單一停車區的即時資料 /// [HttpGet("parking_space_area-{id}")] public async Task> 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 獲取車輛資訊 /// /// 獲取車輛資訊 /// [HttpGet("parking_space_area_name-{id}-time-{time}")] public async Task> 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 /// /// 新增一筆停車資料 /// [HttpPost] public async Task> 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 /// /// 透過車牌號碼刪除停車資料 /// [HttpDelete("parking_space_name-{id}-time-{time}")] public async Task 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 透過車牌號碼找出資料 /// /// 透過車牌號碼找出資料 /// [HttpGet("license_plate_number-{id}")] public async Task> 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 透過時間找出資料 /// /// 透過時間找出資料 /// [HttpGet("date-{id}")] public async Task> 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(); } } }