Converts Tags and Genres as HashSets, to enforce non-duplicate values

This commit is contained in:
BinaryAlley committed 2024-08-28 21:48:20 +03:00
1 parent e10ca26ae4
commit 182c8aadb5
3 files changed
+30 -31

No files matched your search

@@ -25,8 +25,8 @@ public class BookDto : IStorageEntity
public string? OriginalLanguageCode { get; init; }
public string? OriginalLanguageName { get; init; }
public string? OriginalLanguageNativeName { get; init; }
public List<TagDto> Tags { get; init; } = [];
public List<GenreDto> Genres { get; init; } = [];
public HashSet<TagDto> Tags { get; set; } = [];
public HashSet<GenreDto> Genres { get; set; } = [];
public string? Publisher { get; init; }
public int? PageCount { get; init; }
public string Format { get; init; } = null!;
@@ -1,7 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Models.Books;
using Lumina.Application.Common.Models.Common;
using Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
#endregion
@@ -9,13 +8,13 @@ using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Lumina.DataAccess.Common.Configuration;
/// <summary>
/// Configures the entity mapping for the <see cref="BookDto"/> aggregate root.
/// Configures the entity mapping for the <see cref="BookDto"/> entity.
/// </summary>
public class BookConfiguration : IEntityTypeConfiguration<BookDto>
{
#region ===================================================================== METHODS ===================================================================================
/// <summary>
/// Configures the <see cref="Book"/> entity.
/// Configures the <see cref="BookDto"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<BookDto> builder)
@@ -41,7 +40,8 @@ public class BookConfiguration : IEntityTypeConfiguration<BookDto>
builder.Property(book => book.OriginalLanguageCode).HasColumnOrder(13);
builder.Property(book => book.OriginalLanguageName).HasColumnOrder(14);
builder.Property(book => book.OriginalLanguageNativeName).HasColumnOrder(15);
// since Tag is a Domain ValueObject (no identity), we need to configure it as a many-to-many relationship
// since Tag is a Domain ValueObject (no identity), but we also don't want to have duplicates,
// we need to configure it as a many-to-many relationship, where the tag itself is the primary key
builder.HasMany(book => book.Tags)
.WithMany()
.UsingEntity<Dictionary<string, object>>(
@@ -112,18 +112,18 @@ public class BookConfiguration : IEntityTypeConfiguration<BookDto>
ratingBuilder.Property<int>("Id").ValueGeneratedOnAdd();
ratingBuilder.HasKey("Id");
ratingBuilder.Property(r => r.Value)
ratingBuilder.Property(rating => rating.Value)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(r => r.MaxValue)
ratingBuilder.Property(rating => rating.MaxValue)
.HasColumnType("decimal(3,2)")
.IsRequired();
ratingBuilder.Property(r => r.VoteCount)
ratingBuilder.Property(rating => rating.VoteCount)
.IsRequired(false);
ratingBuilder.Property(r => r.Source)
ratingBuilder.Property(rating => rating.Source)
.HasConversion<string>()
.HasMaxLength(50)
.IsRequired();
@@ -150,26 +150,6 @@ public class BookConfiguration : IEntityTypeConfiguration<BookDto>
#endregion
}
public class TagConfiguration : IEntityTypeConfiguration<TagDto>
{
public void Configure(EntityTypeBuilder<TagDto> builder)
{
builder.ToTable("Tags");
builder.HasKey(t => t.Name);
builder.Property(t => t.Name).IsRequired().HasMaxLength(50);
}
}
public class GenreConfiguration : IEntityTypeConfiguration<GenreDto>
{
public void Configure(EntityTypeBuilder<GenreDto> builder)
{
builder.ToTable("Genres");
builder.HasKey(g => g.Name);
builder.Property(g => g.Name).IsRequired().HasMaxLength(50);
}
}
//public class ContributorConfiguration : IEntityTypeConfiguration<ContributorIdDto>
//{
// public void Configure(EntityTypeBuilder<ContributorIdDto> builder)
@@ -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
}