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檢驗結果ID
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class FqcItemController : ControllerBase
    {
        private readonly AMESContext _context;

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

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

        // GET: api/FqcItem/5
        [HttpGet("{id}")]
        public async Task<ActionResult<FqcItem>> GetFqcItem(int id)
        {
            var fqcItem = await _context.FqcItems.FindAsync(id);

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

            return fqcItem;
        }

        // PUT: api/FqcItem/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> PutFqcItem(int id, FqcItem fqcItem)
        {
            if (id != fqcItem.FqcID)
            {
                return BadRequest();
            }

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

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

            return NoContent();
        }

        // POST: api/FqcItem
        // 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<FqcItem>> PostFqcItem(FqcItem fqcItem)
        {
            _context.FqcItems.Add(fqcItem);
            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (FqcItemExists(fqcItem.FqcID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtAction("GetFqcItem", new { id = fqcItem.FqcID }, fqcItem);
        }

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

            _context.FqcItems.Remove(fqcItem);
            await _context.SaveChangesAsync();

            return fqcItem;
        }

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