diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/Books/IBookRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/Books/IBookRepository.cs index ca22fcc6..ae4f6297 100644 --- a/src/Lumina.Application/Common/DataAccess/Repositories/Books/IBookRepository.cs +++ b/src/Lumina.Application/Common/DataAccess/Repositories/Books/IBookRepository.cs @@ -1,8 +1,7 @@ -#region ========================================================================= USING ===================================================================================== +#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.DataAccess.Repositories.Common.Actions; using Lumina.Application.Common.DataAccess.Repositories.Common.Base; using Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary; - #endregion namespace Lumina.Application.Common.DataAccess.Repositories.Books; @@ -14,4 +13,4 @@ public interface IBookRepository : IRepository, IInsertRepositoryAction, IGetAllRepositoryAction { -} \ No newline at end of file +} diff --git a/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs b/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs new file mode 100644 index 00000000..d9317228 --- /dev/null +++ b/src/Lumina.Application/Common/DataAccess/Repositories/Users/IUserRepository.cs @@ -0,0 +1,15 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Application.Common.DataAccess.Repositories.Common.Actions; +using Lumina.Application.Common.DataAccess.Repositories.Common.Base; +using Lumina.Contracts.Entities.UsersManagement; +#endregion + +namespace Lumina.Application.Common.DataAccess.Repositories.Users; + +/// +/// Interface for the repository for users. +/// +public interface IUserRepository : IRepository, + IInsertRepositoryAction +{ +} diff --git a/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserMapping.cs b/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserMapping.cs new file mode 100644 index 00000000..132d0f2b --- /dev/null +++ b/src/Lumina.Application/Common/Mapping/UsersManagement/Users/UserMapping.cs @@ -0,0 +1,29 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Contracts.Entities.UsersManagement; +using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate; +#endregion + +namespace Lumina.Application.Common.Mapping.UsersManagement.Users; + +/// +/// Extension methods for converting . +/// +public static class UserMapping +{ + /// + /// Converts to . + /// + /// The domain model to be converted. + /// The repository entity containing sensitive information not mapped to the Domain model. + /// + /// Because Authentication and Authorization are Application Layer concerns, the members involved in those concerns are not mirrored in the Domain entity. + /// However, since Entity Framework updates an entire entity, when mapping from the Domain entity to the DAL entity, one still needs to provide the + /// values of the non-included members, such that they are not overriden with . Those values should come in the requested . + /// + /// The converted repository entity. + public static UserEntity ToRepositoryEntity(this User domainModel, UserEntity repositoryEntity) + { + repositoryEntity.Username = domainModel.Username; + return repositoryEntity; + } +} diff --git a/src/Lumina.Contracts/Entities/UsersManagement/UserEntity.cs b/src/Lumina.Contracts/Entities/UsersManagement/UserEntity.cs new file mode 100644 index 00000000..df297151 --- /dev/null +++ b/src/Lumina.Contracts/Entities/UsersManagement/UserEntity.cs @@ -0,0 +1,54 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Contracts.Entities.Common; +using System; +using System.Diagnostics; +#endregion + +namespace Lumina.Contracts.Entities.UsersManagement; + +/// +/// Repository entity for a user. +/// +[DebuggerDisplay("Username: {Username}")] +public class UserEntity : IStorageEntity +{ + /// + /// Gets the Id of the user. + /// + public required Guid Id { get; init; } + + /// + /// Gets or sets the username of the user. + /// + public required string Username { get; set; } + + /// + /// Gets or sets the hashed password of the user. + /// + public required string Password { get; set; } + + /// + /// Gets or sets the optional Time-based One-Time Password (TOTP) secret for two-factor authentication. + /// + public string? TotpSecret { get; set; } + + /// + /// Gets or sets the optional token used for account verification or password reset. + /// + public string? VerificationToken { get; set; } + + /// + /// Gets or sets the optional date and time when the verification token was created. + /// + public DateTime? VerificationTokenCreated { get; set; } + + /// + /// Gets the time and date when the entity was added. + /// + public required DateTime Created { get; set; } + + /// + /// Gets the optional time and date when the entity was updated. + /// + public DateTime? Updated { get; set; } +} diff --git a/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs b/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs new file mode 100644 index 00000000..622fd3c4 --- /dev/null +++ b/src/Lumina.DataAccess/Common/Configuration/UserConfiguration.cs @@ -0,0 +1,44 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Contracts.Entities.UsersManagement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +#endregion + +namespace Lumina.DataAccess.Common.Configuration; + +/// +/// Configures the entity mapping for the entity. +/// +public class UserConfiguration : IEntityTypeConfiguration +{ + /// + /// Configures the entity. + /// + /// The builder to be used to configure the entity. + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("Users"); + builder.HasKey(user => user.Id); + builder.Property(user => user.Id) + .ValueGeneratedNever() // because EF always tries to generate the value for the Id, and because we generate it as part of the aggregate root, we need to tell EF not to generate it + .HasColumnOrder(0); + + builder.Property(user => user.Username) + .IsRequired() + .HasMaxLength(255) + .HasColumnOrder(1); + + builder.Property(user => user.Password) + .IsRequired() + .HasColumnOrder(2); + + builder.Property(user => user.TotpSecret) + .HasColumnOrder(3); + + builder.Property(user => user.VerificationToken) + .HasColumnOrder(4); + + builder.Property(user => user.VerificationTokenCreated) + .HasColumnOrder(5); + } +} diff --git a/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.Designer.cs b/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.Designer.cs new file mode 100644 index 00000000..af2adb76 --- /dev/null +++ b/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.Designer.cs @@ -0,0 +1,366 @@ +// +using System; +using Lumina.DataAccess.Core.UoW; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; + +#nullable disable + +namespace Lumina.DataAccess.Common.Migrations +{ + [DbContext(typeof(LuminaDbContext))] + [Migration("20241103132414_AddUsersTable")] + partial class AddUsersTable + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "8.0.10"); + + modelBuilder.Entity("BookGenres", b => + { + b.Property("BookId") + .HasColumnType("TEXT"); + + b.Property("GenreId") + .HasColumnType("TEXT"); + + b.HasKey("BookId", "GenreId"); + + b.HasIndex("GenreId"); + + b.ToTable("BookGenres", (string)null); + }); + + modelBuilder.Entity("BookTags", b => + { + b.Property("BookId") + .HasColumnType("TEXT"); + + b.Property("TagId") + .HasColumnType("TEXT"); + + b.HasKey("BookId", "TagId"); + + b.HasIndex("TagId"); + + b.ToTable("BookTags", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.Common.GenreEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Genres", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.Common.TagEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Tags", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b => + { + b.Property("Id") + .HasColumnType("TEXT") + .HasColumnOrder(0); + + b.Property("Created") + .HasColumnType("TEXT"); + + b.Property("Password") + .IsRequired() + .HasColumnType("TEXT") + .HasColumnOrder(2); + + b.Property("TotpSecret") + .HasColumnType("TEXT") + .HasColumnOrder(3); + + b.Property("Updated") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT") + .HasColumnOrder(1); + + b.Property("VerificationToken") + .HasColumnType("TEXT") + .HasColumnOrder(4); + + b.Property("VerificationTokenCreated") + .HasColumnType("TEXT") + .HasColumnOrder(5); + + b.HasKey("Id"); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b => + { + b.Property("Id") + .HasColumnType("TEXT") + .HasColumnOrder(0); + + b.Property("ASIN") + .HasMaxLength(10) + .HasColumnType("TEXT") + .HasColumnOrder(21); + + b.Property("AppleBooksId") + .HasColumnType("TEXT") + .HasColumnOrder(29); + + b.Property("BarnesAndNobleId") + .HasMaxLength(10) + .HasColumnType("TEXT") + .HasColumnOrder(28); + + b.Property("Created") + .HasColumnType("TEXT") + .HasColumnOrder(30); + + b.Property("Description") + .HasMaxLength(2000) + .HasColumnType("TEXT") + .HasColumnOrder(3); + + b.Property("Edition") + .HasMaxLength(50) + .HasColumnType("TEXT") + .HasColumnOrder(19); + + b.Property("Format") + .IsRequired() + .HasColumnType("TEXT") + .HasColumnOrder(18); + + b.Property("GoodreadsId") + .HasColumnType("TEXT") + .HasColumnOrder(22); + + b.Property("GoogleBooksId") + .HasMaxLength(12) + .HasColumnType("TEXT") + .HasColumnOrder(27); + + b.Property("LCCN") + .HasColumnType("TEXT") + .HasColumnOrder(23); + + b.Property("LanguageCode") + .HasColumnType("TEXT") + .HasColumnOrder(10); + + b.Property("LanguageName") + .HasColumnType("TEXT") + .HasColumnOrder(11); + + b.Property("LanguageNativeName") + .HasColumnType("TEXT") + .HasColumnOrder(12); + + b.Property("LibraryThingId") + .HasMaxLength(50) + .HasColumnType("TEXT") + .HasColumnOrder(26); + + b.Property("OCLCNumber") + .HasColumnType("TEXT") + .HasColumnOrder(24); + + b.Property("OpenLibraryId") + .HasMaxLength(50) + .HasColumnType("TEXT") + .HasColumnOrder(25); + + b.Property("OriginalLanguageCode") + .HasColumnType("TEXT") + .HasColumnOrder(13); + + b.Property("OriginalLanguageName") + .HasColumnType("TEXT") + .HasColumnOrder(14); + + b.Property("OriginalLanguageNativeName") + .HasColumnType("TEXT") + .HasColumnOrder(15); + + b.Property("OriginalReleaseDate") + .HasColumnType("TEXT") + .HasColumnOrder(4); + + b.Property("OriginalReleaseYear") + .HasColumnType("INTEGER") + .HasColumnOrder(5); + + b.Property("OriginalTitle") + .HasMaxLength(255) + .HasColumnType("TEXT") + .HasColumnOrder(2); + + b.Property("PageCount") + .HasColumnType("INTEGER") + .HasColumnOrder(17); + + b.Property("Publisher") + .HasMaxLength(100) + .HasColumnType("TEXT") + .HasColumnOrder(16); + + b.Property("ReReleaseDate") + .HasColumnType("TEXT") + .HasColumnOrder(6); + + b.Property("ReReleaseYear") + .HasColumnType("INTEGER") + .HasColumnOrder(7); + + b.Property("ReleaseCountry") + .HasColumnType("TEXT") + .HasColumnOrder(8); + + b.Property("ReleaseVersion") + .HasColumnType("TEXT") + .HasColumnOrder(9); + + b.Property("Title") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT") + .HasColumnOrder(1); + + b.Property("Updated") + .HasColumnType("TEXT") + .HasColumnOrder(31); + + b.Property("VolumeNumber") + .HasColumnType("INTEGER") + .HasColumnOrder(20); + + b.HasKey("Id"); + + b.ToTable("Books", (string)null); + }); + + modelBuilder.Entity("BookGenres", b => + { + b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null) + .WithMany() + .HasForeignKey("BookId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Lumina.Contracts.Entities.Common.GenreEntity", null) + .WithMany() + .HasForeignKey("GenreId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("BookTags", b => + { + b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null) + .WithMany() + .HasForeignKey("BookId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("Lumina.Contracts.Entities.Common.TagEntity", null) + .WithMany() + .HasForeignKey("TagId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b => + { + b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 => + { + b1.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b1.Property("BookId") + .HasColumnType("TEXT"); + + b1.Property("MaxValue") + .IsRequired() + .HasColumnType("decimal(3,2)"); + + b1.Property("Source") + .IsRequired() + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b1.Property("Value") + .IsRequired() + .HasColumnType("decimal(3,2)"); + + b1.Property("VoteCount") + .HasColumnType("INTEGER"); + + b1.HasKey("Id"); + + b1.HasIndex("BookId"); + + b1.ToTable("BookRatings", (string)null); + + b1.WithOwner() + .HasForeignKey("BookId"); + }); + + b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 => + { + b1.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("INTEGER"); + + b1.Property("BookId") + .HasColumnType("TEXT"); + + b1.Property("Format") + .IsRequired() + .HasMaxLength(6) + .HasColumnType("TEXT"); + + b1.Property("Value") + .IsRequired() + .HasMaxLength(13) + .HasColumnType("TEXT") + .HasColumnName("ISBN"); + + b1.HasKey("Id"); + + b1.HasIndex("BookId"); + + b1.ToTable("BookISBNs", (string)null); + + b1.WithOwner() + .HasForeignKey("BookId"); + }); + + b.Navigation("ISBNs"); + + b.Navigation("Ratings"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.cs b/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.cs new file mode 100644 index 00000000..713e1739 --- /dev/null +++ b/src/Lumina.DataAccess/Common/Migrations/20241103132414_AddUsersTable.cs @@ -0,0 +1,39 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Lumina.DataAccess.Common.Migrations; + +/// +public partial class AddUsersTable : Migration +{ + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Users", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + Username = table.Column(type: "TEXT", maxLength: 255, nullable: false), + Password = table.Column(type: "TEXT", nullable: false), + TotpSecret = table.Column(type: "TEXT", nullable: true), + VerificationToken = table.Column(type: "TEXT", nullable: true), + VerificationTokenCreated = table.Column(type: "TEXT", nullable: true), + Created = table.Column(type: "TEXT", nullable: false), + Updated = table.Column(type: "TEXT", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Users", x => x.Id); + }); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "Users"); + } +} diff --git a/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs b/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs index 1c5bbaa3..d9949ea8 100644 --- a/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs +++ b/src/Lumina.DataAccess/Common/Migrations/LuminaDbContextModelSnapshot.cs @@ -15,7 +15,7 @@ namespace Lumina.DataAccess.Common.Migrations protected override void BuildModel(ModelBuilder modelBuilder) { #pragma warning disable 612, 618 - modelBuilder.HasAnnotation("ProductVersion", "8.0.8"); + modelBuilder.HasAnnotation("ProductVersion", "8.0.10"); modelBuilder.Entity("BookGenres", b => { @@ -47,7 +47,69 @@ namespace Lumina.DataAccess.Common.Migrations b.ToTable("BookTags", (string)null); }); - modelBuilder.Entity("Lumina.Application.Common.Models.Books.BookModel", b => + modelBuilder.Entity("Lumina.Contracts.Entities.Common.GenreEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Genres", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.Common.TagEntity", b => + { + b.Property("Name") + .HasMaxLength(50) + .HasColumnType("TEXT"); + + b.HasKey("Name"); + + b.ToTable("Tags", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b => + { + b.Property("Id") + .HasColumnType("TEXT") + .HasColumnOrder(0); + + b.Property("Created") + .HasColumnType("TEXT"); + + b.Property("Password") + .IsRequired() + .HasColumnType("TEXT") + .HasColumnOrder(2); + + b.Property("TotpSecret") + .HasColumnType("TEXT") + .HasColumnOrder(3); + + b.Property("Updated") + .HasColumnType("TEXT"); + + b.Property("Username") + .IsRequired() + .HasMaxLength(255) + .HasColumnType("TEXT") + .HasColumnOrder(1); + + b.Property("VerificationToken") + .HasColumnType("TEXT") + .HasColumnOrder(4); + + b.Property("VerificationTokenCreated") + .HasColumnType("TEXT") + .HasColumnOrder(5); + + b.HasKey("Id"); + + b.ToTable("Users", (string)null); + }); + + modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b => { b.Property("Id") .HasColumnType("TEXT") @@ -194,37 +256,15 @@ namespace Lumina.DataAccess.Common.Migrations b.ToTable("Books", (string)null); }); - modelBuilder.Entity("Lumina.Application.Common.Models.Common.GenreModel", b => - { - b.Property("Name") - .HasMaxLength(50) - .HasColumnType("TEXT"); - - b.HasKey("Name"); - - b.ToTable("Genres", (string)null); - }); - - modelBuilder.Entity("Lumina.Application.Common.Models.Common.TagModel", b => - { - b.Property("Name") - .HasMaxLength(50) - .HasColumnType("TEXT"); - - b.HasKey("Name"); - - b.ToTable("Tags", (string)null); - }); - modelBuilder.Entity("BookGenres", b => { - b.HasOne("Lumina.Application.Common.Models.Books.BookModel", null) + b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null) .WithMany() .HasForeignKey("BookId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("Lumina.Application.Common.Models.Common.GenreModel", null) + b.HasOne("Lumina.Contracts.Entities.Common.GenreEntity", null) .WithMany() .HasForeignKey("GenreId") .OnDelete(DeleteBehavior.Cascade) @@ -233,22 +273,22 @@ namespace Lumina.DataAccess.Common.Migrations modelBuilder.Entity("BookTags", b => { - b.HasOne("Lumina.Application.Common.Models.Books.BookModel", null) + b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null) .WithMany() .HasForeignKey("BookId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); - b.HasOne("Lumina.Application.Common.Models.Common.TagModel", null) + b.HasOne("Lumina.Contracts.Entities.Common.TagEntity", null) .WithMany() .HasForeignKey("TagId") .OnDelete(DeleteBehavior.Cascade) .IsRequired(); }); - modelBuilder.Entity("Lumina.Application.Common.Models.Books.BookModel", b => + modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b => { - b.OwnsMany("Lumina.Application.Common.Models.Books.BookRatingModel", "Ratings", b1 => + b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 => { b1.Property("Id") .ValueGeneratedOnAdd() @@ -257,7 +297,8 @@ namespace Lumina.DataAccess.Common.Migrations b1.Property("BookId") .HasColumnType("TEXT"); - b1.Property("MaxValue") + b1.Property("MaxValue") + .IsRequired() .HasColumnType("decimal(3,2)"); b1.Property("Source") @@ -265,7 +306,8 @@ namespace Lumina.DataAccess.Common.Migrations .HasMaxLength(50) .HasColumnType("TEXT"); - b1.Property("Value") + b1.Property("Value") + .IsRequired() .HasColumnType("decimal(3,2)"); b1.Property("VoteCount") @@ -281,7 +323,7 @@ namespace Lumina.DataAccess.Common.Migrations .HasForeignKey("BookId"); }); - b.OwnsMany("Lumina.Application.Common.Models.Books.IsbnModel", "ISBNs", b1 => + b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 => { b1.Property("Id") .ValueGeneratedOnAdd() diff --git a/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs b/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs index 2b07166b..f44a7ba5 100644 --- a/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs +++ b/src/Lumina.DataAccess/Core/Repositories/Books/BookRepository.cs @@ -38,8 +38,8 @@ internal sealed class BookRepository : IBookRepository /// An representing either a successfull operation, or an error. public async Task> InsertAsync(BookEntity book, CancellationToken cancellationToken) { - bool jobExists = await _luminaDbContext.Books.AnyAsync(repositoryBook => repositoryBook.Id == book.Id, cancellationToken: cancellationToken).ConfigureAwait(false); - if (jobExists) + bool bookExists = await _luminaDbContext.Books.AnyAsync(repositoryBook => repositoryBook.Id == book.Id, cancellationToken: cancellationToken).ConfigureAwait(false); + if (bookExists) return Errors.WrittenContent.BookAlreadyExists; // fetch existing tags and genres diff --git a/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs b/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs new file mode 100644 index 00000000..7ef0e209 --- /dev/null +++ b/src/Lumina.DataAccess/Core/Repositories/Users/UserRepository.cs @@ -0,0 +1,46 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +using Lumina.Application.Common.DataAccess.Repositories.Users; +using Lumina.Contracts.Entities.UsersManagement; +using Lumina.DataAccess.Core.UoW; +using Lumina.Domain.Common.Errors; +using Microsoft.EntityFrameworkCore; +using System.Threading; +using System.Threading.Tasks; +#endregion + +namespace Lumina.DataAccess.Core.Repositories.Users; + +/// +/// Repository for users. +/// +internal sealed class UserRepository : IUserRepository +{ + private readonly LuminaDbContext _luminaDbContext; + + /// + /// Initializes a new instance of the class. + /// + /// Injected Entity Framework DbContext. + public UserRepository(LuminaDbContext luminaDbContext) + { + _luminaDbContext = luminaDbContext; + } + + /// + /// Adds a new user. + /// + /// The user to add. + /// Cancellation token that can be used to stop the execution. + /// An representing either a successfull operation, or an error. + public async Task> InsertAsync(UserEntity user, CancellationToken cancellationToken) + { + bool userExists = await _luminaDbContext.Users.AnyAsync(repositoryUser => repositoryUser.Id == user.Id, cancellationToken: cancellationToken).ConfigureAwait(false); + if (userExists) + return Errors.Users.UserAlreadyExists; + + _luminaDbContext.Users.Add(user); + return Result.Created; + } + +} diff --git a/src/Lumina.DataAccess/Core/UoW/LuminaDbContext.cs b/src/Lumina.DataAccess/Core/UoW/LuminaDbContext.cs index dcf7147e..f7d06b86 100644 --- a/src/Lumina.DataAccess/Core/UoW/LuminaDbContext.cs +++ b/src/Lumina.DataAccess/Core/UoW/LuminaDbContext.cs @@ -1,5 +1,6 @@ #region ========================================================================= USING ===================================================================================== using Lumina.Contracts.Entities.Common; +using Lumina.Contracts.Entities.UsersManagement; using Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary; using Lumina.Domain.Common.Events; using Microsoft.EntityFrameworkCore; @@ -19,6 +20,7 @@ namespace Lumina.DataAccess.Core.UoW; public class LuminaDbContext : DbContext { public virtual DbSet Books { get; set; } = null!; + public virtual DbSet Users { get; set; } = null!; /// /// Initializes a new instance of the class. diff --git a/src/Lumina.Domain/Common/Errors/Errors.Users.cs b/src/Lumina.Domain/Common/Errors/Errors.Users.cs new file mode 100644 index 00000000..d448682b --- /dev/null +++ b/src/Lumina.Domain/Common/Errors/Errors.Users.cs @@ -0,0 +1,16 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +#endregion + +namespace Lumina.Domain.Common.Errors; + +/// +/// Users error types. +/// +public static partial class Errors +{ + public static class Users + { + public static Error UserAlreadyExists => Error.Conflict(description: nameof(UserAlreadyExists)); + } +} diff --git a/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/Book.cs b/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/Book.cs index faa4dbcd..d2f98079 100644 --- a/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/Book.cs +++ b/src/Lumina.Domain/Core/Aggregates/WrittenContentLibrary/BookLibraryAggregate/Book.cs @@ -14,7 +14,7 @@ using System.Diagnostics; namespace Lumina.Domain.Core.Aggregates.WrittenContentLibrary.BookLibraryAggregate; /// -/// Aggregate root for a book. +/// Aggregate root for a book library aggregate. /// [DebuggerDisplay("{Id}: {Title}")] public sealed class Book : AggregateRoot @@ -322,4 +322,4 @@ public sealed class Book : AggregateRoot contributors, ratings); } -} \ No newline at end of file +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/User.cs b/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/User.cs new file mode 100644 index 00000000..953b6b96 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/User.cs @@ -0,0 +1,64 @@ +#region ========================================================================= USING ===================================================================================== +using ErrorOr; +using Lumina.Domain.Common.Models.Core; +using Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate; + +/// +/// Aggregate root for a user aggregate. +/// +[DebuggerDisplay("{Id}: {Username}")] +public sealed class User : AggregateRoot +{ + /// + /// Gets or sets the username of the user. + /// + public string Username { get; private set; } + + /// + /// Initializes a new instance of the class. + /// + /// The unique identifier of the user. + /// The username of the user. + private User( + UserId id, + string username) : base(id) + { + Username = username; + } + + /// + /// Creates a new instance of the class. + /// + /// The username of the user. + /// + /// An containing either a successfully created , or an error message. + /// + public static ErrorOr Create( + string username) + { + return new User( + UserId.CreateUnique(), + username); + } + + /// + /// Creates a new instance of the , with a pre-existing . + /// + /// The object representing the id of the user. + /// The username of the user. + /// + /// An containing either a successfully created , or an error message. + /// + public static ErrorOr Create( + UserId id, + string username) + { + return new User( + id, + username); + } +} diff --git a/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/ValueObjects/UserId.cs b/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/ValueObjects/UserId.cs new file mode 100644 index 00000000..853648d8 --- /dev/null +++ b/src/Lumina.Domain/Core/BoundedContexts/UserManagementBoundedContext/UserAggregate/ValueObjects/UserId.cs @@ -0,0 +1,48 @@ +#region ========================================================================= USING ===================================================================================== +using Lumina.Domain.Common.Models.Core; +using System; +using System.Collections.Generic; +using System.Diagnostics; +#endregion + +namespace Lumina.Domain.Core.BoundedContexts.UserManagementBoundedContext.UserAggregate.ValueObjects; + +/// +/// Value Object for the Id of a user. +/// +[DebuggerDisplay("{Value}")] +public sealed class UserId : EntityId +{ + /// + /// Initializes a new instance of the class. + /// + /// The value representing this object. + private UserId(Guid value) : base(value) + { + } + + /// + /// Creates a new instance of the class. + /// + /// The created instance. + public static UserId CreateUnique() + { + return new UserId(Guid.NewGuid()); + } + + /// + /// Creates a new instance of the class, from a pre-existing . + /// + /// The value used to create the instance. + /// The created instance. + public static UserId Create(Guid value) + { + return new UserId(value); + } + + /// + public override IEnumerable GetEqualityComponents() + { + yield return Value; + } +}