2011-02-24 01:23:53 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2011-02-22 09:39:59 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
using Portoa.Persistence;
|
|
|
|
|
using VideoGameQuotes.Api;
|
|
|
|
|
|
|
|
|
|
namespace VideoGameQuotes.Web.Services {
|
|
|
|
|
public interface IGameService {
|
|
|
|
|
[CanBeNull]
|
|
|
|
|
Game FindByNameAndSystems(string name, IEnumerable<int> systemIds);
|
|
|
|
|
Game Save(Game game);
|
2011-02-23 09:19:32 +00:00
|
|
|
|
Game FindById(int id);
|
2011-02-24 01:23:53 +00:00
|
|
|
|
void Delete(int id);
|
|
|
|
|
int GetQuotesForGame(Game game);
|
2011-02-22 09:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class GameService : IGameService {
|
|
|
|
|
private readonly IRepository<Game> repository;
|
2011-02-24 01:23:53 +00:00
|
|
|
|
private readonly IRepository<Quote> quoteRepository;
|
2011-02-22 09:39:59 +00:00
|
|
|
|
|
2011-02-24 01:23:53 +00:00
|
|
|
|
public GameService(IRepository<Game> repository, IRepository<Quote> quoteRepository) {
|
2011-02-22 09:39:59 +00:00
|
|
|
|
this.repository = repository;
|
2011-02-24 01:23:53 +00:00
|
|
|
|
this.quoteRepository = quoteRepository;
|
2011-02-22 09:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Game FindByNameAndSystems(string name, IEnumerable<int> systemIds) {
|
|
|
|
|
return repository
|
|
|
|
|
.Records
|
|
|
|
|
.FirstOrDefault(game => game.Name == name && game.Systems.Any(system => systemIds.Contains(system.Id)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Game Save(Game game) {
|
|
|
|
|
return repository.Save(game);
|
|
|
|
|
}
|
2011-02-23 09:19:32 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public Game FindById(int id) {
|
|
|
|
|
return repository.FindById(id);
|
|
|
|
|
}
|
2011-02-24 01:23:53 +00:00
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public void Delete(int id) {
|
|
|
|
|
repository.Delete(id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[UnitOfWork]
|
|
|
|
|
public int GetQuotesForGame(Game game) {
|
|
|
|
|
return quoteRepository
|
|
|
|
|
.Records
|
|
|
|
|
.Where(quote => quote.Game == game)
|
|
|
|
|
.Count();
|
|
|
|
|
}
|
2011-02-22 09:39:59 +00:00
|
|
|
|
}
|
|
|
|
|
}
|