diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index 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(); + 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>> 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.cs index 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>> GetVolumes(int seriesId, bool forUser = true) + public async Task>> 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.cs index e199a1a44..6a98eedd9 100644 --- a/API/Controllers/UsersController.cs +++ b/API/Controllers/UsersController.cs @@ -26,11 +26,8 @@ namespace API.Controllers var 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> 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.cs index 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() + .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.cs index 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.cs index 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.Data public async Task> 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(_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(_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> GetVolumesDtoAsync(int seriesId, int userId = 0) + public async Task> GetVolumesDtoAsync(int seriesId, int userId) { var volumes = await _context.Volume .Where(vol => vol.SeriesId == seriesId) .OrderBy(volume => volume.Number) - .ProjectTo(_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(_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.cs index 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 { /// /// Represents the progress a single user has on a given Volume. diff --git a/API/Entities/Library.cs b/API/Entities/Library.cs index 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.Entities diff --git a/API/Interfaces/ILibraryRepository.cs b/API/Interfaces/ILibraryRepository.cs index 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> GetLibraryDtosAsync(); Task LibraryExists(string libraryName); diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs index d76dfd65b..e49e36c99 100644 --- a/API/Services/DirectoryService.cs +++ b/API/Services/DirectoryService.cs @@ -257,6 +257,7 @@ namespace API.Services library.Series = new List(); // Temp delete everything until we can mark items Unavailable foreach (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);