Implements first stage library management system

This commit is contained in:
BinaryAlley committed 2024-11-03 19:43:46 +02:00
1 parent 5e0f94cd82
commit 36352ea9e8
291 files changed
+3733 -1384

No files matched your search

@@ -1,8 +1,9 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Entities.Common;
using Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary;
using Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Collections.Generic;
#endregion
@@ -25,63 +26,114 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
.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(book => book.Title).IsRequired().HasMaxLength(255).HasColumnOrder(1);
builder.Property(book => book.OriginalTitle).HasMaxLength(255).HasColumnOrder(2);
builder.Property(book => book.Description).HasMaxLength(2000).HasColumnOrder(3);
builder.Property(book => book.OriginalReleaseDate).HasColumnOrder(4);
builder.Property(book => book.OriginalReleaseYear).HasColumnOrder(5);
builder.Property(book => book.ReReleaseDate).HasColumnOrder(6);
builder.Property(book => book.ReReleaseYear).HasColumnOrder(7);
builder.Property(book => book.ReleaseCountry).HasColumnOrder(8);
builder.Property(book => book.ReleaseVersion).HasColumnOrder(9);
builder.Property(book => book.LanguageCode).HasColumnOrder(10);
builder.Property(book => book.LanguageName).HasColumnOrder(11);
builder.Property(book => book.LanguageNativeName).HasColumnOrder(12);
builder.Property(book => book.OriginalLanguageCode).HasColumnOrder(13);
builder.Property(book => book.OriginalLanguageName).HasColumnOrder(14);
builder.Property(book => book.OriginalLanguageNativeName).HasColumnOrder(15);
builder.Property(book => book.Title)
.IsRequired()
.HasMaxLength(255)
.HasColumnOrder(1);
builder.Property(book => book.OriginalTitle)
.HasMaxLength(255)
.HasColumnOrder(2);
builder.Property(book => book.Description)
.HasMaxLength(2000)
.HasColumnOrder(3);
builder.Property(book => book.OriginalReleaseDate)
.HasColumnOrder(4);
builder.Property(book => book.OriginalReleaseYear)
.HasColumnOrder(5);
builder.Property(book => book.ReReleaseDate)
.HasColumnOrder(6);
builder.Property(book => book.ReReleaseYear)
.HasColumnOrder(7);
builder.Property(book => book.ReleaseCountry)
.HasColumnOrder(8);
builder.Property(book => book.ReleaseVersion)
.HasColumnOrder(9);
builder.Property(book => book.LanguageCode)
.HasColumnOrder(10);
builder.Property(book => book.LanguageName)
.HasColumnOrder(11);
builder.Property(book => book.LanguageNativeName)
.HasColumnOrder(12);
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), 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>>(
"BookTags",
j => j.HasOne<TagEntity>().WithMany().HasForeignKey("TagId"),
j => j.HasOne<BookEntity>().WithMany().HasForeignKey("BookId"),
j =>
{
j.HasKey("BookId", "TagId");
j.ToTable("BookTags");
});
.WithMany()
.UsingEntity<Dictionary<string, object>>(
"BookTags",
j => j.HasOne<TagEntity>()
.WithMany()
.HasForeignKey("TagId"),
j => j.HasOne<BookEntity>()
.WithMany()
.HasForeignKey("BookId"),
j =>
{
j.HasKey("BookId", "TagId");
j.ToTable("BookTags");
});
builder.HasMany(book => book.Genres)
.WithMany()
.UsingEntity<Dictionary<string, object>>(
"BookGenres",
j => j.HasOne<GenreEntity>().WithMany().HasForeignKey("GenreId"),
j => j.HasOne<BookEntity>().WithMany().HasForeignKey("BookId"),
j =>
{
j.HasKey("BookId", "GenreId");
j.ToTable("BookGenres");
});
.WithMany()
.UsingEntity<Dictionary<string, object>>(
"BookGenres",
j => j.HasOne<GenreEntity>()
.WithMany()
.HasForeignKey("GenreId"),
j => j.HasOne<BookEntity>()
.WithMany()
.HasForeignKey("BookId"),
j =>
{
j.HasKey("BookId", "GenreId");
j.ToTable("BookGenres");
});
builder.Property(book => book.Publisher).HasMaxLength(100).HasColumnOrder(16);
builder.Property(book => book.PageCount).HasColumnOrder(17);
builder.Property(book => book.Format).HasColumnOrder(18);
builder.Property(book => book.Edition).HasMaxLength(50).HasColumnOrder(19);
builder.Property(book => book.VolumeNumber).HasColumnOrder(20);
builder.Property(book => book.ASIN).HasMaxLength(10).HasColumnOrder(21);
builder.Property(book => book.GoodreadsId).HasColumnOrder(22);
builder.Property(book => book.LCCN).HasColumnOrder(23);
builder.Property(book => book.OCLCNumber).HasColumnOrder(24);
builder.Property(book => book.OpenLibraryId).HasMaxLength(50).HasColumnOrder(25);
builder.Property(book => book.LibraryThingId).HasMaxLength(50).HasColumnOrder(26);
builder.Property(book => book.GoogleBooksId).HasMaxLength(12).HasColumnOrder(27);
builder.Property(book => book.BarnesAndNobleId).HasMaxLength(10).HasColumnOrder(28);
builder.Property(book => book.AppleBooksId).HasColumnOrder(29);
builder.Property(book => book.Created).HasColumnOrder(30);
builder.Property(book => book.Updated).HasColumnOrder(31);
builder.Property(book => book.Publisher)
.HasMaxLength(100)
.HasColumnOrder(16);
builder.Property(book => book.PageCount)
.HasColumnOrder(17);
builder.Property(book => book.Format)
.HasConversion<string>()
.HasColumnOrder(18);
builder.Property(book => book.Edition)
.HasMaxLength(50)
.HasColumnOrder(19);
builder.Property(book => book.VolumeNumber)
.HasColumnOrder(20);
builder.Property(book => book.ASIN)
.HasMaxLength(10)
.HasColumnOrder(21);
builder.Property(book => book.GoodreadsId)
.HasColumnOrder(22);
builder.Property(book => book.LCCN)
.HasColumnOrder(23);
builder.Property(book => book.OCLCNumber)
.HasColumnOrder(24);
builder.Property(book => book.OpenLibraryId)
.HasMaxLength(50)
.HasColumnOrder(25);
builder.Property(book => book.LibraryThingId)
.HasMaxLength(50)
.HasColumnOrder(26);
builder.Property(book => book.GoogleBooksId)
.HasMaxLength(12)
.HasColumnOrder(27);
builder.Property(book => book.BarnesAndNobleId)
.HasMaxLength(10)
.HasColumnOrder(28);
builder.Property(book => book.AppleBooksId)
.HasColumnOrder(29);
builder.Property(book => book.Created)
.HasColumnOrder(30);
builder.Property(book => book.Updated)
.HasColumnOrder(31);
//builder.HasMany<ContributorIdModel>()
//.WithMany()
@@ -108,8 +160,10 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
builder.OwnsMany(book => book.Ratings, ratingBuilder =>
{
ratingBuilder.ToTable("BookRatings");
ratingBuilder.WithOwner().HasForeignKey("BookId");
ratingBuilder.Property<int>("Id").ValueGeneratedOnAdd();
ratingBuilder.WithOwner()
.HasForeignKey("BookId");
ratingBuilder.Property<Guid>("Id")
.ValueGeneratedOnAdd();
ratingBuilder.HasKey("Id");
ratingBuilder.Property(rating => rating.Value)
@@ -132,8 +186,10 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
builder.OwnsMany(book => book.ISBNs, isbnBuilder =>
{
isbnBuilder.ToTable("BookISBNs");
isbnBuilder.WithOwner().HasForeignKey("BookId");
isbnBuilder.Property<int>("Id").ValueGeneratedOnAdd();
isbnBuilder.WithOwner()
.HasForeignKey("BookId");
isbnBuilder.Property<Guid>("Id")
.ValueGeneratedOnAdd();
isbnBuilder.HasKey("Id");
isbnBuilder.Property(isbn => isbn.Value)
@@ -157,4 +213,4 @@ public class BookConfiguration : IEntityTypeConfiguration<BookEntity>
// builder.HasKey(c => c.Id);
// builder.Property(c => c.Id).ValueGeneratedNever();
// }
//}
//}
@@ -0,0 +1,60 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Entities.MediaLibrary.Management;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
#endregion
namespace Lumina.DataAccess.Common.Configuration;
/// <summary>
/// Configures the entity mapping for the <see cref="LibraryEntity"/> entity.
/// </summary>
public class LibraryConfiguration : IEntityTypeConfiguration<LibraryEntity>
{
/// <summary>
/// Configures the <see cref="LibraryEntity"/> entity.
/// </summary>
/// <param name="builder">The builder to be used to configure the entity.</param>
public void Configure(EntityTypeBuilder<LibraryEntity> builder)
{
builder.ToTable("Libraries");
builder.HasKey(library => library.Id);
builder.Property(library => library.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(library => library.Title)
.IsRequired()
.HasMaxLength(255)
.HasColumnOrder(1);
builder.Property(library => library.LibraryType)
.IsRequired()
.HasConversion<string>()
.HasColumnOrder(2);
// one user with many libraries
builder.HasOne(library => library.User)
.WithMany(user => user.Libraries)
.HasForeignKey(library => library.UserId)
.IsRequired();
// using OwnsMany because paths are domain Value Objects, thus they have no independent identity, and their lifecycle is bound to the Library
builder.OwnsMany(library => library.ContentLocations, contentLocationBuilder =>
{
contentLocationBuilder.ToTable("LibraryContentLocations");
contentLocationBuilder.WithOwner()
.HasForeignKey("LibraryId");
contentLocationBuilder.Property<Guid>("Id")
.ValueGeneratedOnAdd();
contentLocationBuilder.HasKey("Id");
contentLocationBuilder.Property(location => location.Path)
.HasColumnName("Path")
.HasMaxLength(260)
.IsRequired();
});
}
}
@@ -40,5 +40,11 @@ public class UserConfiguration : IEntityTypeConfiguration<UserEntity>
builder.Property(user => user.VerificationTokenCreated)
.HasColumnOrder(5);
builder.HasMany(user => user.Libraries)
.WithOne(library => library.User)
.HasForeignKey(library => library.UserId)
.IsRequired() // a library must have a user
.OnDelete(DeleteBehavior.Cascade);
}
}
@@ -0,0 +1,441 @@
// <auto-generated />
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("20241103170430_AddLibrariesTable")]
partial class AddLibrariesTable
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.10");
modelBuilder.Entity("BookGenres", b =>
{
b.Property<Guid>("BookId")
.HasColumnType("TEXT");
b.Property<string>("GenreId")
.HasColumnType("TEXT");
b.HasKey("BookId", "GenreId");
b.HasIndex("GenreId");
b.ToTable("BookGenres", (string)null);
});
modelBuilder.Entity("BookTags", b =>
{
b.Property<Guid>("BookId")
.HasColumnType("TEXT");
b.Property<string>("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<string>("Name")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Genres", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.Common.TagEntity", b =>
{
b.Property<string>("Name")
.HasMaxLength(50)
.HasColumnType("TEXT");
b.HasKey("Name");
b.ToTable("Tags", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.Management.LibraryEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<string>("LibraryType")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT");
b.Property<Guid>("UserId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("UserId");
b.ToTable("Libraries", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<string>("ASIN")
.HasMaxLength(10)
.HasColumnType("TEXT")
.HasColumnOrder(21);
b.Property<string>("AppleBooksId")
.HasColumnType("TEXT")
.HasColumnOrder(29);
b.Property<string>("BarnesAndNobleId")
.HasMaxLength(10)
.HasColumnType("TEXT")
.HasColumnOrder(28);
b.Property<DateTime>("Created")
.HasColumnType("TEXT")
.HasColumnOrder(30);
b.Property<string>("Description")
.HasMaxLength(2000)
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<string>("Edition")
.HasMaxLength(50)
.HasColumnType("TEXT")
.HasColumnOrder(19);
b.Property<string>("Format")
.HasColumnType("TEXT")
.HasColumnOrder(18);
b.Property<string>("GoodreadsId")
.HasColumnType("TEXT")
.HasColumnOrder(22);
b.Property<string>("GoogleBooksId")
.HasMaxLength(12)
.HasColumnType("TEXT")
.HasColumnOrder(27);
b.Property<string>("LCCN")
.HasColumnType("TEXT")
.HasColumnOrder(23);
b.Property<string>("LanguageCode")
.HasColumnType("TEXT")
.HasColumnOrder(10);
b.Property<string>("LanguageName")
.HasColumnType("TEXT")
.HasColumnOrder(11);
b.Property<string>("LanguageNativeName")
.HasColumnType("TEXT")
.HasColumnOrder(12);
b.Property<string>("LibraryThingId")
.HasMaxLength(50)
.HasColumnType("TEXT")
.HasColumnOrder(26);
b.Property<string>("OCLCNumber")
.HasColumnType("TEXT")
.HasColumnOrder(24);
b.Property<string>("OpenLibraryId")
.HasMaxLength(50)
.HasColumnType("TEXT")
.HasColumnOrder(25);
b.Property<string>("OriginalLanguageCode")
.HasColumnType("TEXT")
.HasColumnOrder(13);
b.Property<string>("OriginalLanguageName")
.HasColumnType("TEXT")
.HasColumnOrder(14);
b.Property<string>("OriginalLanguageNativeName")
.HasColumnType("TEXT")
.HasColumnOrder(15);
b.Property<DateOnly?>("OriginalReleaseDate")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<int?>("OriginalReleaseYear")
.HasColumnType("INTEGER")
.HasColumnOrder(5);
b.Property<string>("OriginalTitle")
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<int?>("PageCount")
.HasColumnType("INTEGER")
.HasColumnOrder(17);
b.Property<string>("Publisher")
.HasMaxLength(100)
.HasColumnType("TEXT")
.HasColumnOrder(16);
b.Property<DateOnly?>("ReReleaseDate")
.HasColumnType("TEXT")
.HasColumnOrder(6);
b.Property<int?>("ReReleaseYear")
.HasColumnType("INTEGER")
.HasColumnOrder(7);
b.Property<string>("ReleaseCountry")
.HasColumnType("TEXT")
.HasColumnOrder(8);
b.Property<string>("ReleaseVersion")
.HasColumnType("TEXT")
.HasColumnOrder(9);
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT")
.HasColumnOrder(31);
b.Property<int?>("VolumeNumber")
.HasColumnType("INTEGER")
.HasColumnOrder(20);
b.HasKey("Id");
b.ToTable("Books", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<string>("TotpSecret")
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<string>("VerificationToken")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<DateTime?>("VerificationTokenCreated")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.HasKey("Id");
b.ToTable("Users", (string)null);
});
modelBuilder.Entity("BookGenres", b =>
{
b.HasOne("Lumina.Contracts.Entities.MediaLibrary.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.MediaLibrary.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.MediaLibrary.Management.LibraryEntity", b =>
{
b.HasOne("Lumina.Contracts.Entities.UsersManagement.UserEntity", "User")
.WithMany("Libraries")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.Management.LibraryContentLocationEntity", "ContentLocations", b1 =>
{
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b1.Property<Guid>("LibraryId")
.HasColumnType("TEXT");
b1.Property<string>("Path")
.IsRequired()
.HasMaxLength(260)
.HasColumnType("TEXT")
.HasColumnName("Path");
b1.HasKey("Id");
b1.HasIndex("LibraryId");
b1.ToTable("LibraryContentLocations", (string)null);
b1.WithOwner()
.HasForeignKey("LibraryId");
});
b.Navigation("ContentLocations");
b.Navigation("User");
});
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
{
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 =>
{
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b1.Property<Guid>("BookId")
.HasColumnType("TEXT");
b1.Property<decimal?>("MaxValue")
.IsRequired()
.HasColumnType("decimal(3,2)");
b1.Property<string>("Source")
.IsRequired()
.HasMaxLength(50)
.HasColumnType("TEXT");
b1.Property<decimal?>("Value")
.IsRequired()
.HasColumnType("decimal(3,2)");
b1.Property<int?>("VoteCount")
.HasColumnType("INTEGER");
b1.HasKey("Id");
b1.HasIndex("BookId");
b1.ToTable("BookRatings", (string)null);
b1.WithOwner()
.HasForeignKey("BookId");
});
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 =>
{
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b1.Property<Guid>("BookId")
.HasColumnType("TEXT");
b1.Property<string>("Format")
.IsRequired()
.HasMaxLength(6)
.HasColumnType("TEXT");
b1.Property<string>("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");
});
modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b =>
{
b.Navigation("Libraries");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,129 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lumina.DataAccess.Common.Migrations;
/// <inheritdoc />
public partial class AddLibrariesTable : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "Format",
table: "Books",
type: "TEXT",
nullable: true,
oldClrType: typeof(string),
oldType: "TEXT");
migrationBuilder.AlterColumn<Guid>(
name: "Id",
table: "BookRatings",
type: "TEXT",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER")
.OldAnnotation("Sqlite:Autoincrement", true);
migrationBuilder.AlterColumn<Guid>(
name: "Id",
table: "BookISBNs",
type: "TEXT",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER")
.OldAnnotation("Sqlite:Autoincrement", true);
migrationBuilder.CreateTable(
name: "Libraries",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Title = table.Column<string>(type: "TEXT", maxLength: 255, nullable: false),
LibraryType = table.Column<string>(type: "TEXT", nullable: false),
UserId = table.Column<Guid>(type: "TEXT", nullable: false),
Created = table.Column<DateTime>(type: "TEXT", nullable: false),
Updated = table.Column<DateTime>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Libraries", x => x.Id);
table.ForeignKey(
name: "FK_Libraries_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "LibraryContentLocations",
columns: table => new
{
Id = table.Column<Guid>(type: "TEXT", nullable: false),
Path = table.Column<string>(type: "TEXT", maxLength: 260, nullable: false),
LibraryId = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_LibraryContentLocations", x => x.Id);
table.ForeignKey(
name: "FK_LibraryContentLocations_Libraries_LibraryId",
column: x => x.LibraryId,
principalTable: "Libraries",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Libraries_UserId",
table: "Libraries",
column: "UserId");
migrationBuilder.CreateIndex(
name: "IX_LibraryContentLocations_LibraryId",
table: "LibraryContentLocations",
column: "LibraryId");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "LibraryContentLocations");
migrationBuilder.DropTable(
name: "Libraries");
migrationBuilder.AlterColumn<string>(
name: "Format",
table: "Books",
type: "TEXT",
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "TEXT",
oldNullable: true);
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "BookRatings",
type: "INTEGER",
nullable: false,
oldClrType: typeof(Guid),
oldType: "TEXT")
.Annotation("Sqlite:Autoincrement", true);
migrationBuilder.AlterColumn<int>(
name: "Id",
table: "BookISBNs",
type: "INTEGER",
nullable: false,
oldClrType: typeof(Guid),
oldType: "TEXT")
.Annotation("Sqlite:Autoincrement", true);
}
}
@@ -69,7 +69,7 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("Tags", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b =>
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.Management.LibraryEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
@@ -78,38 +78,31 @@ namespace Lumina.DataAccess.Common.Migrations
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<string>("Password")
b.Property<string>("LibraryType")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<string>("TotpSecret")
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT");
b.Property<string>("Username")
b.Property<string>("Title")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<string>("VerificationToken")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT");
b.Property<DateTime?>("VerificationTokenCreated")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.Property<Guid>("UserId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Users", (string)null);
b.HasIndex("UserId");
b.ToTable("Libraries", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b =>
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
@@ -144,7 +137,6 @@ namespace Lumina.DataAccess.Common.Migrations
.HasColumnOrder(19);
b.Property<string>("Format")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(18);
@@ -256,9 +248,49 @@ namespace Lumina.DataAccess.Common.Migrations
b.ToTable("Books", (string)null);
});
modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b =>
{
b.Property<Guid>("Id")
.HasColumnType("TEXT")
.HasColumnOrder(0);
b.Property<DateTime>("Created")
.HasColumnType("TEXT");
b.Property<string>("Password")
.IsRequired()
.HasColumnType("TEXT")
.HasColumnOrder(2);
b.Property<string>("TotpSecret")
.HasColumnType("TEXT")
.HasColumnOrder(3);
b.Property<DateTime?>("Updated")
.HasColumnType("TEXT");
b.Property<string>("Username")
.IsRequired()
.HasMaxLength(255)
.HasColumnType("TEXT")
.HasColumnOrder(1);
b.Property<string>("VerificationToken")
.HasColumnType("TEXT")
.HasColumnOrder(4);
b.Property<DateTime?>("VerificationTokenCreated")
.HasColumnType("TEXT")
.HasColumnOrder(5);
b.HasKey("Id");
b.ToTable("Users", (string)null);
});
modelBuilder.Entity("BookGenres", b =>
{
b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null)
b.HasOne("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", null)
.WithMany()
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.Cascade)
@@ -273,7 +305,7 @@ namespace Lumina.DataAccess.Common.Migrations
modelBuilder.Entity("BookTags", b =>
{
b.HasOne("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", null)
b.HasOne("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", null)
.WithMany()
.HasForeignKey("BookId")
.OnDelete(DeleteBehavior.Cascade)
@@ -286,13 +318,51 @@ namespace Lumina.DataAccess.Common.Migrations
.IsRequired();
});
modelBuilder.Entity("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookEntity", b =>
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.Management.LibraryEntity", b =>
{
b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 =>
b.HasOne("Lumina.Contracts.Entities.UsersManagement.UserEntity", "User")
.WithMany("Libraries")
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.Management.LibraryContentLocationEntity", "ContentLocations", b1 =>
{
b1.Property<int>("Id")
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("TEXT");
b1.Property<Guid>("LibraryId")
.HasColumnType("TEXT");
b1.Property<string>("Path")
.IsRequired()
.HasMaxLength(260)
.HasColumnType("TEXT")
.HasColumnName("Path");
b1.HasKey("Id");
b1.HasIndex("LibraryId");
b1.ToTable("LibraryContentLocations", (string)null);
b1.WithOwner()
.HasForeignKey("LibraryId");
});
b.Navigation("ContentLocations");
b.Navigation("User");
});
modelBuilder.Entity("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookEntity", b =>
{
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.BookRatingEntity", "Ratings", b1 =>
{
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("TEXT");
b1.Property<Guid>("BookId")
.HasColumnType("TEXT");
@@ -323,11 +393,11 @@ namespace Lumina.DataAccess.Common.Migrations
.HasForeignKey("BookId");
});
b.OwnsMany("Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 =>
b.OwnsMany("Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary.IsbnEntity", "ISBNs", b1 =>
{
b1.Property<int>("Id")
b1.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
.HasColumnType("TEXT");
b1.Property<Guid>("BookId")
.HasColumnType("TEXT");
@@ -357,6 +427,11 @@ namespace Lumina.DataAccess.Common.Migrations
b.Navigation("Ratings");
});
modelBuilder.Entity("Lumina.Contracts.Entities.UsersManagement.UserEntity", b =>
{
b.Navigation("Libraries");
});
#pragma warning restore 612, 618
}
}
@@ -2,7 +2,7 @@
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.Books;
using Lumina.Contracts.Entities.Common;
using Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary;
using Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.DataAccess.Core.UoW;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
@@ -0,0 +1,45 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Repositories.MediaLibrary;
using Lumina.Contracts.Entities.MediaLibrary.Management;
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.Libraries;
/// <summary>
/// Repository for media libraries.
/// </summary>
internal sealed class LibraryRepository : ILibraryRepository
{
private readonly LuminaDbContext _luminaDbContext;
/// <summary>
/// Initializes a new instance of the <see cref="LibraryRepository"/> class.
/// </summary>
/// <param name="luminaDbContext">Injected Entity Framework DbContext.</param>
public LibraryRepository(LuminaDbContext luminaDbContext)
{
_luminaDbContext = luminaDbContext;
}
/// <summary>
/// Adds a new library.
/// </summary>
/// <param name="library">The library to add.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>An <see cref="ErrorOr{TValue}"/> representing either a successfull operation, or an error.</returns>
public async Task<ErrorOr<Created>> InsertAsync(LibraryEntity library, CancellationToken cancellationToken)
{
bool libraryExists = await _luminaDbContext.Libraries.AnyAsync(repositoryLibrary => repositoryLibrary.Id == library.Id, cancellationToken: cancellationToken).ConfigureAwait(false);
if (libraryExists)
return Errors.Library.LibraryAlreadyExists;
_luminaDbContext.Libraries.Add(library);
return Result.Created;
}
}
@@ -1,7 +1,8 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Contracts.Entities.Common;
using Lumina.Contracts.Entities.MediaLibrary.Management;
using Lumina.Contracts.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Contracts.Entities.UsersManagement;
using Lumina.Contracts.Entities.WrittenContentLibrary.BookLibrary;
using Lumina.Domain.Common.Events;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
@@ -21,6 +22,7 @@ public class LuminaDbContext : DbContext
{
public virtual DbSet<BookEntity> Books { get; set; } = null!;
public virtual DbSet<UserEntity> Users { get; set; } = null!;
public virtual DbSet<LibraryEntity> Libraries { get; set; } = null!;
/// <summary>
/// Initializes a new instance of the <see cref="LuminaDbContext"/> class.
@@ -43,6 +45,8 @@ public class LuminaDbContext : DbContext
base.OnModelCreating(modelBuilder);
}
// TODO: implement async versions of SaveChanges
/// <summary>
/// Overrides the SaveChanges method of the DbContext class to automatically set the Created and Updated properties of entities that implement the <see cref="IStorageEntity"/>.
/// </summary>
@@ -51,8 +55,8 @@ public class LuminaDbContext : DbContext
{
// get all the entity entries that are either added or modified
IEnumerable<EntityEntry> entries = ChangeTracker.Entries()
.Where(e => e.Entity is IStorageEntity &&
(e.State is EntityState.Added or EntityState.Modified));
.Where(entity => entity.Entity is IStorageEntity &&
(entity.State is EntityState.Added or EntityState.Modified));
foreach (EntityEntry? entityEntry in entries)
{
// if the entity is in Added state, set the Created property to the current date and time