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 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"); } }