mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-06-21 22:40:34 -04:00
* Introduced a new claim on the Token to get UserId as well as Username, thus allowing for many places of reduced DB calls. All users will need to reauthenticate. Introduced UTC Dates throughout the application, they are not exposed in all DTOs, that will come later when we fully switch over. For now, Utc dates will be updated along side timezone specific dates. Refactored get-progress/progress api to be 50% faster by reducing how much data is loaded from the query. * Speed up the following apis: collection/search, download/bookmarks, reader/bookmark-info, recommended/quick-reads, recommended/quick-catchup-reads, recommended/highly-rated, recommended/more-in, recommended/rediscover, want-to-read/ * Added a migration to sync all dates with their new UTC counterpart. * Added LastReadingProgressUtc onto ChapterDto for some browsing apis, but not all. Added LastReadingProgressUtc to reading list items. Refactored the migration to run raw SQL which is much faster. * Added LastReadingProgressUtc onto ChapterDto for some browsing apis, but not all. Added LastReadingProgressUtc to reading list items. Refactored the migration to run raw SQL which is much faster. * Fixed the unit tests * Fixed an issue with auto mapper which was causing progress page number to not get sent to UI * series/volume has chapter last reading progress * Added filesize and library name on reading list item dto for CDisplayEx. * Some minor code cleanup * Forgot to fill a field
173 lines
5.7 KiB
C#
173 lines
5.7 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using API.Entities;
|
|
using API.Entities.Enums.UserPreferences;
|
|
using API.Entities.Interfaces;
|
|
using API.Entities.Metadata;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
|
|
|
namespace API.Data;
|
|
|
|
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; }
|
|
public DbSet<Series> Series { get; set; }
|
|
public DbSet<Chapter> Chapter { get; set; }
|
|
public DbSet<Volume> Volume { get; set; }
|
|
public DbSet<AppUser> AppUser { get; set; }
|
|
public DbSet<MangaFile> MangaFile { get; set; }
|
|
public DbSet<AppUserProgress> AppUserProgresses { get; set; }
|
|
public DbSet<AppUserRating> AppUserRating { get; set; }
|
|
public DbSet<ServerSetting> ServerSetting { get; set; }
|
|
public DbSet<AppUserPreferences> AppUserPreferences { get; set; }
|
|
public DbSet<SeriesMetadata> SeriesMetadata { get; set; }
|
|
public DbSet<CollectionTag> CollectionTag { get; set; }
|
|
public DbSet<AppUserBookmark> AppUserBookmark { get; set; }
|
|
public DbSet<ReadingList> ReadingList { get; set; }
|
|
public DbSet<ReadingListItem> ReadingListItem { get; set; }
|
|
public DbSet<Person> Person { get; set; }
|
|
public DbSet<Genre> Genre { get; set; }
|
|
public DbSet<Tag> Tag { get; set; }
|
|
public DbSet<SiteTheme> SiteTheme { get; set; }
|
|
public DbSet<SeriesRelation> SeriesRelation { get; set; }
|
|
public DbSet<FolderPath> FolderPath { get; set; }
|
|
public DbSet<Device> Device { get; set; }
|
|
public DbSet<ServerStatistics> ServerStatistics { get; set; }
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder builder)
|
|
{
|
|
base.OnModelCreating(builder);
|
|
|
|
|
|
builder.Entity<AppUser>()
|
|
.HasMany(ur => ur.UserRoles)
|
|
.WithOne(u => u.User)
|
|
.HasForeignKey(ur => ur.UserId)
|
|
.IsRequired();
|
|
|
|
builder.Entity<AppRole>()
|
|
.HasMany(ur => ur.UserRoles)
|
|
.WithOne(u => u.Role)
|
|
.HasForeignKey(ur => ur.RoleId)
|
|
.IsRequired();
|
|
|
|
builder.Entity<SeriesRelation>()
|
|
.HasOne(pt => pt.Series)
|
|
.WithMany(p => p.Relations)
|
|
.HasForeignKey(pt => pt.SeriesId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
builder.Entity<SeriesRelation>()
|
|
.HasOne(pt => pt.TargetSeries)
|
|
.WithMany(t => t.RelationOf)
|
|
.HasForeignKey(pt => pt.TargetSeriesId)
|
|
.OnDelete(DeleteBehavior.Cascade);
|
|
|
|
|
|
|
|
builder.Entity<AppUserPreferences>()
|
|
.Property(b => b.BookThemeName)
|
|
.HasDefaultValue("Dark");
|
|
builder.Entity<AppUserPreferences>()
|
|
.Property(b => b.BackgroundColor)
|
|
.HasDefaultValue("#000000");
|
|
|
|
builder.Entity<AppUserPreferences>()
|
|
.Property(b => b.GlobalPageLayoutMode)
|
|
.HasDefaultValue(PageLayoutMode.Cards);
|
|
|
|
|
|
builder.Entity<Library>()
|
|
.Property(b => b.FolderWatching)
|
|
.HasDefaultValue(true);
|
|
builder.Entity<Library>()
|
|
.Property(b => b.IncludeInDashboard)
|
|
.HasDefaultValue(true);
|
|
builder.Entity<Library>()
|
|
.Property(b => b.IncludeInRecommended)
|
|
.HasDefaultValue(true);
|
|
builder.Entity<Library>()
|
|
.Property(b => b.IncludeInSearch)
|
|
.HasDefaultValue(true);
|
|
builder.Entity<Library>()
|
|
.Property(b => b.ManageCollections)
|
|
.HasDefaultValue(true);
|
|
}
|
|
|
|
|
|
private static void OnEntityTracked(object sender, EntityTrackedEventArgs e)
|
|
{
|
|
if (e.FromQuery || e.Entry.State != EntityState.Added || e.Entry.Entity is not IEntityDate entity) return;
|
|
|
|
entity.Created = DateTime.Now;
|
|
entity.LastModified = DateTime.Now;
|
|
entity.CreatedUtc = DateTime.UtcNow;
|
|
entity.LastModifiedUtc = DateTime.UtcNow;
|
|
}
|
|
|
|
private static void OnEntityStateChanged(object sender, EntityStateChangedEventArgs e)
|
|
{
|
|
if (e.NewState != EntityState.Modified || e.Entry.Entity is not IEntityDate entity) return;
|
|
entity.LastModified = DateTime.Now;
|
|
entity.LastModifiedUtc = DateTime.UtcNow;
|
|
}
|
|
|
|
private void OnSaveChanges()
|
|
{
|
|
foreach (var saveEntity in ChangeTracker.Entries()
|
|
.Where(e => e.State == EntityState.Modified)
|
|
.Select(entry => entry.Entity)
|
|
.OfType<IHasConcurrencyToken>())
|
|
{
|
|
saveEntity.OnSavingChanges();
|
|
}
|
|
}
|
|
|
|
#region SaveChanges overrides
|
|
|
|
public override int SaveChanges()
|
|
{
|
|
this.OnSaveChanges();
|
|
|
|
return base.SaveChanges();
|
|
}
|
|
|
|
public override int SaveChanges(bool acceptAllChangesOnSuccess)
|
|
{
|
|
this.OnSaveChanges();
|
|
|
|
return base.SaveChanges(acceptAllChangesOnSuccess);
|
|
}
|
|
|
|
public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default(CancellationToken))
|
|
{
|
|
this.OnSaveChanges();
|
|
|
|
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
|
|
}
|
|
|
|
public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default(CancellationToken))
|
|
{
|
|
this.OnSaveChanges();
|
|
|
|
return base.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
#endregion
|
|
}
|