mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 05:11:10 +00:00
Converts Tags and Genres as HashSets, to enforce non-duplicate values
This commit is contained in:
1 parent
e10ca26ae4
commit
182c8aadb5
3 files changed
+30
-31
No files matched your search
@@ -2,6 +2,7 @@
|
||||
using ErrorOr;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.Models.Books;
|
||||
using Lumina.Application.Common.Models.Common;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.Domain.Common.Errors;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -41,6 +42,20 @@ internal sealed class BookRepository : IBookRepository
|
||||
var jobExists = await _luminaDbContext.Books.AnyAsync(_book => _book.Id == book.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
|
||||
if (jobExists)
|
||||
return Errors.WrittenContent.BookAlreadyExists;
|
||||
|
||||
// fetch existing tags and genres
|
||||
var existingTags = await _luminaDbContext.Set<TagDto>()
|
||||
.Where(t => book.Tags.Select(bt => bt.Name).Contains(t.Name))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
var existingGenres = await _luminaDbContext.Set<GenreDto>()
|
||||
.Where(g => book.Genres.Select(bg => bg.Name).Contains(g.Name))
|
||||
.ToListAsync(cancellationToken);
|
||||
|
||||
// replace tags and genres in the book with existing ones
|
||||
book.Tags = new HashSet<TagDto>(book.Tags.Select(tag => existingTags.FirstOrDefault(existingTag => existingTag.Name == tag.Name) ?? tag));
|
||||
book.Genres = new HashSet<GenreDto>(book.Genres.Select(genre => existingGenres.FirstOrDefault(existingGenre => existingGenre.Name == genre.Name) ?? genre));
|
||||
|
||||
_luminaDbContext.Books.Add(book);
|
||||
return Result.Created;
|
||||
}
|
||||
@@ -52,7 +67,11 @@ internal sealed class BookRepository : IBookRepository
|
||||
/// <returns>An <see cref="ErrorOr{T}"/> containing either a collection of <see cref="BookDto"/>, or an error.</returns>
|
||||
public async Task<ErrorOr<IEnumerable<BookDto>>> GetAllAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
return await _luminaDbContext.Books.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
return await _luminaDbContext.Books
|
||||
.Include(book => book.Tags)
|
||||
.Include(book => book.Genres)
|
||||
.Include(book => book.ISBNs)
|
||||
.ToListAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
Reference in new issue
Block a user