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/RfCaseProject")]
    [Authorize]
    public class RfCaseProjectController : Controller
    {
        private readonly rockfishContext _context;

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

        // GET: api/RfCaseProject
        [HttpGet]
        public IEnumerable<RfCaseProject> GetRfCaseProject()
        {
            var c = from s in _context.RfCaseProject select s;
            c = c.OrderBy(s => s.Name);
            return c;   
            //return _context.RfCaseProject;
        }

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

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

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

            return Ok(RfCaseProject);
        }

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

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

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

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

            return NoContent();
        }

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

            _context.RfCaseProject.Add(RfCaseProject);
            await _context.SaveChangesAsync();

            return CreatedAtAction("GetRfCaseProject", new { id = RfCaseProject.Id }, RfCaseProject);
        }

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

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

            _context.RfCaseProject.Remove(RfCaseProject);
            await _context.SaveChangesAsync();

            return Ok(RfCaseProject);
        }

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