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.ViewModel;
using Parking_space_WebAPI.Authorization;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園外網")]
public class Parking_spaces_historyController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_historyController(SqlContext context)
{
_context = context;
}
#region 獲取所有停車歷時資料
// GET: api/Parking_spaces_history
///
/// 獲取所有停車歷時資料
///
[HttpGet]
public async Task>> Getparking_spaces_history()
{
if (_context.parking_spaces_history == null)
{
return NotFound();
}
return await _context.parking_spaces_history.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_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
///
/// 新增一筆停車資料
///
[HttpPost]
public async Task> 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 透過車牌號碼找出資料
///
/// 透過車牌號碼找出資料
///
[HttpGet("license_plate_number-{id}")]
public async Task> 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 透過時間找出資料
///
/// 透過時間找出資料
///
[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_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
///
/// 更改歷史停車資料
///
[HttpPut("license_plate_number-{id}-In_time-{date}")]
public async Task 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();
}
}
}