177 lines
5.7 KiB
C#
177 lines
5.7 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 Parking_space_WebAPI.Models;
|
||
using Parking_space_WebAPI.Authorization;
|
||
using Parking_space_WebAPI.Services;
|
||
|
||
namespace Parking_space_WebAPI.Controllers
|
||
{
|
||
[Route("api/[controller]")]
|
||
[ApiController]
|
||
[Authorize]
|
||
[ApiExplorerSettings(GroupName = "校園外網")]
|
||
public class Parking_space_algorithmController : ControllerBase
|
||
{
|
||
private readonly SqlContext _context;
|
||
private static int serialNumber = 1; // 初始流水号
|
||
|
||
public Parking_space_algorithmController(SqlContext context)
|
||
{
|
||
_context = context;
|
||
}
|
||
|
||
#region 獲取所有演算法狀態的資料
|
||
// GET: api/Parking_space_algorithm
|
||
/// <summary>
|
||
/// 獲取所有演算法狀態的資料
|
||
/// </summary>
|
||
[HttpGet]
|
||
public async Task<ActionResult<IEnumerable<Parking_space_algorithm>>> Getparking_space_algorithm()
|
||
{
|
||
if (_context.parking_space_algorithm == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
return await _context.parking_space_algorithm.ToListAsync();
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 獲取單一演算法狀態資料
|
||
// GET: api/Parking_space_algorithm/5
|
||
/// <summary>
|
||
/// 獲取單一演算法狀態資料
|
||
/// </summary>
|
||
[HttpGet("{id}")]
|
||
public async Task<ActionResult<Parking_space_algorithm>> GetParking_space_algorithm(string id)
|
||
{
|
||
if (_context.parking_space_algorithm == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
var parking_space_algorithm = await _context.parking_space_algorithm.FindAsync(id);
|
||
|
||
if (parking_space_algorithm == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
|
||
return parking_space_algorithm;
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 編輯單一演算法狀態資料
|
||
// PUT: api/Parking_space_algorithm/5
|
||
/// <summary>
|
||
/// 編輯單一演算法狀態資料
|
||
/// </summary>
|
||
[HttpPut("{id}")]
|
||
public async Task<IActionResult> PutParking_space_algorithm(string id, Parking_space_algorithm parking_space_algorithm)
|
||
{
|
||
if (id != parking_space_algorithm.algorithm_serial_num)
|
||
{
|
||
return BadRequest();
|
||
}
|
||
|
||
_context.Entry(parking_space_algorithm).State = EntityState.Modified;
|
||
|
||
try
|
||
{
|
||
await _context.SaveChangesAsync();
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
if (!Parking_space_algorithmExists(id))
|
||
{
|
||
return NotFound();
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
|
||
return NoContent();
|
||
}
|
||
#endregion
|
||
|
||
|
||
#region 新增演算法狀態資料
|
||
// POST: api/Parking_space_algorithm
|
||
/// <summary>
|
||
/// 新增演算法狀態資料
|
||
/// </summary>
|
||
[HttpPost]
|
||
public async Task<ActionResult<Parking_space_algorithm>> PostParking_space_algorithm(Parking_space_algorithm parking_space_algorithm)
|
||
{
|
||
if (_context.parking_space_algorithm == null)
|
||
{
|
||
return Problem("Entity set 'SqlContext.parking_space_algorithm' is null.");
|
||
}
|
||
// 获取当前日期时间,并将其格式化为字符串
|
||
string formattedDateTime = DateTime.Now.ToString("yyyyMMddHHmmss") + serialNumber.ToString();
|
||
// 生成流水号,例如:SN20231124161404
|
||
string formattedSerialNumber = $"SN{formattedDateTime}";
|
||
parking_space_algorithm.algorithm_serial_num = formattedDateTime;
|
||
parking_space_algorithm.run_time = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
|
||
_context.parking_space_algorithm.Add(parking_space_algorithm);
|
||
// 增加流水号以便下次使用
|
||
serialNumber++;
|
||
try
|
||
{
|
||
await _context.SaveChangesAsync();
|
||
}
|
||
catch (DbUpdateException)
|
||
{
|
||
if (Parking_space_algorithmExists(parking_space_algorithm.algorithm_serial_num))
|
||
{
|
||
return Conflict();
|
||
}
|
||
else
|
||
{
|
||
throw;
|
||
}
|
||
}
|
||
|
||
return CreatedAtAction("GetParking_space_algorithm", new { id = parking_space_algorithm.algorithm_serial_num }, parking_space_algorithm);
|
||
}
|
||
#endregion
|
||
|
||
#region 刪除演算法狀態資料
|
||
// DELETE: api/Parking_space_algorithm/5
|
||
/// <summary>
|
||
/// 刪除演算法狀態資料
|
||
/// </summary>
|
||
[HttpDelete("{id}")]
|
||
public async Task<IActionResult> DeleteParking_space_algorithm(string id)
|
||
{
|
||
if (_context.parking_space_algorithm == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
var parking_space_algorithm = await _context.parking_space_algorithm.FindAsync(id);
|
||
if (parking_space_algorithm == null)
|
||
{
|
||
return NotFound();
|
||
}
|
||
|
||
_context.parking_space_algorithm.Remove(parking_space_algorithm);
|
||
await _context.SaveChangesAsync();
|
||
|
||
return NoContent();
|
||
}
|
||
#endregion
|
||
|
||
private bool Parking_space_algorithmExists(string id)
|
||
{
|
||
return (_context.parking_space_algorithm?.Any(e => e.algorithm_serial_num == id)).GetValueOrDefault();
|
||
}
|
||
}
|
||
}
|