2011-02-13 09:55:01 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-02-14 11:01:31 +00:00
|
|
|
|
using System.Linq;
|
2011-02-14 19:14:13 +00:00
|
|
|
|
using JetBrains.Annotations;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
using VideoGameQuotes.Api;
|
2011-02-16 10:11:55 +00:00
|
|
|
|
using VideoGameQuotes.Web.Models;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Services {
|
|
|
|
|
public interface IQuoteService {
|
|
|
|
|
Game GetGame(int id);
|
|
|
|
|
IEnumerable<Game> GetAllGames();
|
|
|
|
|
IEnumerable<GamingSystem> GetAllSystems();
|
|
|
|
|
IEnumerable<Publisher> GetAllPublishers();
|
2011-02-13 09:55:01 +00:00
|
|
|
|
IEnumerable<Category> GetAllCategories();
|
2011-02-12 23:53:43 +00:00
|
|
|
|
Quote SaveQuote(Quote quote);
|
|
|
|
|
Quote GetQuote(int id);
|
|
|
|
|
Publisher GetPublisher(int id);
|
|
|
|
|
GamingSystem GetSystem(int systemId);
|
2011-02-13 09:55:01 +00:00
|
|
|
|
Category GetCategory(int categoryId);
|
2011-02-27 01:14:56 +00:00
|
|
|
|
IEnumerable<Quote> GetRecentQuotes(int start, int end, out int totalCount);
|
2011-02-14 19:14:13 +00:00
|
|
|
|
[CanBeNull]
|
|
|
|
|
Quote GetRandomQuote();
|
2011-02-22 04:05:23 +00:00
|
|
|
|
IEnumerable<Quote> GetBestQuotes(int start, int end, out int totalCount);
|
2011-02-15 00:14:24 +00:00
|
|
|
|
Vote GetVoteOrCreateNew(Quote quote, User voter);
|
2011-02-27 01:14:56 +00:00
|
|
|
|
IEnumerable<Quote> GetBrowsableQuotes(BrowseModel model, int start, int end, out int totalCount);
|
2011-02-28 00:38:47 +00:00
|
|
|
|
Quote GetQuoteForDayOfYear(int day);
|
2011-03-04 08:20:20 +00:00
|
|
|
|
DefaultBrowseModel GetDefaultBrowseModel();
|
|
|
|
|
void Delete(int id);
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class QuoteService : IQuoteService {
|
|
|
|
|
private readonly IRepository<Quote> quoteRepository;
|
|
|
|
|
private readonly IRepository<Game> gameRepository;
|
|
|
|
|
private readonly IRepository<GamingSystem> systemRepository;
|
|
|
|
|
private readonly IRepository<Publisher> publisherRepository;
|
2011-02-13 09:55:01 +00:00
|
|
|
|
private readonly IRepository<Category> categoryRepository;
|
2011-02-15 00:14:24 +00:00
|
|
|
|
private readonly IRepository<Vote> voteRepository;
|
2011-02-23 09:19:32 +00:00
|
|
|
|
private static readonly Random random = new Random();
|
2011-02-28 00:38:47 +00:00
|
|
|
|
private static readonly IDictionary<int, Quote> quoteOfTheDayMap = new Dictionary<int, Quote>();
|
2011-02-12 23:53:43 +00:00
|
|
|
|
|
|
|
|
|
public QuoteService(
|
|
|
|
|
IRepository<Quote> quoteRepository,
|
|
|
|
|
IRepository<Game> gameRepository,
|
|
|
|
|
IRepository<GamingSystem> systemRepository,
|
2011-02-15 00:14:24 +00:00
|
|
|
|
IRepository<Publisher> publisherRepository,
|
|
|
|
|
IRepository<Category> categoryRepository,
|
|
|
|
|
IRepository<Vote> voteRepository
|
2011-02-13 09:55:01 +00:00
|
|
|
|
) {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
this.quoteRepository = quoteRepository;
|
2011-02-13 09:55:01 +00:00
|
|
|
|
this.categoryRepository = categoryRepository;
|
2011-02-15 00:14:24 +00:00
|
|
|
|
this.voteRepository = voteRepository;
|
2011-02-12 23:53:43 +00:00
|
|
|
|
this.gameRepository = gameRepository;
|
|
|
|
|
this.systemRepository = systemRepository;
|
|
|
|
|
this.publisherRepository = publisherRepository;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-04 08:20:20 +00:00
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public DefaultBrowseModel GetDefaultBrowseModel() {
|
|
|
|
|
return new DefaultBrowseModel {
|
|
|
|
|
TotalNumberOfCategories = categoryRepository.Records.Count(),
|
|
|
|
|
TotalNumberOfSystems = systemRepository.Records.Count(),
|
|
|
|
|
TotalNumberOfPublishers = publisherRepository.Records.Count(),
|
|
|
|
|
TotalNumberOfGames = gameRepository.Records.Count(),
|
|
|
|
|
TotalNumberOfQuotes = quoteRepository.Records.Count()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public void Delete(int id) {
|
|
|
|
|
quoteRepository.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Game GetGame(int id) {
|
|
|
|
|
return gameRepository.FindById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<Game> GetAllGames() {
|
|
|
|
|
return gameRepository.Records;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
2011-02-13 09:55:01 +00:00
|
|
|
|
public IEnumerable<GamingSystem> GetAllSystems() {
|
2011-02-12 23:53:43 +00:00
|
|
|
|
return systemRepository.Records;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<Publisher> GetAllPublishers() {
|
|
|
|
|
return publisherRepository.Records;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-13 09:55:01 +00:00
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public IEnumerable<Category> GetAllCategories() {
|
|
|
|
|
return categoryRepository.Records;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-12 23:53:43 +00:00
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Quote SaveQuote(Quote quote) {
|
|
|
|
|
return quoteRepository.Save(quote);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Quote GetQuote(int id) {
|
|
|
|
|
return quoteRepository.FindById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Publisher GetPublisher(int id) {
|
|
|
|
|
return publisherRepository.FindById(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public GamingSystem GetSystem(int systemId) {
|
|
|
|
|
return systemRepository.FindById(systemId);
|
|
|
|
|
}
|
2011-02-13 09:55:01 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Category GetCategory(int categoryId) {
|
|
|
|
|
return categoryRepository.FindById(categoryId);
|
|
|
|
|
}
|
2011-02-14 02:50:33 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
2011-02-27 01:14:56 +00:00
|
|
|
|
public IEnumerable<Quote> GetRecentQuotes(int start, int end, out int totalCount) {
|
2011-02-24 22:45:29 +00:00
|
|
|
|
totalCount = quoteRepository.Records.Count();
|
2011-02-14 11:01:31 +00:00
|
|
|
|
return quoteRepository
|
|
|
|
|
.Records
|
|
|
|
|
.OrderByDescending(quote => quote.Created)
|
2011-02-24 22:45:29 +00:00
|
|
|
|
.Skip(start - 1)
|
|
|
|
|
.Take(end - start + 1);
|
2011-02-14 11:01:31 +00:00
|
|
|
|
}
|
2011-02-14 19:14:13 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Quote GetRandomQuote() {
|
2011-02-23 09:19:32 +00:00
|
|
|
|
var length = quoteRepository.Records.Count();
|
|
|
|
|
if (length == 0) {
|
2011-02-14 19:14:13 +00:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-28 00:38:47 +00:00
|
|
|
|
return GetRandomQuote(quoteRepository.Records);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static Quote GetRandomQuote(IEnumerable<Quote> quotes) {
|
|
|
|
|
var length = quotes.Count();
|
|
|
|
|
if (length == 0) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return quotes
|
2011-02-23 09:19:32 +00:00
|
|
|
|
.Skip(random.Next(length))
|
|
|
|
|
.Take(1)
|
|
|
|
|
.Single();
|
2011-02-14 19:14:13 +00:00
|
|
|
|
}
|
2011-02-14 19:48:36 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
2011-02-22 04:05:23 +00:00
|
|
|
|
public IEnumerable<Quote> GetBestQuotes(int start, int end, out int totalCount) {
|
2011-02-24 22:45:29 +00:00
|
|
|
|
totalCount = quoteRepository.Records.Count();
|
|
|
|
|
|
|
|
|
|
return quoteRepository.Records
|
2011-02-16 02:48:58 +00:00
|
|
|
|
.OrderByDescending(quote => quote.Score)
|
2011-02-15 00:14:24 +00:00
|
|
|
|
.ThenByDescending(quote => quote.UpVotes)
|
2011-02-22 04:05:23 +00:00
|
|
|
|
.Skip(start - 1)
|
2011-02-14 19:48:36 +00:00
|
|
|
|
.Take(end - start + 1);
|
|
|
|
|
}
|
2011-02-15 00:14:24 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Vote GetVoteOrCreateNew(Quote quote, User voter) {
|
|
|
|
|
var vote = voteRepository.Records.SingleOrDefault(v => v.Quote == quote && v.Voter == voter);
|
|
|
|
|
return vote ?? new Vote {
|
|
|
|
|
Quote = quote,
|
|
|
|
|
Voter = voter
|
|
|
|
|
};
|
|
|
|
|
}
|
2011-02-16 10:11:55 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
2011-02-27 01:14:56 +00:00
|
|
|
|
public IEnumerable<Quote> GetBrowsableQuotes(BrowseModel model, int start, int end, out int totalCount) {
|
2011-02-16 10:11:55 +00:00
|
|
|
|
var quotes = quoteRepository.Records;
|
|
|
|
|
if (model.GameIds.Any()) {
|
|
|
|
|
quotes = quotes.Where(quote => model.GameIds.Contains(quote.Game.Id));
|
|
|
|
|
}
|
|
|
|
|
if (model.PublisherIds.Any()) {
|
|
|
|
|
quotes = quotes.Where(quote => quote.Game.Publishers.Any(publisher => model.PublisherIds.Contains(publisher.Id)));
|
|
|
|
|
}
|
|
|
|
|
if (model.SystemIds.Any()) {
|
|
|
|
|
quotes = quotes.Where(quote => quote.Game.Systems.Any(system => model.SystemIds.Contains(system.Id)));
|
|
|
|
|
}
|
|
|
|
|
if (model.CategoryIds.Any()) {
|
|
|
|
|
quotes = quotes.Where(quote => quote.Categories.Any(category => model.CategoryIds.Contains(category.Id)));
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-27 01:14:56 +00:00
|
|
|
|
totalCount = quotes.Count();
|
2011-02-28 06:17:49 +00:00
|
|
|
|
return quotes.OrderByDescending(quote => quote.Score).ThenBy(quote => quote.UpVotes).Skip(start - 1).Take(end - start + 1);
|
2011-02-16 10:11:55 +00:00
|
|
|
|
}
|
2011-02-28 00:38:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[UnitOfWork, CanBeNull]
|
|
|
|
|
public Quote GetQuoteForDayOfYear(int day) {
|
|
|
|
|
if (!quoteOfTheDayMap.ContainsKey(day)) {
|
|
|
|
|
quoteOfTheDayMap[day] = GetRandomQuote(quoteRepository.Records.Where(quote => quote.Score > 0));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return quoteOfTheDayMap[day];
|
|
|
|
|
}
|
2011-02-12 23:53:43 +00:00
|
|
|
|
}
|
|
|
|
|
}
|