mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-07-09 03:04:19 -04:00
Fixed grant-access api and new library to properly update the db. Somehow the old way of updating db no longer works.
This commit is contained in:
parent
80283bcd49
commit
295e62d773
@ -1,4 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
@ -60,14 +61,7 @@ namespace API.Controllers
|
||||
Folders = createLibraryDto.Folders.Select(x => new FolderPath {Path = x}).ToList()
|
||||
};
|
||||
|
||||
foreach (var admin in admins)
|
||||
{
|
||||
// If user is null, then set it
|
||||
admin.Libraries ??= new List<Library>();
|
||||
admin.Libraries.Add(library);
|
||||
}
|
||||
|
||||
|
||||
_unitOfWork.LibraryRepository.Update(library);
|
||||
if (!await _unitOfWork.Complete())
|
||||
{
|
||||
return BadRequest("There was a critical issue. Please try again.");
|
||||
@ -87,7 +81,6 @@ namespace API.Controllers
|
||||
[HttpGet("list")]
|
||||
public ActionResult<IEnumerable<string>> GetDirectories(string path)
|
||||
{
|
||||
// TODO: Move this to another controller.
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
return Ok(Directory.GetLogicalDrives());
|
||||
@ -105,26 +98,46 @@ namespace API.Controllers
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpPut("grant-access")]
|
||||
public async Task<ActionResult<MemberDto>> AddLibraryToUser(UpdateLibraryForUserDto updateLibraryForUserDto)
|
||||
[HttpPost("grant-access")]
|
||||
public async Task<ActionResult<MemberDto>> UpdateUserLibraries(UpdateLibraryForUserDto updateLibraryForUserDto)
|
||||
{
|
||||
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(updateLibraryForUserDto.Username);
|
||||
|
||||
if (user == null) return BadRequest("Could not validate user");
|
||||
|
||||
user.Libraries = new List<Library>();
|
||||
|
||||
foreach (var selectedLibrary in updateLibraryForUserDto.SelectedLibraries)
|
||||
|
||||
var libraryString = String.Join(",", updateLibraryForUserDto.SelectedLibraries.Select(x => x.Name));
|
||||
_logger.LogInformation($"Granting user {updateLibraryForUserDto.Username} access to: {libraryString}");
|
||||
|
||||
var allLibraries = await _unitOfWork.LibraryRepository.GetLibrariesAsync();
|
||||
foreach (var library in allLibraries)
|
||||
{
|
||||
user.Libraries.Add(_mapper.Map<Library>(selectedLibrary));
|
||||
library.AppUsers ??= new List<AppUser>();
|
||||
var libraryContainsUser = library.AppUsers.Any(u => u.UserName == user.UserName);
|
||||
var libraryIsSelected = updateLibraryForUserDto.SelectedLibraries.Any(l => l.Id == library.Id);
|
||||
if (libraryContainsUser && !libraryIsSelected)
|
||||
{
|
||||
// Remove
|
||||
library.AppUsers.Remove(user);
|
||||
}
|
||||
else if (!libraryContainsUser && libraryIsSelected)
|
||||
{
|
||||
library.AppUsers.Add(user);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (!_unitOfWork.HasChanges())
|
||||
{
|
||||
_logger.LogInformation($"Added: {updateLibraryForUserDto.SelectedLibraries} to {updateLibraryForUserDto.Username}");
|
||||
return Ok(_mapper.Map<MemberDto>(user));
|
||||
}
|
||||
|
||||
if (await _unitOfWork.Complete())
|
||||
{
|
||||
_logger.LogInformation($"Added: {updateLibraryForUserDto.SelectedLibraries} to {updateLibraryForUserDto.Username}");
|
||||
return Ok(user);
|
||||
return Ok(_mapper.Map<MemberDto>(user));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return BadRequest("There was a critical issue. Please try again.");
|
||||
}
|
||||
|
||||
@ -132,15 +145,14 @@ namespace API.Controllers
|
||||
[HttpPost("scan")]
|
||||
public ActionResult Scan(int libraryId)
|
||||
{
|
||||
//BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(libraryId, true));
|
||||
_taskScheduler.ScanLibrary(libraryId, true);
|
||||
return Ok();
|
||||
}
|
||||
|
||||
[HttpGet("libraries-for")]
|
||||
public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser(string username)
|
||||
[HttpGet("libraries")]
|
||||
public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser()
|
||||
{
|
||||
return Ok(await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(username));
|
||||
return Ok(await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(User.GetUsername()));
|
||||
}
|
||||
|
||||
[HttpGet("series")]
|
||||
|
@ -36,6 +36,13 @@ namespace API.Data
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Library>> GetLibrariesAsync()
|
||||
{
|
||||
return await _context.Library
|
||||
.Include(l => l.AppUsers)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<Library> GetLibraryForNameAsync(string libraryName)
|
||||
{
|
||||
|
@ -32,27 +32,9 @@ namespace API.Data
|
||||
|
||||
public void Delete(AppUser user)
|
||||
{
|
||||
// TODO: Check how to implement for _userMangaer
|
||||
_context.AppUser.Remove(user);
|
||||
}
|
||||
|
||||
public async Task<bool> SaveAllAsync()
|
||||
{
|
||||
return await _context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<AppUser>> GetUsersAsync()
|
||||
{
|
||||
return await _userManager.Users.ToListAsync();
|
||||
//return await _context.Users.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<AppUser> GetUserByIdAsync(int id)
|
||||
{
|
||||
// TODO: How to use userManager
|
||||
return await _context.AppUser.FindAsync(id);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets an AppUser by username. Returns back Progress information.
|
||||
/// </summary>
|
||||
@ -60,7 +42,7 @@ namespace API.Data
|
||||
/// <returns></returns>
|
||||
public async Task<AppUser> GetUserByUsernameAsync(string username)
|
||||
{
|
||||
return await _userManager.Users
|
||||
return await _context.Users
|
||||
.Include(u => u.Progresses)
|
||||
.SingleOrDefaultAsync(x => x.UserName == username);
|
||||
}
|
||||
@ -72,7 +54,7 @@ namespace API.Data
|
||||
|
||||
public async Task<IEnumerable<MemberDto>> GetMembersAsync()
|
||||
{
|
||||
return await _userManager.Users
|
||||
return await _context.Users
|
||||
.Include(x => x.Libraries)
|
||||
.Include(r => r.UserRoles)
|
||||
.ThenInclude(r => r.Role)
|
||||
@ -92,21 +74,16 @@ namespace API.Data
|
||||
Folders = l.Folders.Select(x => x.Path).ToList()
|
||||
}).ToList()
|
||||
})
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<MemberDto> GetMemberAsync(string username)
|
||||
{
|
||||
return await _userManager.Users.Where(x => x.UserName == username)
|
||||
.Include(x => x.Libraries)
|
||||
.ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
|
||||
.SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
public void UpdateReadingProgressAsync(int volumeId, int pageNum)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// public async Task<MemberDto> GetMemberAsync(string username)
|
||||
// {
|
||||
// return await _context.Users.Where(x => x.UserName == username)
|
||||
// .Include(x => x.Libraries)
|
||||
// .ProjectTo<MemberDto>(_mapper.ConfigurationProvider)
|
||||
// .SingleOrDefaultAsync();
|
||||
// }
|
||||
}
|
||||
}
|
@ -10,6 +10,13 @@ namespace API.Helpers
|
||||
public AutoMapperProfiles()
|
||||
{
|
||||
CreateMap<LibraryDto, Library>();
|
||||
// .ForMember(dest => dest.Folders,
|
||||
// opt =>
|
||||
// opt.MapFrom(src => src.Folders.Select(x => new FolderPath()
|
||||
// {
|
||||
// Path = x,
|
||||
// //LibraryId = src.Id
|
||||
// }).ToList()));
|
||||
|
||||
CreateMap<Volume, VolumeDto>();
|
||||
|
||||
|
@ -12,6 +12,7 @@ namespace API.Interfaces
|
||||
Task<bool> LibraryExists(string libraryName);
|
||||
Task<Library> GetLibraryForIdAsync(int libraryId);
|
||||
Task<IEnumerable<LibraryDto>> GetLibraryDtosForUsernameAsync(string userName);
|
||||
Task<IEnumerable<Library>> GetLibrariesAsync();
|
||||
Task<Library> GetLibraryForNameAsync(string libraryName);
|
||||
Task<bool> DeleteLibrary(int libraryId);
|
||||
}
|
||||
|
@ -8,9 +8,9 @@ namespace API.Interfaces
|
||||
public interface IUserRepository
|
||||
{
|
||||
void Update(AppUser user);
|
||||
public void Delete(AppUser user);
|
||||
Task<AppUser> GetUserByUsernameAsync(string username);
|
||||
Task<IEnumerable<MemberDto>> GetMembersAsync();
|
||||
public void Delete(AppUser user);
|
||||
Task<IEnumerable<AppUser>> GetAdminUsersAsync();
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ namespace API.Services
|
||||
private readonly ILogger<TaskScheduler> _logger;
|
||||
private readonly IUnitOfWork _unitOfWork;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly BackgroundJobServer _client;
|
||||
public BackgroundJobServer Client => new BackgroundJobServer();
|
||||
|
||||
public TaskScheduler(ICacheService cacheService, ILogger<TaskScheduler> logger,
|
||||
IUnitOfWork unitOfWork, IDirectoryService directoryService)
|
||||
@ -19,8 +19,7 @@ namespace API.Services
|
||||
_logger = logger;
|
||||
_unitOfWork = unitOfWork;
|
||||
_directoryService = directoryService;
|
||||
_client = new BackgroundJobServer();
|
||||
|
||||
|
||||
_logger.LogInformation("Scheduling/Updating cache cleanup on a daily basis.");
|
||||
RecurringJob.AddOrUpdate(() => _cacheService.Cleanup(), Cron.Daily);
|
||||
//RecurringJob.AddOrUpdate(() => scanService.ScanLibraries(), Cron.Daily);
|
||||
|
Loading…
x
Reference in New Issue
Block a user