MyBookmark/Modules.Library.Domain/Interactors/GenreInteractor.cs
2024-09-04 23:08:56 +03:00

35 lines
1.4 KiB
C#

using Modules.Library.Domain.Entities.Genre;
using Modules.Library.Domain.Exceptions.Genre;
using Modules.Library.Domain.Gateways;
namespace Modules.Library.Domain.Interactors;
public class GenreInteractor(IGenreGateway gateway)
{
public async Task<Guid> Create(string name)
{
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(name);
if (await gateway.GetFirstOrDefaultWhere(q => q.Name == name) != null) throw new GenreIsAlreadyExistException();
var newGenre = new Genre(name);
return await gateway.AddAsync(newGenre);
}
public async Task Edit(Guid genreId, string name)
{
if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(name);
var genre = await gateway.GetByIdAsync(genreId);
if (await gateway.GetFirstOrDefaultWhere(q => q.Name == name) != null) throw new GenreWithSameNameIsAlreadyExistException();
genre.SetName(name);
if (!await gateway.UpdateAsync(genre)) throw new Exception("Save unsuccessfull");
}
public async Task Delete(Guid genreId)
{
var genre = await gateway.GetByIdAsync(genreId);
//await gateway.DeleteAsync(genre);
if (genre.Deleted) throw new Exception("AlreadyDeleted");
genre.SetDeleted();
if (!await gateway.UpdateAsync(genre)) throw new Exception("Save unsuccessfull");
}
}