using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using esoft_API.Models.Entities;

namespace esoft_API.Controllers
{
    public class ExecutorsController : ApiController
    {
        private esoftEntities db = new esoftEntities();

        // GET: api/Executors
        public IQueryable<Executor> GetExecutor()
        {
            return db.Executor;
        }

        // GET: api/Executors/5
        [ResponseType(typeof(Executor))]
        public IHttpActionResult GetExecutor(int id)
        {
            Executor executor = db.Executor.Find(id);
            if (executor == null)
            {
                return NotFound();
            }

            return Ok(executor);
        }

        // PUT: api/Executors/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutExecutor(int id, Executor executor)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != executor.ID)
            {
                return BadRequest();
            }

            db.Entry(executor).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ExecutorExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Executors
        [ResponseType(typeof(Executor))]
        public IHttpActionResult PostExecutor(Executor executor)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Executor.Add(executor);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateException)
            {
                if (ExecutorExists(executor.ID))
                {
                    return Conflict();
                }
                else
                {
                    throw;
                }
            }

            return CreatedAtRoute("DefaultApi", new { id = executor.ID }, executor);
        }

        // DELETE: api/Executors/5
        [ResponseType(typeof(Executor))]
        public IHttpActionResult DeleteExecutor(int id)
        {
            Executor executor = db.Executor.Find(id);
            if (executor == null)
            {
                return NotFound();
            }

            db.Executor.Remove(executor);
            db.SaveChanges();

            return Ok(executor);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool ExecutorExists(int id)
        {
            return db.Executor.Count(e => e.ID == id) > 0;
        }
    }
}