55 lines
1.4 KiB
C#
55 lines
1.4 KiB
C#
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<Game> GetGamesForPublisher(Publisher publisher);
|
|
Publisher FindById(int id);
|
|
void Delete(int id);
|
|
}
|
|
|
|
public class PublisherService : IPublisherService {
|
|
private readonly IRepository<Publisher> repository;
|
|
private readonly IRepository<Game> gameRepository;
|
|
|
|
public PublisherService(IRepository<Publisher> repository, IRepository<Game> 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<Game> 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);
|
|
}
|
|
}
|
|
} |