Some api work

This commit is contained in:
Joseph Milazzo 2020-12-20 17:47:18 -06:00
parent b6e0e05205
commit 8156aeb495
9 changed files with 82 additions and 64 deletions

View file

@ -23,15 +23,18 @@ namespace API.Controllers
private readonly ILibraryRepository _libraryRepository;
private readonly ILogger<LibraryController> _logger;
private readonly IUserRepository _userRepository;
private readonly IMapper _mapper;
public LibraryController(DataContext context, IDirectoryService directoryService,
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository)
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository,
IMapper mapper)
{
_context = context;
_directoryService = directoryService;
_libraryRepository = libraryRepository;
_logger = logger;
_userRepository = userRepository;
_mapper = mapper;
}
/// <summary>
@ -60,6 +63,43 @@ namespace API.Controllers
{
return Ok(await _libraryRepository.GetLibrariesAsync());
}
// Do I need this method?
// [HttpGet("library/{username}")]
// public async Task<ActionResult<IEnumerable<LibraryDto>>> GetLibrariesForUser(string username)
// {
// _logger.LogDebug("Method hit");
// var user = await _userRepository.GetUserByUsernameAsync(User.GetUsername());
//
// if (user == null) return BadRequest("Could not validate user");
//
// return Ok(await _libraryRepository.GetLibrariesForUserAsync(user));
// }
[HttpPut("update-for")]
public async Task<ActionResult<MemberDto>> UpdateLibrary(UpdateLibraryDto updateLibraryDto)
{
// TODO: Only admins can do this
var user = await _userRepository.GetUserByUsernameAsync(updateLibraryDto.Username);
if (user == null) return BadRequest("Could not validate user");
if (!user.IsAdmin) return Unauthorized("Only admins are permitted");
user.Libraries = new List<Library>();
foreach (var selectedLibrary in updateLibraryDto.SelectedLibraries)
{
user.Libraries.Add(_mapper.Map<Library>(selectedLibrary));
}
if (await _userRepository.SaveAllAsync())
{
return Ok(user);
}
return BadRequest("Not Implemented");
}