Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.
C# annotationscbzcomicinfocomicscomics-readercross-platformcsharpepubepub-readerfreelinuxmangamedia-servermetadataopdsopds-feedpdfself-hostedubooquitywindows
Added ability to automatically track last modified and created timestamps for entities via an interface. DBContext will automatically update for us.
Joseph Milazzod632e53f18c0
diff --git a/API/Data/DataContext.cs b/API/Data/DataContext.csindex a83a76e9e..7a75ad138 100644--- a/API/Data/DataContext.cs+++ b/API/Data/DataContext.cs@@ -1,16 +1,21 @@-using API.Entities;+using System;+using API.Entities;+using API.Entities.Interfaces;using Microsoft.AspNetCore.Identity;using Microsoft.AspNetCore.Identity.EntityFrameworkCore;using Microsoft.EntityFrameworkCore;+using Microsoft.EntityFrameworkCore.ChangeTracking;namespace API.Data{- public class DataContext : IdentityDbContext<AppUser, AppRole, int,+ public sealed class DataContext : IdentityDbContext<AppUser, AppRole, int,IdentityUserClaim<int>, AppUserRole, IdentityUserLogin<int>,IdentityRoleClaim<int>, IdentityUserToken<int>>{public DataContext(DbContextOptions options) : base(options){+ ChangeTracker.Tracked += OnEntityTracked;+ ChangeTracker.StateChanged += OnEntityStateChanged;}public DbSet<Library> Library { get; set; }@@ -33,12 +38,18 @@ namespace API.Data.WithOne(u => u.Role).HasForeignKey(ur => ur.RoleId).IsRequired();-- // builder.Entity<Library>()- // .HasMany(s => s.Series)- // .WithOne(l => l.Library)- // .HasForeignKey(x => x.Id)-+ }++ void OnEntityTracked(object sender, EntityTrackedEventArgs e)+ {+ if (!e.FromQuery && e.Entry.State == EntityState.Added && e.Entry.Entity is IEntityDate entity)+ entity.Created = DateTime.Now;+ }++ void OnEntityStateChanged(object sender, EntityStateChangedEventArgs e)+ {+ if (e.NewState == EntityState.Modified && e.Entry.Entity is IEntityDate entity)+ entity.LastModified = DateTime.Now;}}}\ No newline at end of filediff --git a/API/Data/Migrations/20210102165536_EntityTimestamps.Designer.cs b/API/Data/Migrations/20210102165536_EntityTimestamps.Designer.csnew file mode 100644index 000000000..de4910b51--- /dev/null+++ b/API/Data/Migrations/20210102165536_EntityTimestamps.Designer.cs@@ -0,0 +1,509 @@+// <auto-generated />+using System;+using API.Data;+using Microsoft.EntityFrameworkCore;+using Microsoft.EntityFrameworkCore.Infrastructure;+using Microsoft.EntityFrameworkCore.Migrations;+using Microsoft.EntityFrameworkCore.Storage.ValueConversion;++namespace API.Data.Migrations+{+ [DbContext(typeof(DataContext))]+ [Migration("20210102165536_EntityTimestamps")]+ partial class EntityTimestamps+ {+ protected override void BuildTargetModel(ModelBuilder modelBuilder)+ {+#pragma warning disable 612, 618+ modelBuilder+ .HasAnnotation("ProductVersion", "5.0.1");++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedName")+ .IsUnique()+ .HasDatabaseName("RoleNameIndex");++ b.ToTable("AspNetRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("AccessFailedCount")+ .HasColumnType("INTEGER");++ b.Property<string>("ConcurrencyStamp")+ .IsConcurrencyToken()+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<string>("Email")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<bool>("EmailConfirmed")+ .HasColumnType("INTEGER");++ b.Property<bool>("IsAdmin")+ .HasColumnType("INTEGER");++ b.Property<DateTime>("LastActive")+ .HasColumnType("TEXT");++ b.Property<bool>("LockoutEnabled")+ .HasColumnType("INTEGER");++ b.Property<DateTimeOffset?>("LockoutEnd")+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedEmail")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("NormalizedUserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.Property<string>("PasswordHash")+ .HasColumnType("TEXT");++ b.Property<string>("PhoneNumber")+ .HasColumnType("TEXT");++ b.Property<bool>("PhoneNumberConfirmed")+ .HasColumnType("INTEGER");++ b.Property<uint>("RowVersion")+ .IsConcurrencyToken()+ .HasColumnType("INTEGER");++ b.Property<string>("SecurityStamp")+ .HasColumnType("TEXT");++ b.Property<bool>("TwoFactorEnabled")+ .HasColumnType("INTEGER");++ b.Property<string>("UserName")+ .HasMaxLength(256)+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("NormalizedEmail")+ .HasDatabaseName("EmailIndex");++ b.HasIndex("NormalizedUserName")+ .IsUnique()+ .HasDatabaseName("UserNameIndex");++ b.ToTable("AspNetUsers");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("UserId", "RoleId");++ b.HasIndex("RoleId");++ b.ToTable("AspNetUserRoles");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("Path")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.ToTable("FolderPath");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<int>("Type")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.ToTable("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("FilePath")+ .HasColumnType("TEXT");++ b.Property<int>("VolumeId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("VolumeId");++ b.ToTable("MangaFile");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("CoverImage")+ .HasColumnType("TEXT");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<int>("LibraryId")+ .HasColumnType("INTEGER");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("OriginalName")+ .HasColumnType("TEXT");++ b.Property<string>("SortName")+ .HasColumnType("TEXT");++ b.Property<string>("Summary")+ .HasColumnType("TEXT");++ b.HasKey("Id");++ b.HasIndex("LibraryId");++ b.ToTable("Series");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");++ b.Property<string>("Number")+ .HasColumnType("TEXT");++ b.Property<int>("SeriesId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("SeriesId");++ b.ToTable("Volume");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.Property<int>("AppUsersId")+ .HasColumnType("INTEGER");++ b.Property<int>("LibrariesId")+ .HasColumnType("INTEGER");++ b.HasKey("AppUsersId", "LibrariesId");++ b.HasIndex("LibrariesId");++ b.ToTable("AppUserLibrary");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("RoleId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("RoleId");++ b.ToTable("AspNetRoleClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.Property<int>("Id")+ .ValueGeneratedOnAdd()+ .HasColumnType("INTEGER");++ b.Property<string>("ClaimType")+ .HasColumnType("TEXT");++ b.Property<string>("ClaimValue")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("Id");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserClaims");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderKey")+ .HasColumnType("TEXT");++ b.Property<string>("ProviderDisplayName")+ .HasColumnType("TEXT");++ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.HasKey("LoginProvider", "ProviderKey");++ b.HasIndex("UserId");++ b.ToTable("AspNetUserLogins");+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.Property<int>("UserId")+ .HasColumnType("INTEGER");++ b.Property<string>("LoginProvider")+ .HasColumnType("TEXT");++ b.Property<string>("Name")+ .HasColumnType("TEXT");++ b.Property<string>("Value")+ .HasColumnType("TEXT");++ b.HasKey("UserId", "LoginProvider", "Name");++ b.ToTable("AspNetUserTokens");+ });++ modelBuilder.Entity("API.Entities.AppUserRole", b =>+ {+ b.HasOne("API.Entities.AppRole", "Role")+ .WithMany("UserRoles")+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.AppUser", "User")+ .WithMany("UserRoles")+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Role");++ b.Navigation("User");+ });++ modelBuilder.Entity("API.Entities.FolderPath", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Folders")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.MangaFile", b =>+ {+ b.HasOne("API.Entities.Volume", "Volume")+ .WithMany("Files")+ .HasForeignKey("VolumeId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Volume");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.HasOne("API.Entities.Library", "Library")+ .WithMany("Series")+ .HasForeignKey("LibraryId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Library");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.HasOne("API.Entities.Series", "Series")+ .WithMany("Volumes")+ .HasForeignKey("SeriesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.Navigation("Series");+ });++ modelBuilder.Entity("AppUserLibrary", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("AppUsersId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();++ b.HasOne("API.Entities.Library", null)+ .WithMany()+ .HasForeignKey("LibrariesId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityRoleClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppRole", null)+ .WithMany()+ .HasForeignKey("RoleId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserClaim<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserLogin<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("Microsoft.AspNetCore.Identity.IdentityUserToken<int>", b =>+ {+ b.HasOne("API.Entities.AppUser", null)+ .WithMany()+ .HasForeignKey("UserId")+ .OnDelete(DeleteBehavior.Cascade)+ .IsRequired();+ });++ modelBuilder.Entity("API.Entities.AppRole", b =>+ {+ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.AppUser", b =>+ {+ b.Navigation("UserRoles");+ });++ modelBuilder.Entity("API.Entities.Library", b =>+ {+ b.Navigation("Folders");++ b.Navigation("Series");+ });++ modelBuilder.Entity("API.Entities.Series", b =>+ {+ b.Navigation("Volumes");+ });++ modelBuilder.Entity("API.Entities.Volume", b =>+ {+ b.Navigation("Files");+ });+#pragma warning restore 612, 618+ }+ }+}diff --git a/API/Data/Migrations/20210102165536_EntityTimestamps.cs b/API/Data/Migrations/20210102165536_EntityTimestamps.csnew file mode 100644index 000000000..2ed6041f0--- /dev/null+++ b/API/Data/Migrations/20210102165536_EntityTimestamps.cs@@ -0,0 +1,80 @@+using System;+using Microsoft.EntityFrameworkCore.Migrations;++namespace API.Data.Migrations+{+ public partial class EntityTimestamps : Migration+ {+ protected override void Up(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.AddColumn<DateTime>(+ name: "Created",+ table: "Volume",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));++ migrationBuilder.AddColumn<DateTime>(+ name: "LastModified",+ table: "Volume",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));++ migrationBuilder.AddColumn<DateTime>(+ name: "Created",+ table: "Series",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));++ migrationBuilder.AddColumn<DateTime>(+ name: "LastModified",+ table: "Series",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));++ migrationBuilder.AddColumn<DateTime>(+ name: "Created",+ table: "Library",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));++ migrationBuilder.AddColumn<DateTime>(+ name: "LastModified",+ table: "Library",+ type: "TEXT",+ nullable: false,+ defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));+ }++ protected override void Down(MigrationBuilder migrationBuilder)+ {+ migrationBuilder.DropColumn(+ name: "Created",+ table: "Volume");++ migrationBuilder.DropColumn(+ name: "LastModified",+ table: "Volume");++ migrationBuilder.DropColumn(+ name: "Created",+ table: "Series");++ migrationBuilder.DropColumn(+ name: "LastModified",+ table: "Series");++ migrationBuilder.DropColumn(+ name: "Created",+ table: "Library");++ migrationBuilder.DropColumn(+ name: "LastModified",+ table: "Library");+ }+ }+}diff --git a/API/Data/Migrations/DataContextModelSnapshot.cs b/API/Data/Migrations/DataContextModelSnapshot.csindex 110c60774..f840250c9 100644--- a/API/Data/Migrations/DataContextModelSnapshot.cs+++ b/API/Data/Migrations/DataContextModelSnapshot.cs@@ -164,6 +164,12 @@ namespace API.Data.Migrationsb.Property<string>("CoverImage").HasColumnType("TEXT");+ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");+b.Property<string>("Name").HasColumnType("TEXT");@@ -203,6 +209,12 @@ namespace API.Data.Migrationsb.Property<string>("CoverImage").HasColumnType("TEXT");+ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");+b.Property<int>("LibraryId").HasColumnType("INTEGER");@@ -231,6 +243,12 @@ namespace API.Data.Migrations.ValueGeneratedOnAdd().HasColumnType("INTEGER");+ b.Property<DateTime>("Created")+ .HasColumnType("TEXT");++ b.Property<DateTime>("LastModified")+ .HasColumnType("TEXT");+b.Property<string>("Number").HasColumnType("TEXT");diff --git a/API/Entities/FolderPath.cs b/API/Entities/FolderPath.csindex d1e49180f..84d3ea798 100644--- a/API/Entities/FolderPath.cs+++ b/API/Entities/FolderPath.cs@@ -1,4 +1,5 @@-namespace API.Entities++namespace API.Entities{public class FolderPath{diff --git a/API/Entities/Interfaces/IEntityDate.cs b/API/Entities/Interfaces/IEntityDate.csnew file mode 100644index 000000000..79330546e--- /dev/null+++ b/API/Entities/Interfaces/IEntityDate.cs@@ -0,0 +1,10 @@+using System;++namespace API.Entities.Interfaces+{+ public interface IEntityDate+ {+ DateTime Created { get; set; }+ DateTime LastModified { get; set; }+ }+}\ No newline at end of filediff --git a/API/Entities/Library.cs b/API/Entities/Library.csindex 53b3073a7..3c2129c37 100644--- a/API/Entities/Library.cs+++ b/API/Entities/Library.cs@@ -1,15 +1,20 @@-using System.Collections.Generic;+using System;+using System.Collections.Generic;+using API.Entities.Interfaces;namespace API.Entities{- public class Library+ public class Library : IEntityDate{public int Id { get; set; }public string Name { get; set; }public string CoverImage { get; set; }public LibraryType Type { get; set; }+ public DateTime Created { get; set; }+ public DateTime LastModified { get; set; }public ICollection<FolderPath> Folders { get; set; }public ICollection<AppUser> AppUsers { get; set; }public ICollection<Series> Series { get; set; }+}}\ No newline at end of filediff --git a/API/Entities/MangaFile.cs b/API/Entities/MangaFile.csindex 685d399e9..06e132193 100644--- a/API/Entities/MangaFile.cs+++ b/API/Entities/MangaFile.cs@@ -1,4 +1,5 @@-namespace API.Entities++namespace API.Entities{public class MangaFile{diff --git a/API/Entities/Series.cs b/API/Entities/Series.csindex dd47e528d..f20fc8658 100644--- a/API/Entities/Series.cs+++ b/API/Entities/Series.cs@@ -1,8 +1,10 @@-using System.Collections.Generic;+using System;+using System.Collections.Generic;+using API.Entities.Interfaces;namespace API.Entities{- public class Series+ public class Series : IEntityDate{public int Id { get; set; }/// <summary>@@ -22,6 +24,8 @@ namespace API.Entities/// </summary>public string Summary { get; set; }public string CoverImage { get; set; }+ public DateTime Created { get; set; }+ public DateTime LastModified { get; set; }public ICollection<Volume> Volumes { get; set; }public Library Library { get; set; }diff --git a/API/Entities/Volume.cs b/API/Entities/Volume.csindex bb3638323..80d1f6573 100644--- a/API/Entities/Volume.cs+++ b/API/Entities/Volume.cs@@ -1,12 +1,16 @@-using System.Collections.Generic;+using System;+using System.Collections.Generic;+using API.Entities.Interfaces;namespace API.Entities{- public class Volume+ public class Volume : IEntityDate{public int Id { get; set; }public string Number { get; set; }public ICollection<MangaFile> Files { get; set; }+ public DateTime Created { get; set; }+ public DateTime LastModified { get; set; }// Many-to-Many relationshipspublic Series Series { get; set; }diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.csindex de1cf994b..f8ee643ce 100644--- a/API/Services/DirectoryService.cs+++ b/API/Services/DirectoryService.cs@@ -223,7 +223,7 @@ namespace API.Servicesforeach (var seriesKey in series.Keys){var s = UpdateSeries(seriesKey, series[seriesKey].ToArray());- Console.WriteLine($"Created/Updated series {s.Name}");+ _logger.LogInformation($"Created/Updated series {s.Name}");libraryEntity.Series.Add(s);}