Fixed offset bug in GetCachedPagePath for if you've read just one page. Fixed a bad refactor for getting files.

This commit is contained in:
Joseph Milazzo 2021-01-19 12:06:45 -06:00
parent 14e8c3b820
commit c75feb03e1
5 changed files with 26 additions and 19 deletions

View File

@ -158,15 +158,10 @@ namespace API.Controllers
} }
[HttpGet("series")] [HttpGet("series")]
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false) public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId)
{ {
int userId = 0; var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
if (forUser) return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id));
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
userId = user.Id;
}
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId));
} }
[Authorize(Policy = "RequireAdminRole")] [Authorize(Policy = "RequireAdminRole")]

View File

@ -40,10 +40,10 @@ namespace API.Data
.HasForeignKey(ur => ur.RoleId) .HasForeignKey(ur => ur.RoleId)
.IsRequired(); .IsRequired();
// AppUsers have Libraries, not other way around // AppUsers have Libraries, not other way around
builder.Entity<Library>() // builder.Entity<Library>()
.HasMany(p => p.AppUsers) // .HasMany(p => p.AppUsers)
.WithMany(p => p.Libraries) // .WithMany(p => p.Libraries)
.UsingEntity(j => j.ToTable("AppUserLibrary")); // .UsingEntity(j => j.ToTable("AppUserLibrary"));
} }
void OnEntityTracked(object sender, EntityTrackedEventArgs e) void OnEntityTracked(object sender, EntityTrackedEventArgs e)

View File

@ -1,4 +1,6 @@
using System.Collections.Generic; using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using API.DTOs; using API.DTOs;
@ -33,13 +35,15 @@ namespace API.Data
public async Task<IEnumerable<LibraryDto>> GetLibraryDtosForUsernameAsync(string userName) public async Task<IEnumerable<LibraryDto>> GetLibraryDtosForUsernameAsync(string userName)
{ {
// TODO: Speed this query up Stopwatch sw = Stopwatch.StartNew();
return await _context.Library var libs = await _context.Library
.Include(l => l.AppUsers) .Include(l => l.AppUsers)
.Where(library => library.AppUsers.Any(x => x.UserName == userName)) .Where(library => library.AppUsers.Any(x => x.UserName == userName))
.ProjectTo<LibraryDto>(_mapper.ConfigurationProvider) .ProjectTo<LibraryDto>(_mapper.ConfigurationProvider)
.AsNoTracking() .AsNoTracking()
.ToListAsync(); .ToListAsync();
Console.WriteLine("Processed GetLibraryDtosForUsernameAsync in {0} milliseconds", sw.ElapsedMilliseconds);
return libs;
} }
public async Task<IEnumerable<Library>> GetLibrariesAsync() public async Task<IEnumerable<Library>> GetLibrariesAsync()

View File

@ -113,7 +113,7 @@ namespace API.Services
var array = files.ToArray(); var array = files.ToArray();
Array.Sort(array, _numericComparer); // TODO: Find a way to apply numericComparer to IList. 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; pagesSoFar += mangaFile.NumberOfPages;

View File

@ -40,8 +40,8 @@ namespace API.Services
/// <param name="searchPatternExpression">Regex version of search pattern (ie \.mp3|\.mp4). Defaults to * meaning all files.</param> /// <param name="searchPatternExpression">Regex version of search pattern (ie \.mp3|\.mp4). Defaults to * meaning all files.</param>
/// <param name="searchOption">SearchOption to use, defaults to TopDirectoryOnly</param> /// <param name="searchOption">SearchOption to use, defaults to TopDirectoryOnly</param>
/// <returns>List of file paths</returns> /// <returns>List of file paths</returns>
public static IEnumerable<string> GetFiles(string path, public static IEnumerable<string> GetFilesWithCertainExtensions(string path,
string searchPatternExpression = "*", string searchPatternExpression = "",
SearchOption searchOption = SearchOption.TopDirectoryOnly) SearchOption searchOption = SearchOption.TopDirectoryOnly)
{ {
if (!Directory.Exists(path)) return ImmutableList<string>.Empty; if (!Directory.Exists(path)) return ImmutableList<string>.Empty;
@ -50,6 +50,14 @@ namespace API.Services
.Where(file => .Where(file =>
reSearchPattern.IsMatch(Path.GetExtension(file))); reSearchPattern.IsMatch(Path.GetExtension(file)));
} }
public static IList<string> GetFiles(string path)
{
if (!Directory.Exists(path)) return ImmutableList<string>.Empty;
return Directory.GetFiles(path);
}
public IEnumerable<string> ListDirectory(string rootPath) public IEnumerable<string> ListDirectory(string rootPath)
{ {
@ -384,7 +392,7 @@ namespace API.Services
} }
try { try {
files = DirectoryService.GetFiles(currentDir, Parser.Parser.MangaFileExtensions) files = DirectoryService.GetFilesWithCertainExtensions(currentDir, Parser.Parser.MangaFileExtensions)
.ToArray(); .ToArray();
} }
catch (UnauthorizedAccessException e) { catch (UnauthorizedAccessException e) {