parking-webapi/WebApi_data_value/Controllers/Yuntech_monthly_rent_numberController.cs

224 lines
8.0 KiB
C#
Raw Normal View History

2024-09-28 01:25:42 +08:00
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.Authorization;
using WebApi_data_value.Models;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園大內網")]
public class Yuntech_monthly_rent_numberController : ControllerBase
{
private readonly SqlContext _context;
public Yuntech_monthly_rent_numberController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Yuntech_monthly_rent_number
/// <summary>
/// 取出所有月租臨停資料(URL 區域 顯示數量)
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Yuntech_monthly_rent_number>>> GetYuntech_monthly_rent_number()
{
if (_context.yuntech_monthly_rent_number == null)
{
return NotFound();
}
return await _context.yuntech_monthly_rent_number.ToListAsync();
}
#endregion
#region
// GET: api/Yuntech_monthly_rent_number/parking_space_area-{id}
/// <summary>
/// 從類別抓月租臨停資料
/// </summary>
[HttpGet("parking_space_area-{id}")]
public async Task<IEnumerable<Yuntech_monthly_rent_number>> GetYuntech_monthly_rent_number(string id)
{
var yuntech_monthly_rent_number = await (from c in _context.yuntech_monthly_rent_number
where c.category == id
select new Yuntech_monthly_rent_number
{
category = c.category,
number = c.number,
}).ToArrayAsync();
return yuntech_monthly_rent_number;
}
#endregion
#region
/// <summary>
/// 更新月租臨停資料
/// </summary>
// PUT: api/Yuntech_monthly_rent_number/{id}
[HttpPut("{id}")]
public async Task<IActionResult> PutYuntech_monthly_rent_number(string id, Yuntech_monthly_rent_number yuntech_monthly_rent_number)
{
if (id != yuntech_monthly_rent_number.category)
{
return BadRequest();
}
_context.Entry(yuntech_monthly_rent_number).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Yuntech_monthly_rent_numberExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
#endregion
#region
/// <summary>
/// 新增月租臨停資料(URL 區域 顯示數量)
/// </summary>
// POST: api/Yuntech_monthly_rent_number
[HttpPost]
public async Task<ActionResult<Yuntech_monthly_rent_number>> PostYuntech_monthly_rent_number(Yuntech_monthly_rent_number yuntech_monthly_rent_number)
{
if (_context.yuntech_monthly_rent_number == null)
{
return Problem("Entity set 'SqlContext.yuntech_monthly_rent_number' is null.");
}
_context.yuntech_monthly_rent_number.Add(yuntech_monthly_rent_number);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Yuntech_monthly_rent_numberExists(yuntech_monthly_rent_number.category))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetYuntech_monthly_rent_number", new { id = yuntech_monthly_rent_number.category }, yuntech_monthly_rent_number);
}
#endregion
#region
/// <summary>
/// 刪除月租臨停資料
/// </summary>
// DELETE: api/Yuntech_monthly_rent_number/{id}
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteYuntech_monthly_rent_number(string id)
{
if (_context.yuntech_monthly_rent_number == null)
{
return NotFound();
}
var yuntech_monthly_rent_number = await _context.yuntech_monthly_rent_number.FindAsync(id);
if (yuntech_monthly_rent_number == null)
{
return NotFound();
}
_context.yuntech_monthly_rent_number.Remove(yuntech_monthly_rent_number);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region
/// <summary>
/// 更新車位數量
/// </summary>
[HttpPost("update-parking-number")]
public async Task<IActionResult> UpdateParkingNumber(string license_plate_number, bool isIncrease)
{
int change = isIncrease ? 1 : -1;
// 更新車位數量
var yuntech_parking = await _context.yuntech_parking.FirstOrDefaultAsync();
if (yuntech_parking != null)
{
string now_num_str = yuntech_parking.now_num;
int now_num_int;
Int32.TryParse(now_num_str, out now_num_int);
now_num_int += change;
now_num_str = now_num_int.ToString();
yuntech_parking.now_num = now_num_str;
// 保存更改
await _context.SaveChangesAsync();
}
// 比對車牌號碼
var in_list_data = await _context.yuntech_parking_user_list
.FirstOrDefaultAsync(user => user.user_license_plate_number == license_plate_number);
if (in_list_data != null)
{
// 車牌號碼相同,更新月租車位數量
var monthly_rent_data = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "月租");
if (monthly_rent_data != null)
{
int now_number;
Int32.TryParse(monthly_rent_data.number, out now_number);
now_number += change;
monthly_rent_data.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
else
{
// 車牌號碼不同,更新臨停車位數量
var temp_rent_data = await _context.yuntech_monthly_rent_number
.FirstOrDefaultAsync(rent => rent.category == "臨停");
if (temp_rent_data != null)
{
int now_number;
Int32.TryParse(temp_rent_data.number, out now_number);
now_number += change;
temp_rent_data.number = now_number.ToString();
// 保存更改
await _context.SaveChangesAsync();
}
}
return Ok();
}
#endregion
private bool Yuntech_monthly_rent_numberExists(string id)
{
return (_context.yuntech_monthly_rent_number?.Any(e => e.category == id)).GetValueOrDefault();
}
}
}