Implemented the ability to update a library including folders.

This commit is contained in:
Joseph Milazzo 2021-01-04 13:06:01 -06:00
parent ac2b40aba6
commit 219f6e675a
5 changed files with 37 additions and 7 deletions

View file

@ -106,15 +106,15 @@ namespace API.Controllers
[Authorize(Policy = "RequireAdminRole")]
[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");
user.Libraries = new List<Library>();
foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries)
foreach (var selectedLibrary in updateLibraryForUserDto.SelectedLibraries)
{
user.Libraries.Add(_mapper.Map<Library>(selectedLibrary));
}
@ -152,6 +152,25 @@ namespace API.Controllers
public async Task<ActionResult<bool>> DeleteLibrary(int 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);
library.Name = libraryForUserDto.Name;
library.Folders = libraryForUserDto.Folders.Select(s => new FolderPath() {Path = s}).ToList();
_libraryRepository.Update(library);
if (await _libraryRepository.SaveAllAsync())
{
// TODO: We should probably call ScanLibrary after this
return Ok();
}
return BadRequest("There was a critical issue updating the library.");
}
}
}