Merge pull request #19 from Kareadita/feature/ui-cleanup

Update Library API
This commit is contained in:
Joseph Milazzo 2021-01-04 17:14:48 -06:00 committed by GitHub
commit 98cb94f31c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 47 additions and 7 deletions

View File

@ -106,15 +106,15 @@ namespace API.Controllers
[Authorize(Policy = "RequireAdminRole")] [Authorize(Policy = "RequireAdminRole")]
[HttpPut("update-for")] [HttpPut("update-for")]
public async Task<ActionResult<MemberDto>> UpdateLibrary(UpdateLibraryDto updateLibraryDto) public async Task<ActionResult<MemberDto>> AddLibraryToUser(UpdateLibraryForUserDto updateLibraryForUserDto)
{ {
var user = await _userRepository.GetUserByUsernameAsync(updateLibraryDto.Username); var user = await _userRepository.GetUserByUsernameAsync(updateLibraryForUserDto.Username);
if (user == null) return BadRequest("Could not validate user"); if (user == null) return BadRequest("Could not validate user");
user.Libraries = new List<Library>(); user.Libraries = new List<Library>();
foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries) foreach (var selectedLibrary in updateLibraryForUserDto.SelectedLibraries)
{ {
user.Libraries.Add(_mapper.Map<Library>(selectedLibrary)); user.Libraries.Add(_mapper.Map<Library>(selectedLibrary));
} }
@ -153,5 +153,34 @@ namespace API.Controllers
{ {
return Ok(await _libraryRepository.DeleteLibrary(libraryId)); return Ok(await _libraryRepository.DeleteLibrary(libraryId));
} }
[Authorize(Policy = "RequireAdminRole")]
[HttpPost("update")]
public async Task<ActionResult> UpdateLibrary(UpdateLibraryDto libraryForUserDto)
{
var library = await _libraryRepository.GetLibraryForIdAsync(libraryForUserDto.Id);
var originalFolders = library.Folders.Select(x => x.Path);
var differenceBetweenFolders = originalFolders.Except(libraryForUserDto.Folders);
library.Name = libraryForUserDto.Name;
library.Folders = libraryForUserDto.Folders.Select(s => new FolderPath() {Path = s}).ToList();
_libraryRepository.Update(library);
if (await _libraryRepository.SaveAllAsync())
{
if (differenceBetweenFolders.Any())
{
BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(library.Id));
}
return Ok();
}
return BadRequest("There was a critical issue updating the library.");
}
} }
} }

View File

@ -2,10 +2,10 @@
namespace API.DTOs namespace API.DTOs
{ {
// NOTE: Should this be a Record? https://www.youtube.com/watch?v=9Byvwa9yF-I
public class UpdateLibraryDto public class UpdateLibraryDto
{ {
public string Username { get; init; } public int Id { get; init; }
public IEnumerable<LibraryDto> SelectedLibraries { get; init; } public string Name { get; init; }
public IEnumerable<string> Folders { get; init; }
} }
} }

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace API.DTOs
{
public class UpdateLibraryForUserDto
{
public string Username { get; init; }
public IEnumerable<LibraryDto> SelectedLibraries { get; init; }
}
}

View File

@ -45,6 +45,7 @@ namespace API
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")); app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
} }
app.UseHangfireDashboard(); app.UseHangfireDashboard();
backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!")); backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));

BIN
images/Image-1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 MiB