152 lines
4.1 KiB
C#
152 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using JetBrains.Annotations;
|
|
using Portoa.Persistence;
|
|
using VideoGameQuotes.Api;
|
|
|
|
namespace VideoGameQuotes.Web.Services {
|
|
public interface IQuoteService {
|
|
Game GetGame(int id);
|
|
IEnumerable<Game> GetAllGames();
|
|
IEnumerable<GamingSystem> GetAllSystems();
|
|
IEnumerable<Publisher> GetAllPublishers();
|
|
IEnumerable<Category> GetAllCategories();
|
|
Quote SaveQuote(Quote quote);
|
|
Quote GetQuote(int id);
|
|
Publisher GetPublisher(int id);
|
|
GamingSystem GetSystem(int systemId);
|
|
Category GetCategory(int categoryId);
|
|
Category SaveCategory(Category category);
|
|
IEnumerable<Quote> GetMostRecentQuotes(int limit);
|
|
[CanBeNull]
|
|
Quote GetRandomQuote();
|
|
IEnumerable<Quote> GetBestQuotes(int start, int end);
|
|
Vote SaveVote(Vote vote);
|
|
[NotNull]
|
|
Vote GetVoteOrCreateNew(Quote quote, User voter);
|
|
}
|
|
|
|
public class QuoteService : IQuoteService {
|
|
private readonly IRepository<Quote> quoteRepository;
|
|
private readonly IRepository<Game> gameRepository;
|
|
private readonly IRepository<GamingSystem> systemRepository;
|
|
private readonly IRepository<Publisher> publisherRepository;
|
|
private readonly IRepository<Category> categoryRepository;
|
|
private readonly IRepository<Vote> voteRepository;
|
|
|
|
public QuoteService(
|
|
IRepository<Quote> quoteRepository,
|
|
IRepository<Game> gameRepository,
|
|
IRepository<GamingSystem> systemRepository,
|
|
IRepository<Publisher> publisherRepository,
|
|
IRepository<Category> categoryRepository,
|
|
IRepository<Vote> voteRepository
|
|
) {
|
|
this.quoteRepository = quoteRepository;
|
|
this.categoryRepository = categoryRepository;
|
|
this.voteRepository = voteRepository;
|
|
this.gameRepository = gameRepository;
|
|
this.systemRepository = systemRepository;
|
|
this.publisherRepository = publisherRepository;
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public Game GetGame(int id) {
|
|
return gameRepository.FindById(id);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<Game> GetAllGames() {
|
|
return gameRepository.Records;
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<GamingSystem> GetAllSystems() {
|
|
return systemRepository.Records;
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<Publisher> GetAllPublishers() {
|
|
return publisherRepository.Records;
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<Category> GetAllCategories() {
|
|
return categoryRepository.Records;
|
|
}
|
|
|
|
[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);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public Category GetCategory(int categoryId) {
|
|
return categoryRepository.FindById(categoryId);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public Category SaveCategory(Category category) {
|
|
return categoryRepository.Save(category);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<Quote> GetMostRecentQuotes(int limit) {
|
|
return quoteRepository
|
|
.Records
|
|
.OrderByDescending(quote => quote.Created)
|
|
.Take(limit);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public Quote GetRandomQuote() {
|
|
var quotes = quoteRepository.Records.ToArray();
|
|
if (quotes.Length == 0) {
|
|
return null;
|
|
}
|
|
|
|
return quotes[new Random().Next(quotes.Length)];
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public IEnumerable<Quote> GetBestQuotes(int start, int end) {
|
|
var records = quoteRepository.Records.ToArray();
|
|
|
|
return records
|
|
.OrderByDescending(quote => quote.NetVotes)
|
|
.ThenByDescending(quote => quote.UpVotes)
|
|
.Skip(start)
|
|
.Take(end - start + 1);
|
|
}
|
|
|
|
[UnitOfWork]
|
|
public Vote SaveVote(Vote vote) {
|
|
return voteRepository.Save(vote);
|
|
}
|
|
|
|
[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
|
|
};
|
|
}
|
|
}
|
|
} |