You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

213 lines
6.1 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 NgKeypartsController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public NgKeypartsController(AMESContext context)
{
_context = context;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
// GET: api/NgKeyparts
[HttpGet]
public async Task<ActionResult<IEnumerable<NgKeypart>>> GetNgKeypart()
{
return await _context.NgKeyparts.ToListAsync();
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgKeyparts/5
[HttpGet("{id}")]
public async Task<ActionResult<IEnumerable<NgKeypart>>> GetNgKeypart(int id)
{
IQueryable<NgKeypart> q = _context.NgKeyparts;
q = q.Where(p => p.KeypartID.Equals(id));
var ngKeypart = await q.ToListAsync();
if (ngKeypart == null)
{
return NotFound();
}
return ngKeypart;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgKeyparts/5
[HttpGet("ComponentID/{id}")]
public async Task<ActionResult<IEnumerable<NgKeypart>>> GetNgKeypartByComponentID(int id)
{
IQueryable<NgKeypart> q = _context.NgKeyparts;
q = q.Where(p => p.ComponentID.Equals(id));
try
{
var ngKeypart = await q.ToListAsync();
if (ngKeypart == null)
{
return NotFound();
}
return ngKeypart;
}
catch (Exception e1)
{
return NotFound();
}
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="ngKeypart"></param>
/// <returns></returns>
// PUT: api/NgKeyparts/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<NgKeypart>> PutNgKeypart(int id, NgKeypart ngKeypart)
{
ResultModel<NgKeypart> result = new ResultModel<NgKeypart>();
if (id != ngKeypart.KeypartID)
{
result.Success = false;
result.Msg = "不良組件ID錯誤";
return result;
}
_context.Entry(ngKeypart).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NgKeypartExists(id))
{
result.Success = false;
result.Msg = "不良組件ID不存在";
return result;
}
else
{
throw;
}
}
result.Success = true;
result.Msg = "OK";
return result;
}
/// <summary>
///
/// </summary>
/// <param name="ngKeypart"></param>
/// <returns></returns>
// POST: api/NgKeyparts
// 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<NgKeypart>> PostNgKeypart(NgKeypart ngKeypart)
{
ResultModel<NgKeypart> result = new ResultModel<NgKeypart>();
Helper helper = new Helper(_context);
ngKeypart.KeypartID = helper.GetIDKey("KEYPART_ID").Result;
_context.NgKeyparts.Add(ngKeypart);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException ex)
{
if (NgKeypartExists(ngKeypart.KeypartID))
{
result.Success = false;
result.Msg = "不良組件ID重複";
return result;
}
else
{
result.Success = false;
result.Msg = ex.InnerException.Message;
return result;
}
}
result.Success = true;
result.Msg = ngKeypart.KeypartID.ToString();
return result;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/NgKeyparts/5
[HttpDelete("{id}")]
public async Task<ResultModel<NgKeypart>> DeleteNgKeypart(int id)
{
ResultModel<NgKeypart> result = new ResultModel<NgKeypart>();
var ngKeypart = await _context.NgKeyparts.FindAsync(id);
if (ngKeypart == null)
{
result.Success = false;
result.Msg = "不良組件ID不存在";
return result;
}
_context.NgKeyparts.Remove(ngKeypart);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
return result;
}
private bool NgKeypartExists(int id)
{
return _context.NgKeyparts.Any(e => e.KeypartID == id);
}
}
}