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 ExceptionWorktimeBlobsController : ControllerBase { private readonly AMESContext _context; /// <summary> /// /// </summary> /// <param name="context"></param> public ExceptionWorktimeBlobsController(AMESContext context) { _context = context; } /// <summary> /// 获取全部異常附件資料 /// </summary> /// <returns></returns> // GET: api/ExceptionWorktimeBlobs [HttpGet] public async Task<ActionResult<IEnumerable<ExceptionWorktimeBlob>>> GetExceptionWorktimeBlobs() { IQueryable<ExceptionWorktimeBlob> q = _context.ExceptionWorktimeBlobs; q = q.OrderBy(p => p.ExceptionWorktimeID); var ExceptionWorktimeBlobs = await q.ToListAsync(); return ExceptionWorktimeBlobs; } /// <summary> /// 用ID获取该異常附件資料 /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/ExceptionWorktimeBlobs/5 [HttpGet("{id}")] public async Task<ActionResult<IEnumerable<ExceptionWorktimeBlob>>> GetExceptionWorktimeBlobs(int id) { IQueryable<ExceptionWorktimeBlob> q = _context.ExceptionWorktimeBlobs; q = q.Where(p => p.ExceptionWorktimeID.Equals(id)); var ExceptionWorktimeBlob = await q.ToListAsync(); if (ExceptionWorktimeBlob == null) { return NotFound(); } return ExceptionWorktimeBlob; } /// <summary> /// 更新異常附件資料 /// </summary> /// <param name="id"></param> /// <param name="ExceptionWorktimeBlob"></param> /// <returns></returns> // PUT: api/InspectionResultBlobs/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<ExceptionWorktimeBlob>> PutExceptionWorktimeBlobs(int id, [FromBody] ExceptionWorktimeBlob ExceptionWorktimeBlob) { ResultModel<ExceptionWorktimeBlob> result = new ResultModel<ExceptionWorktimeBlob>(); if (id != ExceptionWorktimeBlob.ExceptionWorktimeID) { result.Success = false; result.Msg = "ID錯誤"; return result; } _context.Entry(ExceptionWorktimeBlob).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (Exception e) { result.Success = false; result.Msg = e.Message; return result; } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// 新增異常附件資料 /// </summary> /// <param name="ExceptionWorktimeBlob"></param> /// <returns></returns> // POST: api/InspectionResultBlobs // 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<ExceptionWorktimeBlob>> PostExceptionWorktimeBlobs(ExceptionWorktimeBlob ExceptionWorktimeBlob) { ResultModel<ExceptionWorktimeBlob> result = new ResultModel<ExceptionWorktimeBlob>(); Helper helper = new Helper(_context); _context.ExceptionWorktimeBlobs.Add(ExceptionWorktimeBlob); try { await _context.SaveChangesAsync(); } catch (Exception e) { result.Success = false; result.Msg = e.Message; return result; } result.Success = true; result.Msg = "OK"; return result; } /// <summary> /// 删除巡檢類別資料 /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/InspectionResultBlobs/5 [HttpDelete("{id}")] public async Task<ResultModel<ExceptionWorktimeBlob>> DeleteExceptionWorktimeBlobs(int id) { ResultModel<ExceptionWorktimeBlob> result = new ResultModel<ExceptionWorktimeBlob>(); var inspectionType = await _context.ExceptionWorktimeBlobs.Where(m => m.ExceptionWorktimeID == id).FirstOrDefaultAsync(); if (inspectionType == null) { result.Success = false; result.Msg = "異常工時ID不存在"; return result; } _context.ExceptionWorktimeBlobs.Remove(inspectionType); await _context.SaveChangesAsync(); result.Success = true; result.Msg = "OK"; return result; } private bool ExceptionWorktimeBlobsExists(int id) { return _context.ExceptionWorktimeBlobs.Any(e => e.ExceptionWorktimeID == id); } } }