using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using rockfishCore.Models;

namespace rockfishCore.Controllers
{
    [Produces("application/json")]
    [Route("api/LicenseTemplates")]
    [Authorize]
    public class LicenseTemplatesController : Controller
    {
        private readonly rockfishContext _context;

        public LicenseTemplatesController(rockfishContext context)
        {
            _context = context;
        }

        // GET: api/LicenseTemplates
        [HttpGet]
        public IEnumerable<LicenseTemplates> GetLicenseTemplates()
        {
            return _context.LicenseTemplates;
        }

        // GET: api/LicenseTemplates/5
        [HttpGet("{id}")]
        public async Task<IActionResult> GetLicenseTemplates([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var licenseTemplates = await _context.LicenseTemplates.SingleOrDefaultAsync(m => m.Id == id);

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

            return Ok(licenseTemplates);
        }

        // PUT: api/LicenseTemplates/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutLicenseTemplates([FromRoute] long id, [FromBody] LicenseTemplates licenseTemplates)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != licenseTemplates.Id)
            {
                return BadRequest();
            }

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

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

            return NoContent();
        }

        // POST: api/LicenseTemplates
        [HttpPost]
        public async Task<IActionResult> PostLicenseTemplates([FromBody] LicenseTemplates licenseTemplates)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            _context.LicenseTemplates.Add(licenseTemplates);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetLicenseTemplates", new { id = licenseTemplates.Id }, licenseTemplates);
        }

        // DELETE: api/LicenseTemplates/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteLicenseTemplates([FromRoute] long id)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var licenseTemplates = await _context.LicenseTemplates.SingleOrDefaultAsync(m => m.Id == id);
            if (licenseTemplates == null)
            {
                return NotFound();
            }

            _context.LicenseTemplates.Remove(licenseTemplates);
            await _context.SaveChangesAsync();

            return Ok(licenseTemplates);
        }

        private bool LicenseTemplatesExists(long id)
        {
            return _context.LicenseTemplates.Any(e => e.Id == id);
        }
    }
}