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")]
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false)
public async Task<ActionResult<IEnumerable<Series>>> 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")]

View File

@ -40,10 +40,10 @@ namespace API.Data
.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"));
// builder.Entity<Library>()
// .HasMany(p => p.AppUsers)
// .WithMany(p => p.Libraries)
// .UsingEntity(j => j.ToTable("AppUserLibrary"));
}
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.Threading.Tasks;
using API.DTOs;
@ -33,13 +35,15 @@ namespace API.Data
public async Task<IEnumerable<LibraryDto>> 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<LibraryDto>(_mapper.ConfigurationProvider)
.AsNoTracking()
.ToListAsync();
Console.WriteLine("Processed GetLibraryDtosForUsernameAsync in {0} milliseconds", sw.ElapsedMilliseconds);
return libs;
}
public async Task<IEnumerable<Library>> GetLibrariesAsync()

View File

@ -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;

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="searchOption">SearchOption to use, defaults to TopDirectoryOnly</param>
/// <returns>List of file paths</returns>
public static IEnumerable<string> GetFiles(string path,
string searchPatternExpression = "*",
public static IEnumerable<string> GetFilesWithCertainExtensions(string path,
string searchPatternExpression = "",
SearchOption searchOption = SearchOption.TopDirectoryOnly)
{
if (!Directory.Exists(path)) return ImmutableList<string>.Empty;
@ -50,6 +50,14 @@ namespace API.Services
.Where(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)
{
@ -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) {