Refactored Volume to have Name and Number (int) so that we can properly sort and still handle possible split volumes.
Refactored ScanLibrary into Library controller and updated it so it adds the new library to all admins.
This commit is contained in:
parent
d632e53f18
commit
9168e12483
13 changed files with 622 additions and 49 deletions
|
@ -78,6 +78,8 @@ namespace API.Controllers
|
|||
user.LastActive = DateTime.Now;
|
||||
_userRepository.Update(user);
|
||||
await _userRepository.SaveAllAsync();
|
||||
|
||||
_logger.LogInformation($"{user.UserName} logged in at {user.LastActive}");
|
||||
|
||||
return new UserDto
|
||||
{
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
|
@ -35,6 +36,47 @@ namespace API.Controllers
|
|||
_taskScheduler = taskScheduler;
|
||||
_seriesRepository = seriesRepository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new Library. Upon library creation, adds new library to all Admin accounts.
|
||||
/// </summary>
|
||||
/// <param name="createLibraryDto"></param>
|
||||
/// <returns></returns>
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpPost("create")]
|
||||
public async Task<ActionResult> AddLibrary(CreateLibraryDto createLibraryDto)
|
||||
{
|
||||
if (await _libraryRepository.LibraryExists(createLibraryDto.Name))
|
||||
{
|
||||
return BadRequest("Library name already exists. Please choose a unique name to the server.");
|
||||
}
|
||||
|
||||
var admins = (await _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()
|
||||
};
|
||||
|
||||
foreach (var admin in admins)
|
||||
{
|
||||
// If user is null, then set it
|
||||
admin.Libraries ??= new List<Library>();
|
||||
admin.Libraries.Add(library);
|
||||
}
|
||||
|
||||
|
||||
if (await _userRepository.SaveAllAsync())
|
||||
{
|
||||
//TODO: Enqueue scan library task
|
||||
return Ok();
|
||||
}
|
||||
|
||||
return BadRequest("There was a critical issue. Please try again.");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of directories for a given path. If path is empty, returns root drives.
|
||||
|
@ -92,6 +134,7 @@ namespace API.Controllers
|
|||
|
||||
// We have to send a json encoded Library (aka a DTO) to the Background Job thread.
|
||||
// Because we use EF, we have circular dependencies back to Library and it will crap out
|
||||
// TODO: Refactor this to use libraryId and move Library call in method.
|
||||
BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(library));
|
||||
return Ok();
|
||||
}
|
||||
|
@ -106,7 +149,6 @@ namespace API.Controllers
|
|||
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId)
|
||||
{
|
||||
return Ok(await _seriesRepository.GetSeriesForLibraryIdAsync(libraryId));
|
||||
|
||||
}
|
||||
}
|
||||
}
|
|
@ -22,47 +22,6 @@ namespace API.Controllers
|
|||
_libraryRepository = libraryRepository;
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpPost("add-library")]
|
||||
public async Task<ActionResult> AddLibrary(CreateLibraryDto createLibraryDto)
|
||||
{
|
||||
// NOTE: I think we should move this into library controller because it gets added to all admins
|
||||
|
||||
var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
|
||||
|
||||
if (user == null) return BadRequest("Could not validate user");
|
||||
|
||||
|
||||
if (await _libraryRepository.LibraryExists(createLibraryDto.Name))
|
||||
{
|
||||
return BadRequest("Library name already exists. Please choose a unique name to the server.");
|
||||
}
|
||||
|
||||
var library = new Library
|
||||
{
|
||||
Name = createLibraryDto.Name.ToLower(),
|
||||
Type = createLibraryDto.Type,
|
||||
AppUsers = new List<AppUser>() { user }
|
||||
};
|
||||
|
||||
library.Folders = createLibraryDto.Folders.Select(x => new FolderPath
|
||||
{
|
||||
Path = x,
|
||||
Library = library
|
||||
}).ToList();
|
||||
|
||||
user.Libraries ??= new List<Library>(); // If user is null, then set it
|
||||
|
||||
user.Libraries.Add(library);
|
||||
|
||||
if (await _userRepository.SaveAllAsync())
|
||||
{
|
||||
return Ok();
|
||||
}
|
||||
|
||||
return BadRequest("Not implemented");
|
||||
}
|
||||
|
||||
[Authorize(Policy = "RequireAdminRole")]
|
||||
[HttpDelete("delete-user")]
|
||||
public async Task<ActionResult> DeleteUser(string username)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue