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.SYS; using AMESCoreStudio.CommonTools.Result; namespace AMESCoreStudio.WebApi.Controllers.SYS { /// <summary> /// /// </summary> [Route("api/[controller]")] [ApiController] public class TestInfoesController : ControllerBase { private readonly AMESContext _context; /// <summary> /// /// </summary> /// <param name="context"></param> public TestInfoesController(AMESContext context) { _context = context; } /// <summary> /// /// </summary> /// <returns></returns> // GET: api/TestInfoes [HttpGet] public async Task<ResultModel<TestInfo>> GetTestInfo(int page = 0, int limit = 10) { ResultModel<TestInfo> result = new ResultModel<TestInfo>(); IQueryable<TestInfo> q = _context.TestInfoes; q = q.Where(p => p.TestID > 0); result.DataTotal = q.ToList().Count; if (page > 0) { q = q.OrderBy(p => p.TestString).Skip((page - 1) * limit).Take(limit); } else { q = q.OrderBy(p => p.TestString); } var testInfo = await q.ToListAsync(); result.Data = testInfo; if (testInfo == null) { result.Msg = "查無資料"; result.Success = false; return result; } result.Msg = "OK"; result.Success = true; return result; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // GET: api/TestInfoes/5 [HttpGet("{id}")] public async Task<ActionResult<IEnumerable<TestInfo>>> GetTestInfo(int id) { IQueryable<TestInfo> q = _context.TestInfoes; q = q.Where(p => p.TestID.Equals(id)); var testInfo = await q.ToListAsync(); if (testInfo == null) { return NotFound(); } return testInfo; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <param name="testInfo"></param> /// <returns></returns> // PUT: api/TestInfoes/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<TestInfo>> PutTestInfo(int id, [FromBody] TestInfo testInfo) { ResultModel<TestInfo> result = new ResultModel<TestInfo>(); if (id != testInfo.TestID) { result.Msg = "测试編號錯誤"; result.Success = false; return result; } _context.Entry(testInfo).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!TestInfoExists(id)) { result.Msg = "测试編號不存在"; result.Success = false; return result; } else { throw; } } result.Msg = "OK"; result.Success = true; return result; } /// <summary> /// /// </summary> /// <param name="testInfo"></param> /// <returns></returns> // POST: api/TestInfoes // 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<TestInfo>> PostTestInfo(TestInfo testInfo) { ResultModel<TestInfo> result = new ResultModel<TestInfo>(); Helper helper = new Helper(_context); testInfo.TestID = helper.GetIDKey("TEST_ID").Result; _context.TestInfoes.Add(testInfo); await _context.SaveChangesAsync(); result.Msg = "OK"; result.Success = true; return result; } /// <summary> /// /// </summary> /// <param name="id"></param> /// <returns></returns> // DELETE: api/TestInfoes/5 [HttpDelete("{id}")] public async Task<ResultModel<TestInfo>> DeleteTestInfo(int id) { ResultModel<TestInfo> result = new ResultModel<TestInfo>(); var testInfo = await _context.TestInfoes.FindAsync(id); if (testInfo == null) { result.Msg = "测试編號不存在"; result.Success = false; return result; } _context.TestInfoes.Remove(testInfo); await _context.SaveChangesAsync(); result.Msg = "OK"; result.Success = true; return result; } private bool TestInfoExists(int id) { return _context.TestInfoes.Any(e => e.TestID == id); } } }