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>
    /// FQC條碼資料檔
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class FqcBarcodeController : ControllerBase
    {
        private readonly AMESContext _context;

        public FqcBarcodeController(AMESContext context)
        {
            _context = context;
        }

        // GET: api/FqcBarcode
        [HttpGet]
        public async Task<ActionResult<IEnumerable<FqcBarcode>>> GetFqcBarcodes()
        {
            return await _context.FqcBarcodes.ToListAsync();
        }

        // GET: api/FqcBarcode/5
        [HttpGet("{id}")]
        public async Task<ActionResult<FqcBarcode>> GetFqcBarcode(int id)
        {
            var fqcBarcode = await _context.FqcBarcodes.FindAsync(id);

            if (fqcBarcode == null)
            {
                return NotFound();
            }

            return fqcBarcode;
        }

        // PUT: api/FqcBarcode/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<IActionResult> PutFqcBarcode(int id, FqcBarcode fqcBarcode)
        {
            if (id != fqcBarcode.FqcID)
            {
                return BadRequest();
            }

            _context.Entry(fqcBarcode).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!FqcBarcodeExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return NoContent();
        }

        // POST: api/FqcBarcode
        // 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<FqcBarcode>> PostFqcBarcode(FqcBarcode fqcBarcode)
        {
            _context.FqcBarcodes.Add(fqcBarcode);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (FqcBarcodeExists(fqcBarcode.FqcID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetFqcBarcode", new { id = fqcBarcode.FqcID }, fqcBarcode);
        }

        // DELETE: api/FqcBarcode/5
        [HttpDelete("{id}")]
        public async Task<ActionResult<FqcBarcode>> DeleteFqcBarcode(int id)
        {
            var fqcBarcode = await _context.FqcBarcodes.FindAsync(id);
            if (fqcBarcode == null)
            {
                return NotFound();
            }

            _context.FqcBarcodes.Remove(fqcBarcode);
            await _context.SaveChangesAsync();

            return fqcBarcode;
        }

        private bool FqcBarcodeExists(int id)
        {
            return _context.FqcBarcodes.Any(e => e.FqcID == id);
        }
    }
}