2011-02-16 02:48:11 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Web.Mvc;
|
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
using Portoa.Web.Controllers;
|
|
|
|
|
using Portoa.Web.Results;
|
2011-03-07 20:56:34 +00:00
|
|
|
|
using Portoa.Web.Util;
|
2011-02-16 02:48:11 +00:00
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
using VideoGameQuotes.Web.Models;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Controllers {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public interface IApiService {
|
|
|
|
|
IEnumerable<GameDto> GetGames(ApiModel model);
|
|
|
|
|
IEnumerable<SystemDto> GetSystems(ApiModel model);
|
|
|
|
|
IEnumerable<CategoryDto> GetCategories(ApiModel model);
|
|
|
|
|
IEnumerable<PublisherDto> GetPublishers(ApiModel model);
|
|
|
|
|
IEnumerable<QuoteDto> GetQuotes(ApiModel model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ApiService : IApiService {
|
|
|
|
|
private readonly IRepository<Game> gameRepository;
|
|
|
|
|
private readonly IRepository<GamingSystem> systemRepository;
|
|
|
|
|
private readonly IRepository<Category> categoryRepository;
|
|
|
|
|
private readonly IRepository<Publisher> publisherRepository;
|
|
|
|
|
private readonly IRepository<Quote> quoteRepository;
|
|
|
|
|
|
|
|
|
|
private readonly IDictionary<string, CriterionHandler<Category>> categoryHandlers = new Dictionary<string, CriterionHandler<Category>>();
|
|
|
|
|
private readonly IDictionary<string, CriterionHandler<GamingSystem>> systemHandlers = new Dictionary<string, CriterionHandler<GamingSystem>>();
|
|
|
|
|
private readonly IDictionary<string, CriterionHandler<Publisher>> publisherHandlers = new Dictionary<string, CriterionHandler<Publisher>>();
|
2011-02-16 10:11:55 +00:00
|
|
|
|
private readonly IDictionary<string, CriterionHandler<Quote>> quoteHandlers = new Dictionary<string, CriterionHandler<Quote>> {
|
|
|
|
|
{ "game", new Quote.GameCriterionHandler() }
|
|
|
|
|
};
|
2011-02-16 02:48:11 +00:00
|
|
|
|
private readonly IDictionary<string, CriterionHandler<Game>> gameHandlers = new Dictionary<string, CriterionHandler<Game>> {
|
|
|
|
|
{ "system", new Game.SystemCriterionHandler() },
|
|
|
|
|
{ "publisher", new Game.PublisherCriterionHandler() }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public ApiService(
|
|
|
|
|
IRepository<Game> gameRepository,
|
|
|
|
|
IRepository<GamingSystem> systemRepository,
|
|
|
|
|
IRepository<Category> categoryRepository,
|
|
|
|
|
IRepository<Publisher> publisherRepository,
|
|
|
|
|
IRepository<Quote> quoteRepository
|
|
|
|
|
) {
|
|
|
|
|
this.gameRepository = gameRepository;
|
|
|
|
|
this.systemRepository = systemRepository;
|
|
|
|
|
this.categoryRepository = categoryRepository;
|
|
|
|
|
this.publisherRepository = publisherRepository;
|
|
|
|
|
this.quoteRepository = quoteRepository;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-07 20:56:34 +00:00
|
|
|
|
private static IEnumerable<T> SetSortMethod<T, TKey>(IEnumerable<T> records, SortMethod sortMethod, SortOrder sortOrder, Func<T, TKey> propertySelector) where T : Entity<int> {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
switch (sortMethod) {
|
|
|
|
|
case SortMethod.Alphabetical:
|
|
|
|
|
return sortOrder == SortOrder.Descending ? records.OrderByDescending(propertySelector) : records.OrderBy(propertySelector);
|
|
|
|
|
case SortMethod.Date:
|
|
|
|
|
return sortOrder == SortOrder.Descending ? records.OrderByDescending(propertySelector) : records.OrderBy(propertySelector);
|
|
|
|
|
default:
|
|
|
|
|
return records;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<TDto> GetRecords<T, TDto>(
|
|
|
|
|
ApiModel model,
|
|
|
|
|
IRepository<T> repository,
|
|
|
|
|
Func<T, object> sortSelector,
|
|
|
|
|
IDictionary<string,
|
|
|
|
|
CriterionHandler<T>> criterionHandlers
|
|
|
|
|
)
|
2011-03-07 20:56:34 +00:00
|
|
|
|
where T : Entity<int>
|
2011-02-16 02:48:11 +00:00
|
|
|
|
where TDto : new() {
|
|
|
|
|
IEnumerable<T> records;
|
|
|
|
|
if (model.FetchAll) {
|
|
|
|
|
records = SetSortMethod(repository.Records, model.SortMethod, model.SortOrder, sortSelector);
|
|
|
|
|
var expressionBuilder = new List<Func<T, bool>>();
|
|
|
|
|
foreach (var kvp in model.Criteria) {
|
|
|
|
|
if (!criterionHandlers.ContainsKey(kvp.Key)) {
|
|
|
|
|
throw new ApiException(string.Format("Unknown criterion: \"{0}\"", kvp.Key));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
expressionBuilder.AddRange(criterionHandlers[kvp.Key].HandleCriterion(kvp.Value));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (expressionBuilder.Count > 0) {
|
|
|
|
|
records = records.Where(game => expressionBuilder.Aggregate(false, (current, next) => current || next(game)));
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
records = new[] { repository.FindById(model.Id) };
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-02 20:18:33 +00:00
|
|
|
|
return records.ToArray().Select(entity => entity.ToDto<TDto>());
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<GameDto> GetGames(ApiModel model) {
|
|
|
|
|
return GetRecords<Game, GameDto>(model, gameRepository, game => game.Name, gameHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<SystemDto> GetSystems(ApiModel model) {
|
|
|
|
|
return GetRecords<GamingSystem, SystemDto>(model, systemRepository, system => system.Name, systemHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<CategoryDto> GetCategories(ApiModel model) {
|
|
|
|
|
return GetRecords<Category, CategoryDto>(model, categoryRepository, category => category.Name, categoryHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<PublisherDto> GetPublishers(ApiModel model) {
|
|
|
|
|
return GetRecords<Publisher, PublisherDto>(model, publisherRepository, publisher => publisher.Name, publisherHandlers);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<QuoteDto> GetQuotes(ApiModel model) {
|
|
|
|
|
return GetRecords<Quote, QuoteDto>(model, quoteRepository, quote => quote.Id, quoteHandlers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class ApiController : Controller {
|
|
|
|
|
private readonly IApiService apiService;
|
|
|
|
|
|
|
|
|
|
public ApiController(IApiService apiService) {
|
|
|
|
|
this.apiService = apiService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ActionResult Error(HttpStatusCode statusCode, string message = null) {
|
2011-03-03 23:41:42 +00:00
|
|
|
|
return new StatusOverrideResult(this.SerializeToJson(this.CreateJsonErrorResponse(message ?? "Invalid request"))) { StatusCode = statusCode };
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 06:17:49 +00:00
|
|
|
|
private ActionResult GetRecords<TDto>(Func<IApiService, IEnumerable<TDto>> recordGetter) {
|
2011-02-16 02:48:11 +00:00
|
|
|
|
if (!ModelState.IsValid) {
|
|
|
|
|
return Error(HttpStatusCode.BadRequest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2011-03-03 23:41:42 +00:00
|
|
|
|
return this.SerializeToJson(this.CreateJsonResponse(data: new { records = recordGetter(apiService) }));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
} catch (ApiException e) {
|
|
|
|
|
return Error(HttpStatusCode.BadRequest, e.Message);
|
|
|
|
|
} catch {
|
|
|
|
|
return Error(HttpStatusCode.InternalServerError, "An error occurred while trying to fulfill your stupid request");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Game(ApiModel model) {
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return GetRecords(service => service.GetGames(model));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult System(ApiModel model) {
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return GetRecords(service => service.GetSystems(model));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Category(ApiModel model) {
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return GetRecords(service => service.GetCategories(model));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Publisher(ApiModel model) {
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return GetRecords(service => service.GetPublishers(model));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ActionResult Quote(ApiModel model) {
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return GetRecords(service => service.GetQuotes(model));
|
2011-02-16 02:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|