parking-webapi/WebApi_data_value/Controllers/Parking_spaces_lcd_instandController.cs

167 lines
5.7 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;
namespace Parking_space_WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
[ApiExplorerSettings(GroupName = "校園外網")]
public class Parking_spaces_lcd_instandController : ControllerBase
{
private readonly SqlContext _context;
public Parking_spaces_lcd_instandController(SqlContext context)
{
_context = context;
}
#region
// GET: api/Parking_spaces_lcd_instand
/// <summary>
/// 取出字幕機所有資料(URL 區域 顯示數量)
/// </summary>
[HttpGet]
public async Task<ActionResult<IEnumerable<Parking_spaces_lcd_instand>>> Getparking_spaces_lcd_instand()
{
if (_context.parking_spaces_lcd_instand == null)
{
return NotFound();
}
return await _context.parking_spaces_lcd_instand.ToListAsync();
}
#endregion
#region
// GET: api/Parking_spaces_lcd_instand/5
/// <summary>
/// 從區域名稱抓字幕機資料
/// </summary>
[HttpGet("parking_space_area-{id}")]
public async Task<IEnumerable<Parking_spaces_lcd_instand>> GetParking_spaces_lcd_instand(string id)
{
var parking_spaces_lcd_instand = await(from c in _context.parking_spaces_lcd_instand
where c.parking_spaces_name ==id
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,
}).ToArrayAsync();
return parking_spaces_lcd_instand;
}
#endregion
#region
/// <summary>
/// 更新字幕機資料
/// </summary>
// PUT: api/Parking_spaces_lcd_instand/5
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPut("{id}")]
public async Task<IActionResult> PutParking_spaces_lcd_instand(string id, Parking_spaces_lcd_instand parking_spaces_lcd_instand)
{
if (id != parking_spaces_lcd_instand.lcd_ip)
{
return BadRequest();
}
_context.Entry(parking_spaces_lcd_instand).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!Parking_spaces_lcd_instandExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return NoContent();
}
#endregion
#region
/// <summary>
/// 新增字幕機資料(URL 區域 顯示數量)
/// </summary>
// POST: api/Parking_spaces_lcd_instand
// To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754
[HttpPost]
public async Task<ActionResult<Parking_spaces_lcd_instand>> PostParking_spaces_lcd_instand(Parking_spaces_lcd_instand parking_spaces_lcd_instand)
{
if (_context.parking_spaces_lcd_instand == null)
{
return Problem("Entity set 'SqlContext.parking_spaces_lcd_instand' is null.");
}
_context.parking_spaces_lcd_instand.Add(parking_spaces_lcd_instand);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (Parking_spaces_lcd_instandExists(parking_spaces_lcd_instand.lcd_ip))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtAction("GetParking_spaces_lcd_instand", new { id = parking_spaces_lcd_instand.lcd_ip }, parking_spaces_lcd_instand);
}
#endregion
#region
/// <summary>
/// 刪除字幕機資料
/// </summary>
// DELETE: api/Parking_spaces_lcd_instand/5
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteParking_spaces_lcd_instand(string id)
{
if (_context.parking_spaces_lcd_instand == null)
{
return NotFound();
}
var parking_spaces_lcd_instand = await _context.parking_spaces_lcd_instand.FindAsync(id);
if (parking_spaces_lcd_instand == null)
{
return NotFound();
}
_context.parking_spaces_lcd_instand.Remove(parking_spaces_lcd_instand);
await _context.SaveChangesAsync();
return NoContent();
}
#endregion
private bool Parking_spaces_lcd_instandExists(string id)
{
return (_context.parking_spaces_lcd_instand?.Any(e => e.lcd_ip == id)).GetValueOrDefault();
}
}
}