53 lines
1.4 KiB
C#
53 lines
1.4 KiB
C#
|
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<Category> categoryRepository;
|
|||
|
private readonly IRepository<Quote> quoteRepository;
|
|||
|
|
|||
|
public CategoryService(IRepository<Category> categoryRepository, IRepository<Quote> 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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|