diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index d69e98b6e..acf0ce24c 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -158,15 +158,10 @@ namespace API.Controllers } [HttpGet("series")] - public async Task>> GetSeriesForLibrary(int libraryId, bool forUser = false) + public async Task>> GetSeriesForLibrary(int libraryId) { - int userId = 0; - if (forUser) - { - var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); - userId = user.Id; - } - return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId)); + var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); + return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id)); } [Authorize(Policy = "RequireAdminRole")] diff --git a/API/Data/DataContext.cs b/API/Data/DataContext.cs index 97a1c32b8..09a53f0cd 100644 --- a/API/Data/DataContext.cs +++ b/API/Data/DataContext.cs @@ -40,10 +40,10 @@ namespace API.Data .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")); + // 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 54fc64fea..ef21ffd55 100644 --- a/API/Data/LibraryRepository.cs +++ b/API/Data/LibraryRepository.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; @@ -33,13 +35,15 @@ namespace API.Data public async Task> GetLibraryDtosForUsernameAsync(string userName) { - // TODO: Speed this query up - return await _context.Library + Stopwatch sw = Stopwatch.StartNew(); + var libs = await _context.Library .Include(l => l.AppUsers) .Where(library => library.AppUsers.Any(x => x.UserName == userName)) .ProjectTo(_mapper.ConfigurationProvider) .AsNoTracking() .ToListAsync(); + Console.WriteLine("Processed GetLibraryDtosForUsernameAsync in {0} milliseconds", sw.ElapsedMilliseconds); + return libs; } public async Task> GetLibrariesAsync() diff --git a/API/Services/CacheService.cs b/API/Services/CacheService.cs index 3572d7567..0c69abae8 100644 --- a/API/Services/CacheService.cs +++ b/API/Services/CacheService.cs @@ -113,7 +113,7 @@ namespace API.Services var array = files.ToArray(); Array.Sort(array, _numericComparer); // TODO: Find a way to apply numericComparer to IList. - return array.ElementAt((page + 1) - pagesSoFar); + return array.ElementAt(page - pagesSoFar); } pagesSoFar += mangaFile.NumberOfPages; diff --git a/API/Services/DirectoryService.cs b/API/Services/DirectoryService.cs index e49e36c99..25c6349c9 100644 --- a/API/Services/DirectoryService.cs +++ b/API/Services/DirectoryService.cs @@ -40,8 +40,8 @@ namespace API.Services /// Regex version of search pattern (ie \.mp3|\.mp4). Defaults to * meaning all files. /// SearchOption to use, defaults to TopDirectoryOnly /// List of file paths - public static IEnumerable GetFiles(string path, - string searchPatternExpression = "*", + public static IEnumerable GetFilesWithCertainExtensions(string path, + string searchPatternExpression = "", SearchOption searchOption = SearchOption.TopDirectoryOnly) { if (!Directory.Exists(path)) return ImmutableList.Empty; @@ -50,6 +50,14 @@ namespace API.Services .Where(file => reSearchPattern.IsMatch(Path.GetExtension(file))); } + + public static IList GetFiles(string path) + { + if (!Directory.Exists(path)) return ImmutableList.Empty; + return Directory.GetFiles(path); + } + + public IEnumerable ListDirectory(string rootPath) { @@ -384,7 +392,7 @@ namespace API.Services } try { - files = DirectoryService.GetFiles(currentDir, Parser.Parser.MangaFileExtensions) + files = DirectoryService.GetFilesWithCertainExtensions(currentDir, Parser.Parser.MangaFileExtensions) .ToArray(); } catch (UnauthorizedAccessException e) {