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; using AMESCoreStudio.CommonTools.Result; 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> /// <param name="page"></param> /// <param name="limit"></param> /// <returns></returns> // GET: api/SystemInfoes [HttpGet] public async Task<ResultModel<SystemInfo>> GetSystemInfo(int page = 0, int limit = 10) { ResultModel<SystemInfo> result = new ResultModel<SystemInfo>(); IQueryable<SystemInfo> q = _context.SystemInfoes; result.DataTotal = q.ToList().Count; if (page > 0) { q = q.OrderBy(p => p.SystemID).Skip((page - 1) * limit).Take(limit); ; } else { q = q.OrderBy(p => p.SystemID); } var systemInfo = await q.ToListAsync(); result.Data = systemInfo; if (systemInfo == null) { result.Msg = "查無資料"; result.Success = false; return result; } result.Success = true; result.Msg = "OK"; return result; } /// <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<ResultModel<SystemInfo>> PutSystemInfo(int id, [FromBody] SystemInfo systemInfo) { ResultModel<SystemInfo> result = new ResultModel<SystemInfo>(); if (id != systemInfo.SystemID) { result.Success = false; result.Msg = "系統編號錯誤"; return result; } _context.Entry(systemInfo).State = EntityState.Modified; systemInfo.UpdateDateTime = DateTime.Now; systemInfo.UpdateUserId = 0; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!SystemInfoExists(id)) { result.Success = false; result.Msg = "系統編號不存在"; return result; } else { throw; } } result.Success = true; result.Msg = "OK"; return result; } /// <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<ResultModel<SystemInfo>> PostSystemInfo([FromBody] SystemInfo systemInfo) { ResultModel<SystemInfo> result = new ResultModel<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(); result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// 根據ID刪除系統資料 /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/SystemInfoes/5 [HttpDelete("{id}")] public async Task<ResultModel<SystemInfo>> DeleteSystemInfo(int id) { ResultModel<SystemInfo> result = new ResultModel<SystemInfo>(); var systemInfo = await _context.SystemInfoes.Where(m => m.SystemID == id).FirstOrDefaultAsync(); if (systemInfo == null) { result.Success = false; result.Msg = "系統編號不存在"; return result; } _context.SystemInfoes.Remove(systemInfo); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; } private bool SystemInfoExists(int id) { return _context.SystemInfoes.Any(e => e.SystemID == id); } } }