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
Fixed some APIs that worked mins ago....something strange happening with EF relationships.
Joseph Milazzo14e8c3b8205d
diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.csindex 30b946739..d69e98b6e 100644--- a/API/Controllers/LibraryController.cs+++ b/API/Controllers/LibraryController.cs@@ -8,7 +8,6 @@ using API.Entities;using API.Extensions;using API.Interfaces;using AutoMapper;-using Hangfire;using Microsoft.AspNetCore.Authorization;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Logging;@@ -50,23 +49,26 @@ namespace API.Controllers{return BadRequest("Library name already exists. Please choose a unique name to the server.");}-- var admins = (await _unitOfWork.UserRepository.GetAdminUsersAsync()).ToList();var library = new Library{Name = createLibraryDto.Name,Type = createLibraryDto.Type,- AppUsers = admins,Folders = createLibraryDto.Folders.Select(x => new FolderPath {Path = x}).ToList()};- _unitOfWork.LibraryRepository.Update(library);- if (!await _unitOfWork.Complete())+ _unitOfWork.LibraryRepository.Add(library);++ var admins = (await _unitOfWork.UserRepository.GetAdminUsersAsync()).ToList();+ foreach (var admin in admins){- return BadRequest("There was a critical issue. Please try again.");+ admin.Libraries ??= new List<Library>();+ admin.Libraries.Add(library);}++ if (!await _unitOfWork.Complete()) return BadRequest("There was a critical issue. Please try again.");+_logger.LogInformation($"Created a new library: {library.Name}");_taskScheduler.ScanLibrary(library.Id);return Ok();@@ -158,13 +160,13 @@ namespace API.Controllers[HttpGet("series")]public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false){- // TODO: Move to series?+ int userId = 0;if (forUser){var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());- return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id));+ userId = user.Id;}- return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId));+ return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId));}[Authorize(Policy = "RequireAdminRole")]diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.csindex 37064489a..589362a12 100644--- a/API/Controllers/SeriesController.cs+++ b/API/Controllers/SeriesController.cs@@ -51,14 +51,10 @@ namespace API.Controllers}[HttpGet("volumes")]- public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId, bool forUser = true)+ public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId){- if (forUser)- {- var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());- return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id));- }- return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId)); // TODO: Refactor out forUser = false since everything is user based+ var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());+ return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id));}[HttpGet("volume")]diff --git a/API/Controllers/UsersController.cs b/API/Controllers/UsersController.csindex e199a1a44..6a98eedd9 100644--- a/API/Controllers/UsersController.cs+++ b/API/Controllers/UsersController.cs@@ -26,11 +26,8 @@ namespace API.Controllersvar user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username);_unitOfWork.UserRepository.Delete(user);- if (await _unitOfWork.Complete())- {- return Ok();- }-+ if (await _unitOfWork.Complete()) return Ok();+return BadRequest("Could not delete the user.");}@@ -44,14 +41,7 @@ namespace API.Controllers[HttpGet("has-library-access")]public async Task<ActionResult<bool>> HasLibraryAccess(int libraryId){- // TODO: refactor this to use either userexists or usermanager-- var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());-- if (user == null) return BadRequest("Could not validate user");-- var libs = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(user.UserName);-+ var libs = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(User.GetUsername());return Ok(libs.Any(x => x.Id == libraryId));}}diff --git a/API/Data/DataContext.cs b/API/Data/DataContext.csindex 2bb9424c0..97a1c32b8 100644--- a/API/Data/DataContext.cs+++ b/API/Data/DataContext.cs@@ -39,6 +39,11 @@ namespace API.Data.WithOne(u => u.Role).HasForeignKey(ur => ur.RoleId).IsRequired();+ // AppUsers have Libraries, not other way around+ builder.Entity<Library>()+ .HasMany(p => p.AppUsers)+ .WithMany(p => p.Libraries)+ .UsingEntity(j => j.ToTable("AppUserLibrary"));}void OnEntityTracked(object sender, EntityTrackedEventArgs e)diff --git a/API/Data/LibraryRepository.cs b/API/Data/LibraryRepository.csindex 5847430c4..54fc64fea 100644--- a/API/Data/LibraryRepository.cs+++ b/API/Data/LibraryRepository.cs@@ -20,6 +20,11 @@ namespace API.Data_context = context;_mapper = mapper;}++ public void Add(Library library)+ {+ _context.Library.Add(library);+ }public void Update(Library library){diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.csindex 2be26063d..060175c20 100644--- a/API/Data/SeriesRepository.cs+++ b/API/Data/SeriesRepository.cs@@ -1,4 +1,6 @@-using System.Collections.Generic;+using System;+using System.Collections.Generic;+using System.Diagnostics;using System.Linq;using System.Threading.Tasks;using API.DTOs;@@ -48,11 +50,24 @@ namespace API.Datapublic async Task<IEnumerable<SeriesDto>> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId = 0){+ // if (userId > 0)+ // {+ // return await _context.AppUserProgresses+ // .Include(p => p.Series)+ // .Where(p => p.AppUserId == userId && p.Series.LibraryId == libraryId)+ // .Select(p => p.Series)+ // .ProjectTo<SeriesDto>(_mapper.ConfigurationProvider)+ // //.Select(s => s.PagesRead = )+ // .ToListAsync();+ // }++ var sw = Stopwatch.StartNew();var series = await _context.Series.Where(s => s.LibraryId == libraryId).OrderBy(s => s.SortName).ProjectTo<SeriesDto>(_mapper.ConfigurationProvider).ToListAsync();+if (userId > 0){var userProgress = await _context.AppUserProgresses@@ -65,25 +80,26 @@ namespace API.Data}}+ Console.WriteLine("Processed GetSeriesDtoForLibraryIdAsync in {0} milliseconds", sw.ElapsedMilliseconds);return series;}- public async Task<IEnumerable<VolumeDto>> GetVolumesDtoAsync(int seriesId, int userId = 0)+ public async Task<IEnumerable<VolumeDto>> GetVolumesDtoAsync(int seriesId, int userId){var volumes = await _context.Volume.Where(vol => vol.SeriesId == seriesId).OrderBy(volume => volume.Number)- .ProjectTo<VolumeDto>(_mapper.ConfigurationProvider).ToListAsync();- if (userId > 0)- {- var userProgress = await _context.AppUserProgresses- .Where(p => p.AppUserId == userId && volumes.Select(s => s.Id).Contains(p.VolumeId))- .ToListAsync();+ .ProjectTo<VolumeDto>(_mapper.ConfigurationProvider)+ .AsNoTracking()+ .ToListAsync();+ var userProgress = await _context.AppUserProgresses+ .Where(p => p.AppUserId == userId && volumes.Select(s => s.Id).Contains(p.VolumeId))+ .AsNoTracking()+ .ToListAsync();- foreach (var v in volumes)- {- v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead);- }+ foreach (var v in volumes)+ {+ v.PagesRead = userProgress.Where(p => p.VolumeId == v.Id).Sum(p => p.PagesRead);}return volumes;diff --git a/API/Entities/AppUserProgress.cs b/API/Entities/AppUserProgress.csindex 5c8d2c05c..8097c5abf 100644--- a/API/Entities/AppUserProgress.cs+++ b/API/Entities/AppUserProgress.cs@@ -1,4 +1,6 @@-namespace API.Entities+using System.ComponentModel.DataAnnotations.Schema;++namespace API.Entities{/// <summary>/// Represents the progress a single user has on a given Volume.diff --git a/API/Entities/Library.cs b/API/Entities/Library.csindex 3c2129c37..9c93fa337 100644--- a/API/Entities/Library.cs+++ b/API/Entities/Library.cs@@ -1,5 +1,6 @@using System;using System.Collections.Generic;+using System.ComponentModel.DataAnnotations.Schema;using API.Entities.Interfaces;namespace API.Entitiesdiff --git a/API/Interfaces/ILibraryRepository.cs b/API/Interfaces/ILibraryRepository.csindex 6ebc478e3..1a0d3f778 100644--- a/API/Interfaces/ILibraryRepository.cs+++ b/API/Interfaces/ILibraryRepository.cs@@ -7,6 +7,7 @@ namespace API.Interfaces{public interface ILibraryRepository{+ void Add(Library library);void Update(Library library);Task<IEnumerable<LibraryDto>> GetLibraryDtosAsync();Task<bool> LibraryExists(string libraryName);diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.csindex d76dfd65b..e49e36c99 100644--- a/API/Services/DirectoryService.cs+++ b/API/Services/DirectoryService.cs@@ -257,6 +257,7 @@ namespace API.Serviceslibrary.Series = new List<Series>(); // Temp delete everything until we can mark items Unavailableforeach (var seriesKey in series.Keys){+ // TODO: Critical bug: Code is not taking libraryId into account and series are being linked across libraries.var mangaSeries = UpdateSeries(seriesKey, series[seriesKey].ToArray(), forceUpdate);_logger.LogInformation($"Created/Updated series {mangaSeries.Name}");library.Series.Add(mangaSeries);