mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Fixed some APIs that worked mins ago....something strange happening with EF relationships.
This commit is contained in:
parent
295e62d773
commit
14e8c3b820
@ -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")]
|
||||
|
@ -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")]
|
||||
|
@ -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<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));
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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<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;
|
||||
|
@ -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.
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using API.Entities.Interfaces;
|
||||
|
||||
namespace API.Entities
|
||||
|
@ -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);
|
||||
|
@ -257,6 +257,7 @@ namespace API.Services
|
||||
library.Series = new List<Series>(); // 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);
|
||||
|
Loading…
x
Reference in New Issue
Block a user