Created
September 30, 2014 20:22
-
-
Save davidbreyer/6f8a56dad07acd90887d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EntityFramework.Auditing; | |
using SESPolicy.Api.Web.Components; | |
using SESPolicy.Common; | |
using SESPolicy.Model; | |
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Threading.Tasks; | |
using System.Web.Http; | |
namespace GenericWeb.Controllers | |
{ | |
/// <summary> | |
/// The Base API Controller handles the GET, PUT, POST, DELETE and Accessor injection for | |
/// the CRUD operations of a typical database entity. Some or all of these methods can be overridden if | |
/// need be. | |
/// </summary> | |
/// <typeparam name="T">Database Entity</typeparam> | |
/// <typeparam name="D">POCO object to return</typeparam> | |
public interface IBaseApiController<T, D> | |
where T : PersistentEntity | |
where D : class | |
{ | |
Task<IEnumerable<D>> Get(); | |
D Get(int id); | |
HttpResponseMessage Post([FromBody]D value); | |
HttpResponseMessage Put(int id, [FromBody]D value); | |
HttpResponseMessage Delete(int id); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <typeparam name="C"></typeparam> | |
/// <typeparam name="T"></typeparam> | |
/// <typeparam name="D"></typeparam> | |
public abstract class BaseApiController<C, T, D> : ApiController, IBaseApiController<T, D> | |
where T : PersistentEntity | |
where C : AuditDbContext, new() | |
where D : class | |
{ | |
/// <summary> | |
/// | |
/// </summary> | |
protected IGenericAccessor<C, T> Accessor { get; set; } | |
/// <summary> | |
/// | |
/// </summary> | |
/// <returns></returns> | |
public async virtual Task<IEnumerable<D>> Get() | |
{ | |
var returnValue = await Accessor.Repository.GetAllAsync(); | |
return AutoMapper.Mapper.Map<IEnumerable<T>, IEnumerable<D>>(returnValue.ToList()); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="id"></param> | |
/// <returns></returns> | |
public virtual D Get(int id) | |
{ | |
var entity = Accessor.Repository.FindBy(x => x.Id == id).SingleOrDefaultAsync().Result; | |
var entityPoco = AutoMapper.Mapper.Map<T, D>(entity); | |
return entityPoco; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="dto"></param> | |
/// <returns></returns> | |
public virtual HttpResponseMessage Post([FromBody]D dto) | |
{ | |
//Validate Request | |
if (dto == null) | |
{ | |
ModelState.AddModelError("error", localization.ApiDtoNullError); | |
} | |
HttpResponseMessage response; | |
try | |
{ | |
if (ModelState.IsValid) | |
{ | |
var entity = AutoMapper.Mapper.Map<D, T>(dto); | |
Accessor.Repository.Add(entity); | |
Accessor.Commit(); | |
var returnItem = AutoMapper.Mapper.Map<T, D>(entity); | |
response = Request.CreateResponse(HttpStatusCode.OK, returnItem); | |
} | |
else | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); | |
} | |
} | |
catch (Exception ex) | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.GetBaseException().Message); | |
} | |
return response; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="id"></param> | |
/// <param name="dto"></param> | |
/// <returns></returns> | |
public virtual HttpResponseMessage Put(int id, [FromBody]D dto) | |
{ | |
//Validate request | |
if (dto == null) | |
{ | |
ModelState.AddModelError("error", localization.ApiDtoNullError); | |
} | |
HttpResponseMessage response; | |
try | |
{ | |
if (ModelState.IsValid) | |
{ | |
var entity = AutoMapper.Mapper.Map<D, T>(dto); | |
entity.Id = id; | |
if (entity.Id == default(int)) | |
{ | |
Accessor.Repository.Add(entity); | |
} else { | |
Accessor.Repository.Edit(entity); | |
} | |
Accessor.Commit(); | |
var returnItem = AutoMapper.Mapper.Map<T, D>(entity); | |
response = Request.CreateResponse(HttpStatusCode.OK, returnItem); | |
} | |
else | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); | |
} | |
} | |
catch (Exception ex) | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.GetBaseException().Message); | |
} | |
return response; | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="id"></param> | |
/// <returns></returns> | |
public virtual HttpResponseMessage Delete(int id) | |
{ | |
//Validate request | |
if (id == default(int)) | |
{ | |
ModelState.AddModelError("error", localization.ApiNoIdSpecified); | |
} | |
HttpResponseMessage response; | |
try | |
{ | |
var itemToDelete = Accessor.Repository.FindBy(x => x.Id == id).SingleOrDefaultAsync().Result; | |
if (itemToDelete == null) { ModelState.AddModelError("error", string.Format(localization.ApiNoIdFoundToDelete, typeof(T).Name, id)); } | |
if (ModelState.IsValid) | |
{ | |
Accessor.Repository.Delete(itemToDelete); | |
Accessor.Commit(); | |
return new HttpResponseMessage(HttpStatusCode.OK); | |
} | |
else | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); | |
} | |
} | |
catch (Exception ex) | |
{ | |
response = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.GetBaseException().Message); | |
} | |
return response; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment