using System.Collections.Generic; 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 systemIds); Game Save(Game game); Game FindById(int id); void Delete(int id); int GetQuotesForGame(Game game); } public class GameService : IGameService { private readonly IRepository repository; private readonly IRepository quoteRepository; public GameService(IRepository repository, IRepository quoteRepository) { this.repository = repository; this.quoteRepository = quoteRepository; } [UnitOfWork] public Game FindByNameAndSystems(string name, IEnumerable 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); } [UnitOfWork] public Game FindById(int id) { return repository.FindById(id); } [UnitOfWork] public void Delete(int id) { repository.Delete(id); } [UnitOfWork] public int GetQuotesForGame(Game game) { return quoteRepository .Records .Where(quote => quote.Game == game) .Count(); } } }