using MemoEngine.Models;
using MemoEngine.Repositories;
using System;
using System.Collections.Generic;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MemoEngine.Controllers
{
public class MaximServiceController : ApiController
{
MaximServiceRepository repo = new MaximServiceRepository();
// GET: api/MaximService
public IEnumerable<Maxim> Get()
{
return repo.GetMaxims().AsEnumerable();
}
// GET: api/MaximService/5
public Maxim Get(int id)
{
Maxim maxim = repo.GetMaximById(id);
if (maxim == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return maxim;
}
// POST: api/MaximService
public HttpResponseMessage Post([FromBody]Maxim maxim)
{
if (ModelState.IsValid)
{
repo.AddMaxim(maxim);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, maxim);
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = maxim.Id }));
return response;
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
// PUT: api/MaximService/5
public HttpResponseMessage Put(int id, [FromBody]Maxim maxim)
{
if (!ModelState.IsValid)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
if (id != maxim.Id)
{
return Request.CreateResponse(HttpStatusCode.BadRequest);
}
try
{
repo.UpdateMaxim(maxim);
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
// DELETE: api/MaximService/5
public HttpResponseMessage Delete(int id)
{
Maxim maxim = repo.GetMaximById(id);
if (maxim == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
try
{
repo.RemoveMaxim(id);
}
catch (DbUpdateConcurrencyException ex)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
}
return Request.CreateResponse(HttpStatusCode.OK, maxim);
}
}
}