using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using AMESCoreStudio.WebApi.Models.SYS; namespace AMESCoreStudio.WebApi.Controllers.SYS { /// <summary> /// /// </summary> [Route("api/[controller]")] [ApiController] public class SystemInfoesController : ControllerBase { private readonly AMESContext _context; /// <summary> /// /// </summary> /// <param name="context"></param> public SystemInfoesController(AMESContext context) { _context = context; } /// <summary> /// 獲取系統資料 /// </summary> /// <returns></returns> // GET: api/SystemInfoes [HttpGet] public async Task<ActionResult<IEnumerable<SystemInfo>>> GetSystemInfo() { IQueryable<SystemInfo> q = _context.SystemInfoes; q = q.OrderBy(p => p.SystemID); //q = q.OrderByDescending(p => p.SystemID); var systemInfo = await q.ToListAsync(); //return await _context.SystemInfoes.ToListAsync(); return systemInfo; } /// <summary> /// 根據ID查詢系統資料 /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/SystemInfoes/5 [HttpGet("{id}")] public async Task<ActionResult<IEnumerable<SystemInfo>>> GetSystemInfo(int id) { IQueryable<SystemInfo> q = _context.SystemInfoes; q = q.Where(p => p.SystemID.Equals(id)); var systemInfo = await q.ToListAsync(); if (systemInfo == null) { return NotFound(); } return systemInfo; } /// <summary> /// 修改系統資料 /// </summary> /// <param name="id"></param> /// <param name="systemInfo"></param> /// <returns></returns> // PUT: api/SystemInfoes/5 // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPut("{id}")] public async Task<ActionResult<SystemInfo>> PutSystemInfo(int id, [FromBody] SystemInfo systemInfo) { if (id != systemInfo.SystemID) { return BadRequest(); } _context.Entry(systemInfo).State = EntityState.Modified; systemInfo.UpdateDateTime = DateTime.Now; systemInfo.UpdateUserId = 0; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SystemInfoExists(id)) { return NotFound(); } else { throw; } } return systemInfo; //return NoContent(); } /// <summary> /// 新增系統資料 /// </summary> /// <param name="systemInfo"></param> /// <returns></returns> // POST: api/SystemInfoes // To protect from overposting attacks, enable the specific properties you want to bind to, for // more details, see https://go.microsoft.com/fwlink/?linkid=2123754. [HttpPost] public async Task<ActionResult<SystemInfo>> PostSystemInfo([FromBody] SystemInfo systemInfo) { Helper helper = new Helper(_context); systemInfo.SystemID = helper.GetIDKey("SYSTEM_ID").Result; systemInfo.CreateDateTime = DateTime.Now; systemInfo.CreateUserId = 0; _context.SystemInfoes.Add(systemInfo); await _context.SaveChangesAsync(); return CreatedAtAction("GetSystemInfo", new { id = systemInfo.SystemID }, systemInfo); } /// <summary> /// 根據ID刪除系統資料 /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/SystemInfoes/5 [HttpDelete("{id}")] public async Task<ActionResult<SystemInfo>> DeleteSystemInfo(int id) { //var systemInfo = await _context.SystemInfo.FindAsync(id); var systemInfo = await _context.SystemInfoes.Where(m => m.SystemID == id).FirstOrDefaultAsync(); if (systemInfo == null) { return NotFound(); } _context.SystemInfoes.Remove(systemInfo); await _context.SaveChangesAsync(); return systemInfo; } private bool SystemInfoExists(int id) { return _context.SystemInfoes.Any(e => e.SystemID == id); } } }