using System; using System.Collections.Generic; using System.Linq; using JetBrains.Annotations; using Portoa.Persistence; using VideoGameQuotes.Api; namespace VideoGameQuotes.Web.Services { public interface IPublisherService { [CanBeNull] Publisher FindByName(string name); Publisher Save(Publisher publisher); IEnumerable GetGamesForPublisher(Publisher publisher); Publisher FindById(int id); void Delete(int id); } public class PublisherService : IPublisherService { private readonly IRepository repository; private readonly IRepository gameRepository; public PublisherService(IRepository repository, IRepository gameRepository) { this.repository = repository; this.gameRepository = gameRepository; } [UnitOfWork] public Publisher FindByName(string name) { return repository.Records.FirstOrDefault(publisher => publisher.Name == name); } [UnitOfWork] public Publisher Save(Publisher publisher) { return repository.Save(publisher); } [UnitOfWork] public IEnumerable GetGamesForPublisher(Publisher publisher) { return gameRepository .Records .Where(game => game.Publishers.Contains(publisher)); } [UnitOfWork] public Publisher FindById(int id) { return repository.FindById(id); } [UnitOfWork] public void Delete(int id) { repository.Delete(id); } } }