212 lines
6.2 KiB

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 NgComponentsController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public NgComponentsController(AMESContext context)
{
_context = context;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
// GET: api/NgComponents
[HttpGet]
public async Task<ActionResult<IEnumerable<NgComponent>>> GetNgComponent()
{
return await _context.NgComponents.ToListAsync();
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgComponents/5
[HttpGet("{id}")]
public async Task<ActionResult<IEnumerable<NgComponent>>> GetNgComponent(int id)
{
IQueryable<NgComponent> q = _context.NgComponents;
q = q.Where(p => p.ComponentID.Equals(id));
var ngComponent = await q.ToListAsync();
if (ngComponent == null)
{
return NotFound();
}
return ngComponent;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgComponents/5
[HttpGet("NGID/{id}")]
public async Task<ActionResult<IEnumerable<NgComponent>>> GetNgComponentByNGID(int id)
{
IQueryable<NgComponent> q = _context.NgComponents;
q = q.Where(p => p.NgID.Equals(id));
try
{
var ngComponent = await q.ToListAsync();
if (ngComponent == null)
{
return NotFound();
}
return ngComponent;
}
catch (Exception e1)
{
return NotFound();
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="ngComponent"></param>
/// <returns></returns>
// PUT: api/NgComponents/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<NgComponent>> PutNgComponent(int id, NgComponent ngComponent)
{
ResultModel<NgComponent> result = new ResultModel<NgComponent>();
if (id != ngComponent.ComponentID)
{
result.Success = false;
result.Msg = "不良零件ID錯誤";
return result;
}
_context.Entry(ngComponent).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NgComponentExists(id))
{
result.Success = false;
result.Msg = "不良零件ID不存在";
return result;
}
else
{
throw;
}
}
result.Success = true;
result.Msg = "OK";
return result;
}
/// <summary>
///
/// </summary>
/// <param name="ngComponent"></param>
/// <returns></returns>
// POST: api/NgComponents
// 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<NgComponent>> PostNgComponent(NgComponent ngComponent)
{
ResultModel<NgComponent> result = new ResultModel<NgComponent>();
Helper helper = new Helper(_context);
ngComponent.ComponentID = helper.GetIDKey("COMPONENT_ID").Result;
_context.NgComponents.Add(ngComponent);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (NgComponentExists(ngComponent.ComponentID))
{
result.Success = false;
result.Msg = "不良零件ID重複";
return result;
}
else
{
throw;
}
}
result.Success = true;
result.Msg = "OK"+"-"+ngComponent.ComponentID;
return result;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/NgComponents/5
[HttpDelete("{id}")]
public async Task<ResultModel<NgComponent>> DeleteNgComponent(decimal id)
{
ResultModel<NgComponent> result = new ResultModel<NgComponent>();
var ngComponent = await _context.NgComponents.FindAsync(id);
if (ngComponent == null)
{
result.Success = false;
result.Msg = "不良零件ID不存在";
return result;
}
_context.NgComponents.Remove(ngComponent);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
return result;
}
private bool NgComponentExists(decimal id)
{
return _context.NgComponents.Any(e => e.ComponentID == id);
}
}
}