using System.Linq; using JetBrains.Annotations; using Portoa.Persistence; using VideoGameQuotes.Api; namespace VideoGameQuotes.Web.Services { public interface ICategoryService { [CanBeNull] Category FindByName(string name); Category Save(Category category); Category FindById(int id); void Delete(int id); int GetQuotesForCategory(Category category); } public class CategoryService : ICategoryService { private readonly IRepository categoryRepository; private readonly IRepository quoteRepository; public CategoryService(IRepository categoryRepository, IRepository quoteRepository) { this.categoryRepository = categoryRepository; this.quoteRepository = quoteRepository; } [UnitOfWork] public Category FindByName(string name) { return categoryRepository.Records.FirstOrDefault(category => category.Name == name); } [UnitOfWork] public Category Save(Category category) { return categoryRepository.Save(category); } [UnitOfWork] public Category FindById(int id) { return categoryRepository.FindById(id); } [UnitOfWork] public void Delete(int id) { categoryRepository.Delete(id); } [UnitOfWork] public int GetQuotesForCategory(Category category) { return quoteRepository .Records .Where(quote => quote.Categories.Contains(category)) .Count(); } } }