diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index b72094f17..3b0481866 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -106,15 +106,15 @@ namespace API.Controllers [Authorize(Policy = "RequireAdminRole")] [HttpPut("update-for")] - public async Task> UpdateLibrary(UpdateLibraryDto updateLibraryDto) + public async Task> 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"); user.Libraries = new List(); - foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries) + foreach (var selectedLibrary in updateLibraryForUserDto.SelectedLibraries) { user.Libraries.Add(_mapper.Map(selectedLibrary)); } @@ -152,6 +152,35 @@ namespace API.Controllers public async Task> DeleteLibrary(int libraryId) { return Ok(await _libraryRepository.DeleteLibrary(libraryId)); - } + } + + [Authorize(Policy = "RequireAdminRole")] + [HttpPost("update")] + public async Task 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."); + } } } \ No newline at end of file diff --git a/API/DTOs/UpdateLibraryDto.cs b/API/DTOs/UpdateLibraryDto.cs index c664b1df6..0a922172a 100644 --- a/API/DTOs/UpdateLibraryDto.cs +++ b/API/DTOs/UpdateLibraryDto.cs @@ -2,10 +2,10 @@ namespace API.DTOs { - // NOTE: Should this be a Record? https://www.youtube.com/watch?v=9Byvwa9yF-I public class UpdateLibraryDto { - public string Username { get; init; } - public IEnumerable SelectedLibraries { get; init; } + public int Id { get; init; } + public string Name { get; init; } + public IEnumerable Folders { get; init; } } } \ No newline at end of file diff --git a/API/DTOs/UpdateLibraryForUserDto.cs b/API/DTOs/UpdateLibraryForUserDto.cs new file mode 100644 index 000000000..5280f3dd7 --- /dev/null +++ b/API/DTOs/UpdateLibraryForUserDto.cs @@ -0,0 +1,10 @@ +using System.Collections.Generic; + +namespace API.DTOs +{ + public class UpdateLibraryForUserDto + { + public string Username { get; init; } + public IEnumerable SelectedLibraries { get; init; } + } +} \ No newline at end of file diff --git a/API/Startup.cs b/API/Startup.cs index 4ad56e85a..28ed23b46 100644 --- a/API/Startup.cs +++ b/API/Startup.cs @@ -45,6 +45,7 @@ namespace API app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1")); } + app.UseHangfireDashboard(); backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!")); diff --git a/images/Image-1.jpg b/images/Image-1.jpg new file mode 100644 index 000000000..b4dfd57c6 Binary files /dev/null and b/images/Image-1.jpg differ