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 AMESCoreStudio.WebApi; using AMESCoreStudio.WebApi.Models.AMES; using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.AMES { /// <summary> /// /// </summary> [Route("api/[controller]")] [ApiController] public class OutfitLevelInfoesController : ControllerBase { private readonly AMESContext _context; /// <summary> /// /// </summary> /// <param name="context"></param> public OutfitLevelInfoesController(AMESContext context) { _context = context; } /// <summary> /// /// </summary> /// <returns></returns> // GET: api/OutfitLevelInfoes [HttpGet] public async Task<ActionResult<IEnumerable<OutfitLevelInfo>>> GetOutfitLevelInfoes() { IQueryable<OutfitLevelInfo> q = _context.OutfitLevelInfoes; q = q.OrderBy(p => p.LevelNo); var items = await q.ToListAsync(); return items; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/OutfitLevelInfoes/5 [HttpGet("{id}")] public async Task<ActionResult<IEnumerable<OutfitLevelInfo>>> GetOutfitLevelInfoes(string id) { IQueryable<OutfitLevelInfo> q = _context.OutfitLevelInfoes; q = q.Where(p => p.LevelNo.Equals(id)); var items = await q.ToListAsync(); if (items == null) { return NotFound(); } return items; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="items"></param> /// <returns></returns> // PUT: api/OutfitLevelInfoes/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<OutfitLevelInfo>> PutOutfitLevelInfoes(string id,[FromBody] OutfitLevelInfo items) { ResultModel<OutfitLevelInfo> result = new ResultModel<OutfitLevelInfo>(); if (id != items.LevelNo) { result.Success = false; result.Msg = "區域代碼錯誤"; return result; } _context.Entry(items).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!OutfitLevelInfoesExists(id)) { result.Success = false; result.Msg = "區域代碼不存在"; return result; } else { throw; } } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// /// </summary> /// <param name="items"></param> /// <returns></returns> // POST: api/OutfitLevelInfoes // 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<OutfitLevelInfo>> PostOutfitLevelInfoes([FromBody] OutfitLevelInfo items) { ResultModel<OutfitLevelInfo> result = new ResultModel<OutfitLevelInfo>(); _context.OutfitLevelInfoes.Add(items); try { await _context.SaveChangesAsync(); } catch (DbUpdateException) { if (OutfitLevelInfoesExists(items.LevelNo)) { result.Success = false; result.Msg = "區域代碼重複"; return result; } else { throw; } } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/OutfitLevelInfoes/5 [HttpDelete("{id}")] public async Task<ResultModel<OutfitLevelInfo>> DeleteOutfitLevelInfoes(string id) { ResultModel<OutfitLevelInfo> result = new ResultModel<OutfitLevelInfo>(); var items = await _context.OutfitLevelInfoes.FindAsync(id); if (items == null) { result.Success = false; result.Msg = "區域代碼不存在"; return result; } _context.OutfitLevelInfoes.Remove(items); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; } private bool OutfitLevelInfoesExists(string id) { return _context.OutfitLevelInfoes.Any(e => e.LevelNo == id); } } }