parking-webapi/WebApi_data_value/Controllers/Parking_spaces_total_tableController.cs
2024-02-01 13:48:38 +08:00

294 lines
13 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 MySqlX.XDevAPI.Common;
using WebApi_data_value.Models;
using WebApi_data_value.Services;
namespace WebApi_data_value.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Parking_spaces_total_tableController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_total_tableController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Parking_spaces_total_table
/// <summary>
/// 獲取停車場所有資料
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Parking_spaces_total_table>>> Getparking_spaces_total_table()
{
if (_context.parking_spaces_total_table == null)
{
return NotFound();
}
return await _context.parking_spaces_total_table.ToListAsync();
}
#endregion
#region
// GET: api/Parking_spaces_total_table/5
/// <summary>
/// 獲取停車場單一資料
/// </summary>
[HttpGet("{id}")]
public async Task<ActionResult<Parking_spaces_total_table>> GetParking_spaces_total_table(string id)
{
if (_context.parking_spaces_total_table == null)
{
return NotFound();
}
var parking_spaces_total_table = await _context.parking_spaces_total_table.FindAsync(id);
if (parking_spaces_total_table == null)
{
return NotFound();
}
return parking_spaces_total_table;
}
#endregion
#region
// PUT: api/Parking_spaces_total_table/5
/// <summary>
/// 更改停車區資料
/// </summary>
[HttpPut("{id}")]
public async Task<IActionResult> PutParking_spaces_total_table(string id, Parking_spaces_total_table parking_spaces_total_table)
{
if (id != parking_spaces_total_table.parking_spaces_name)
{
return BadRequest();
}
_context.Entry(parking_spaces_total_table).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Parking_spaces_total_tableExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
#endregion
#region
// POST: api/Parking_spaces_total_table
/// <summary>
/// 新增一筆停車區資料
/// </summary>
[HttpPost]
public async Task<ActionResult<Parking_spaces_total_table>> PostParking_spaces_total_table(Parking_spaces_total_table parking_spaces_total_table)
{
if (_context.parking_spaces_total_table == null)
{
return Problem("Entity set 'SqlContext.parking_spaces_total_table' is null.");
}
_context.parking_spaces_total_table.Add(parking_spaces_total_table);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Parking_spaces_total_tableExists(parking_spaces_total_table.parking_spaces_name))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetParking_spaces_total_table", new { id = parking_spaces_total_table.parking_spaces_name }, parking_spaces_total_table);
}
#endregion
#region
// DELETE: api/Parking_spaces_total_table/5
/// <summary>
/// 刪除停車區資料
/// </summary>
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteParking_spaces_total_table(string id)
{
if (_context.parking_spaces_total_table == null)
{
return NotFound();
}
var parking_spaces_total_table = await _context.parking_spaces_total_table.FindAsync(id);
if (parking_spaces_total_table == null)
{
return NotFound();
}
_context.parking_spaces_total_table.Remove(parking_spaces_total_table);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
#region 1
// GET: api/Parking_spaces_total_table/5
/// <summary>
/// 針對指定停車場現在車位數量加1
/// </summary>
[HttpGet("Parking_spaces_total_table_add-{id}")]
public async Task<ActionResult<Parking_spaces_total_table>> Parking_spaces_total_table_add(string id)
{
if (_context.parking_spaces_total_table == null)
{
return NotFound();
}
//var parking_spaces_total_table = await _context.parking_spaces_total_table.FindAsync(id);
var parking_spaces_total_table = await (from c in _context.parking_spaces_total_table
where c.parking_spaces_name == id
select new Parking_spaces_total_table
{
parking_spaces_name = c.parking_spaces_name,
parking_spaces_total_num = c.parking_spaces_total_num,
longitude = c.longitude,
latitude = c.latitude,
parking_spaces_now_num = c.parking_spaces_now_num,
}
).FirstAsync();
_context.parking_spaces_total_table.Remove(parking_spaces_total_table);
await _context.SaveChangesAsync();
if (parking_spaces_total_table.parking_spaces_now_num != null)
{
string parking_spaces_now_num = parking_spaces_total_table.parking_spaces_now_num; //單獨取出資料庫目前停車數量
int parking_spaces_now_num_int;
Int32.TryParse(parking_spaces_now_num, out parking_spaces_now_num_int); //轉int格式
parking_spaces_now_num_int = parking_spaces_now_num_int+1; //停車數量加1
parking_spaces_now_num = parking_spaces_now_num_int.ToString();//轉string格式
parking_spaces_total_table.parking_spaces_now_num = parking_spaces_now_num;
_context.parking_spaces_total_table.Remove(parking_spaces_total_table);
}
_context.parking_spaces_total_table.Add(parking_spaces_total_table);
await _context.SaveChangesAsync();
var parking_spaces_lcd_instand = await (from c in _context.parking_spaces_lcd_instand
where c.parking_spaces_name == parking_spaces_total_table.parking_spaces_name
select new Parking_spaces_lcd_instand
{
lcd_ip = c.lcd_ip,
parking_spaces_name = c.parking_spaces_name,
parking_space_amount = c.parking_space_amount,
}
).FirstAsync();
_context.parking_spaces_lcd_instand.Remove(parking_spaces_lcd_instand);
await _context.SaveChangesAsync();
parking_spaces_lcd_instand.parking_space_amount = parking_spaces_total_table.parking_spaces_now_num;
_context.parking_spaces_lcd_instand.Add(parking_spaces_lcd_instand);
await _context.SaveChangesAsync();
if (parking_spaces_total_table == null)
{
return NotFound();
}
return parking_spaces_total_table;
}
#endregion
#region 1
// GET: api/Parking_spaces_total_table/5
/// <summary>
/// 針對指定停車場現在車位數量減1
/// </summary>
[HttpGet("Parking_spaces_total_table_subtract-{id}")]
public async Task<ActionResult<Parking_spaces_total_table>> Parking_spaces_total_table_subtract(string id)
{
if (_context.parking_spaces_total_table == null)
{
return NotFound();
}
//var parking_spaces_total_table = await _context.parking_spaces_total_table.FindAsync(id);
var parking_spaces_total_table = await (from c in _context.parking_spaces_total_table
where c.parking_spaces_name == id
select new Parking_spaces_total_table
{
parking_spaces_name = c.parking_spaces_name,
parking_spaces_total_num = c.parking_spaces_total_num,
longitude = c.longitude,
latitude = c.latitude,
parking_spaces_now_num = c.parking_spaces_now_num,
}
).FirstAsync();
_context.parking_spaces_total_table.Remove(parking_spaces_total_table);
await _context.SaveChangesAsync();
if (parking_spaces_total_table.parking_spaces_now_num != null)
{
string parking_spaces_now_num = parking_spaces_total_table.parking_spaces_now_num; //單獨取出資料庫目前停車數量
int parking_spaces_now_num_int;
Int32.TryParse(parking_spaces_now_num, out parking_spaces_now_num_int); //轉int格式
parking_spaces_now_num_int = parking_spaces_now_num_int - 1; //停車數量減1
parking_spaces_now_num = parking_spaces_now_num_int.ToString();//轉string格式
parking_spaces_total_table.parking_spaces_now_num = parking_spaces_now_num;
_context.parking_spaces_total_table.Remove(parking_spaces_total_table);
}
_context.parking_spaces_total_table.Add(parking_spaces_total_table);
await _context.SaveChangesAsync();
var parking_spaces_lcd_instand = await (from c in _context.parking_spaces_lcd_instand
where c.parking_spaces_name == parking_spaces_total_table.parking_spaces_name
select new Parking_spaces_lcd_instand
{
lcd_ip = c.lcd_ip,
parking_spaces_name = c.parking_spaces_name,
parking_space_amount = c.parking_space_amount,
}
).FirstAsync();
_context.parking_spaces_lcd_instand.Remove(parking_spaces_lcd_instand);
await _context.SaveChangesAsync();
parking_spaces_lcd_instand.parking_space_amount = parking_spaces_total_table.parking_spaces_now_num;
_context.parking_spaces_lcd_instand.Add(parking_spaces_lcd_instand);
await _context.SaveChangesAsync();
if (parking_spaces_total_table == null)
{
return NotFound();
}
return parking_spaces_total_table;
}
#endregion
private bool Parking_spaces_total_tableExists(string id)
{
return (_context.parking_spaces_total_table?.Any(e => e.parking_spaces_name == id)).GetValueOrDefault();
}
}
}