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.
 
 
 
 
 

120 lines
3.5 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 NgInfoController : ControllerBase
{
private readonly AMESContext _context;
public NgInfoController(AMESContext context)
{
_context = context;
}
// GET: api/NgInfoe
[HttpGet]
public async Task<ActionResult<IEnumerable<NgInfo>>> GetNgInfos()
{
return await _context.NgInfos.ToListAsync();
}
// GET: api/NgInfoe/5
[HttpGet("{id}")]
public async Task<ActionResult<NgInfo>> GetNgInfo(decimal id)
{
var ngInfo = await _context.NgInfos.FindAsync(id);
if (ngInfo == null)
{
return NotFound();
}
return ngInfo;
}
// PUT: api/NgInfoe/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]
public async Task<ResultModel<NgInfo>> PutNgInfo([FromBody] NgInfo ngInfo)
{
ResultModel<NgInfo> result = new ResultModel<NgInfo>();
_context.Attach(ngInfo);
// 指定更新某個欄位
_context.Entry(ngInfo).Property(p => p.CreateUserID).IsModified = true;
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.Message;
}
return result;
}
// POST: api/NgInfoe
// 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<NgInfo>> PostNgInfo([FromBody] NgInfo ngInfo)
{
ResultModel<NgInfo> result = new ResultModel<NgInfo>();
Helper helper = new Helper(_context);
ngInfo.NgID = helper.GetIDKey("NG_ID").Result;
_context.NgInfos.Add(ngInfo);
try
{
await _context.SaveChangesAsync();
result.Success = true;
result.Msg = "OK";
}
catch (Exception ex)
{
result.Success = false;
result.Msg = ex.Message;
}
return result;
}
// DELETE: api/NgInfoe/5
[HttpDelete("{id}")]
public async Task<ActionResult<NgInfo>> DeleteNgInfo(decimal id)
{
var ngInfo = await _context.NgInfos.FindAsync(id);
if (ngInfo == null)
{
return NotFound();
}
_context.NgInfos.Remove(ngInfo);
await _context.SaveChangesAsync();
return ngInfo;
}
private bool NgInfoExists(decimal id)
{
return _context.NgInfos.Any(e => e.NgID == id);
}
}
}