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.
 
 
 
 
 

205 lines
5.8 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 NgRepairsController : ControllerBase
{
private readonly AMESContext _context;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
public NgRepairsController(AMESContext context)
{
_context = context;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
// GET: api/NgRepairs
[HttpGet]
public async Task<ActionResult<IEnumerable<NgRepair>>> GetNgRepair()
{
return await _context.NgRepairs.ToListAsync();
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgRepairs/5
[HttpGet("{id}")]
public async Task<ActionResult<IEnumerable<NgRepair>>> GetNgRepair(int id)
{
IQueryable<NgRepair> q = _context.NgRepairs;
q = q.Where(p => p.RepairID.Equals(id));
var ngRepair = await q.ToListAsync();
if (ngRepair == null)
{
return NotFound();
}
return ngRepair;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// GET: api/NgRepairs/5
[HttpGet("Component/{id}")]
public async Task<ActionResult<IEnumerable<NgRepair>>> GetNgRepairByComponent(int id)
{
IQueryable<NgRepair> q = _context.NgRepairs;
q = q.Where(p => p.ComponentID.Equals(id));
var ngRepair = await q.ToListAsync();
if (ngRepair == null)
{
return NotFound();
}
return ngRepair;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <param name="ngRepair"></param>
/// <returns></returns>
// PUT: api/NgRepairs/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<NgRepair>> PutNgRepair(int id, NgRepair ngRepair)
{
ResultModel<NgRepair> result = new ResultModel<NgRepair>();
if (id != ngRepair.RepairID)
{
result.Success = false;
result.Msg = "不良維修ID錯誤";
return result;
}
_context.Entry(ngRepair).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!NgRepairExists(id))
{
result.Success = false;
result.Msg = "不良維修ID不存在";
return result;
}
else
{
throw;
}
}
result.Success = true;
result.Msg = "OK";
return result;
}
/// <summary>
///
/// </summary>
/// <param name="ngRepair"></param>
/// <returns></returns>
// POST: api/NgRepairs
// 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<NgRepair>> PostNgRepair(NgRepair ngRepair)
{
ResultModel<NgRepair> result = new ResultModel<NgRepair>();
Helper helper = new Helper(_context);
ngRepair.RepairID = helper.GetIDKey("REPAIR_ID").Result;
_context.NgRepairs.Add(ngRepair);
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateException)
{
if (NgRepairExists(ngRepair.RepairID))
{
result.Success = false;
result.Msg = "不良維修ID重複";
return result;
}
else
{
throw;
}
}
result.Success = true;
result.Msg = "OK";
return result;
}
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
// DELETE: api/NgRepairs/5
[HttpDelete("{id}")]
public async Task<ResultModel<NgRepair>> DeleteNgRepair(decimal id)
{
ResultModel<NgRepair> result = new ResultModel<NgRepair>();
var ngRepair = await _context.NgRepairs.FindAsync(id);
if (ngRepair == null)
{
result.Success = false;
result.Msg = "不良維修ID不存在";
return result;
}
_context.NgRepairs.Remove(ngRepair);
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
return result;
}
private bool NgRepairExists(decimal id)
{
return _context.NgRepairs.Any(e => e.RepairID == id);
}
}
}