From 182c8aadb530bec24dd8d309901bae8244edd7e8 Mon Sep 17 00:00:00 2001 From: Binary Alley Date: Wed, 28 Aug 2024 21:48:20 +0300 Subject: [PATCH] Converts Tags and Genres as HashSets, to enforce non-duplicate values --- .../Common/Models/Books/BookDto.cs | 4 +-- .../Common/Configuration/BookConfiguration.cs | 36 +++++-------------- .../Core/Repositories/Books/BookRepository.cs | 21 ++++++++++- 3 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/Lumina.Application/Common/Models/Books/BookDto.cs b/src/Lumina.Application/Common/Models/Books/BookDto.cs index 455701d1..d05c33fc 100644 --- a/src/Lumina.Application/Common/Models/Books/BookDto.cs +++ b/src/Lumina.Application/Common/Models/Books/BookDto.cs @@ -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 Tags { get; init; } = []; - public List Genres { get; init; } = []; + public HashSet Tags { get; set; } = []; + public HashSet Genres { get; set; } = []; public string? Publisher { get; init; } public int? PageCount { get; init; } public string Format { get; init; } = null!; diff --git a/src/Lumina.DataAccess/Common/Configuration/BookConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/BookConfiguration.cs index 58c56d9f..d905f636 100644 --- a/src/Lumina.DataAccess/Common/Configuration/BookConfiguration.cs +++ b/src/Lumina.DataAccess/Common/Configuration/BookConfiguration.cs @@ -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; /// -/// Configures the entity mapping for the aggregate root. +/// Configures the entity mapping for the entity. /// public class BookConfiguration : IEntityTypeConfiguration { #region ===================================================================== METHODS =================================================================================== /// - /// Configures the entity. + /// Configures the entity. /// /// The builder to be used to configure the entity. public void Configure(EntityTypeBuilder builder) @@ -41,7 +40,8 @@ public class BookConfiguration : IEntityTypeConfiguration 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>( @@ -112,18 +112,18 @@ public class BookConfiguration : IEntityTypeConfiguration ratingBuilder.Property("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() .HasMaxLength(50) .IsRequired(); @@ -150,26 +150,6 @@ public class BookConfiguration : IEntityTypeConfiguration #endregion } -public class TagConfiguration : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("Tags"); - builder.HasKey(t => t.Name); - builder.Property(t => t.Name).IsRequired().HasMaxLength(50); - } -} - -public class GenreConfiguration : IEntityTypeConfiguration -{ - public void Configure(EntityTypeBuilder builder) - { - builder.ToTable("Genres"); - builder.HasKey(g => g.Name); - builder.Property(g => g.Name).IsRequired().HasMaxLength(50); - } -} - //public class ContributorConfiguration : IEntityTypeConfiguration //{ // public void Configure(EntityTypeBuilder builder) diff --git a/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs b/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs index ca9ab07d..3d29ab6b 100644 --- a/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs +++ b/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs @@ -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() + .Where(t => book.Tags.Select(bt => bt.Name).Contains(t.Name)) + .ToListAsync(cancellationToken); + + var existingGenres = await _luminaDbContext.Set() + .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(book.Tags.Select(tag => existingTags.FirstOrDefault(existingTag => existingTag.Name == tag.Name) ?? tag)); + book.Genres = new HashSet(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 /// An containing either a collection of , or an error. public async Task>> 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 } \ No newline at end of file