179 lines
5.4 KiB
179 lines
5.4 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;
|
|
|
|
namespace AMESCoreStudio.WebApi.Controllers.AMES
|
|
{
|
|
/// <summary>
|
|
/// 工廠资料维护
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class BarcodeInfoesController : ControllerBase
|
|
{
|
|
private readonly AMESContext _context;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public BarcodeInfoesController(AMESContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取全部條碼资料
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
// GET: api/BarcodeInfoes
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<BarcodeInfo>>> GetBarcodeInfoes()
|
|
{
|
|
IQueryable<BarcodeInfo> q = _context.BarcodeInfoes;
|
|
q = q.OrderBy(p => p.BarcodeID);
|
|
|
|
var BarcodeInfoes = await q.ToListAsync();
|
|
return BarcodeInfoes;
|
|
//return await _context.BarcodeInfoes.ToListAsync();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用ID获取该條碼资料
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
// GET: api/BarcodeInfoes/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<IEnumerable<BarcodeInfo>>> GetBarcodeInfoes(int id)
|
|
{
|
|
|
|
IQueryable<BarcodeInfo> q = _context.BarcodeInfoes;
|
|
q = q.Where(p => p.BarcodeID.Equals(id));
|
|
var barcodeInfo = await q.ToListAsync();
|
|
|
|
if (barcodeInfo == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return barcodeInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 用No获取该條碼资料
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
// GET: api/BarcodeInfoes/5
|
|
[HttpGet("No/{id}")]
|
|
public async Task<ActionResult<IEnumerable<BarcodeInfo>>> GetBarcodeInfoesByNo(string id)
|
|
{
|
|
|
|
IQueryable<BarcodeInfo> q = _context.BarcodeInfoes;
|
|
q = q.Where(p => p.BarcodeNo.Equals(id));
|
|
var barcodeInfo = await q.ToListAsync();
|
|
|
|
if (barcodeInfo == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return barcodeInfo;
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// 更新條碼资料
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <param name="barcodeInfo"></param>
|
|
/// <returns></returns>
|
|
// PUT: api/BarcodeInfoes/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<ActionResult<BarcodeInfo>> PutBarcodeInfoes(int id, [FromBody] BarcodeInfo barcodeInfo)
|
|
{
|
|
if (id != barcodeInfo.BarcodeID)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
_context.Entry(barcodeInfo).State = EntityState.Modified;
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (DbUpdateConcurrencyException)
|
|
{
|
|
if (!BarcodeInfoesExists(id))
|
|
{
|
|
return NotFound();
|
|
}
|
|
else
|
|
{
|
|
throw;
|
|
}
|
|
}
|
|
|
|
return barcodeInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增條碼资料
|
|
/// </summary>
|
|
/// <param name="barcodeInfo"></param>
|
|
/// <returns></returns>
|
|
// POST: api/BarcodeInfoes
|
|
// 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<ActionResult<BarcodeInfo>> PostBarcodeInfoes(BarcodeInfo barcodeInfo)
|
|
{
|
|
Helper helper = new Helper(_context);
|
|
barcodeInfo.BarcodeID = helper.GetIDKey("BARCODE_ID").Result;
|
|
|
|
_context.BarcodeInfoes.Add(barcodeInfo);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return CreatedAtAction("GetBarcodeInfoes", new { id = barcodeInfo.BarcodeID }, barcodeInfo);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除條碼资料
|
|
/// </summary>
|
|
/// <param name="id"></param>
|
|
/// <returns></returns>
|
|
// DELETE: api/BarcodeInfoes/5
|
|
[HttpDelete("{id}")]
|
|
public async Task<ActionResult<BarcodeInfo>> DeleteBarcodeInfoes(int id)
|
|
{
|
|
//var repairResponsibleUnits = await _context.BarcodeInfoes.FindAsync(id);
|
|
var barcodeInfo = await _context.BarcodeInfoes.Where(m => m.BarcodeID == id).FirstOrDefaultAsync();
|
|
if (barcodeInfo == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
_context.BarcodeInfoes.Remove(barcodeInfo);
|
|
await _context.SaveChangesAsync();
|
|
|
|
return barcodeInfo;
|
|
}
|
|
|
|
private bool BarcodeInfoesExists(int id)
|
|
{
|
|
return _context.BarcodeInfoes.Any(e => e.BarcodeID == id);
|
|
}
|
|
}
|
|
}
|
|
|