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 WebApiKnockoutDemo.Models;

namespace WebApiKnockoutDemo.Controllers
{
    public class RatingController : ApiController
    {
        private JudgingEntities entities = new JudgingEntities();
        
        // Example using the default HTTP verb routing technique
        // Can also use GET and PUT

        // POST api/Rating
        [ResponseType(typeof(Rating))]
        public IHttpActionResult PostRating(Rating rating)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            entities.Ratings.Add(rating);
            entities.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = rating.Id }, rating);
        }       

        // DELETE api/Rating/5
        [ResponseType(typeof(Rating))]
        public IHttpActionResult DeleteRating(int id)
        {
            Rating rating = entities.Ratings.Find(id);
            if (rating == null)
            {
                return NotFound();
            }

            entities.Ratings.Remove(rating);
            entities.SaveChanges();

            return Ok(rating);
        }

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

       
    }
}