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 Parking_space_WebAPI.ViewModel; namespace Parking_space_WebAPI.Controllers { [Route("api/[controller]")] [ApiController] [Authorize] [ApiExplorerSettings(GroupName = "校園外網")] public class Parking_spaces_camController : ControllerBase { private readonly SqlContext _context; public Parking_spaces_camController(SqlContext context) { _context = context; } #region 獲取所有cam的資料 // GET: api/Parking_spaces_cam /// /// 獲取所有cam的資料 /// [HttpGet] public async Task> Getparking_spaces_cam() { var parking_spaces_cam = await (from c in _context.parking_spaces_cam orderby c.parking_spaces_name select new Parking_spaces_cam { parking_spaces_name = c.parking_spaces_name, ip = c.ip, mode = c.mode, password = c.password, user_name = c.user_name, url = c.url, port = c.port, http_port = c.http_port }).ToListAsync(); return parking_spaces_cam; } #endregion #region 根據停車場名稱獲取cam的資料 // GET: api/Parking_spaces_cam/5 /// /// 根據停車場名稱獲取cam的資料 /// [HttpGet("parking_spaces_name-{id}")] public async Task> GetParking_spaces_cam(string id) { var parking_spaces_cam = await (from c in _context.parking_spaces_cam where c.parking_spaces_name == id select new Parking_spaces_cam { parking_spaces_name = c.parking_spaces_name, ip = c.ip, mode = c.mode, password = c.password, user_name = c.user_name, url = c.url, port = c.port, http_port = c.http_port }).ToListAsync(); return parking_spaces_cam; } #endregion /* // PUT: api/Parking_spaces_cam/5 // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPut("{id}")] public async Task PutParking_spaces_cam(string id, Parking_spaces_cam parking_spaces_cam) { if (id != parking_spaces_cam.url) { return BadRequest(); } _context.Entry(parking_spaces_cam).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!Parking_spaces_camExists(id)) { return NotFound(); } else { throw; } } return NoContent(); } */ #region 新增cam資料 // POST: api/Parking_spaces_cam /// /// 新增cam資料 /// // To protect from overposting attacks, see https://go.microsoft.com/fwlink/?linkid=2123754 [HttpPost] public async Task> PostParking_spaces_cam(Parking_spaces_cam parking_spaces_cam) { if (_context.parking_spaces_cam == null) { return Problem("Entity set 'SqlContext.parking_spaces_cam' is null."); } _context.parking_spaces_cam.Add(parking_spaces_cam); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (Parking_spaces_camExists(parking_spaces_cam.url)) { return Conflict(); } else { throw; } } return CreatedAtAction("GetParking_spaces_cam", new { id = parking_spaces_cam.url }, parking_spaces_cam); } #endregion #region 刪除cam資料 // DELETE: api/Parking_spaces_cam/5 /// /// 刪除cam資料 /// [HttpDelete("{id}")] public async Task DeleteParking_spaces_cam(string id) { if (_context.parking_spaces_cam == null) { return NotFound(); } var parking_spaces_cam = await _context.parking_spaces_cam.FindAsync(id); if (parking_spaces_cam == null) { return NotFound(); } _context.parking_spaces_cam.Remove(parking_spaces_cam); await _context.SaveChangesAsync(); return NoContent(); } #endregion private bool Parking_spaces_camExists(string id) { return (_context.parking_spaces_cam?.Any(e => e.url == id)).GetValueOrDefault(); } } }