Fixed some APIs that worked mins ago....something strange happening with EF relationships.

This commit is contained in:
Joseph Milazzo 2021-01-19 10:45:37 -06:00
parent 295e62d773
commit 14e8c3b820
10 changed files with 62 additions and 43 deletions

View file

@ -8,7 +8,6 @@ using API.Entities;
using API.Extensions;
using API.Interfaces;
using AutoMapper;
using Hangfire;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
@ -50,23 +49,26 @@ namespace API.Controllers
{
return BadRequest("Library name already exists. Please choose a unique name to the server.");
}
var admins = (await _unitOfWork.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()
};
_unitOfWork.LibraryRepository.Update(library);
if (!await _unitOfWork.Complete())
_unitOfWork.LibraryRepository.Add(library);
var admins = (await _unitOfWork.UserRepository.GetAdminUsersAsync()).ToList();
foreach (var admin in admins)
{
return BadRequest("There was a critical issue. Please try again.");
admin.Libraries ??= new List<Library>();
admin.Libraries.Add(library);
}
if (!await _unitOfWork.Complete()) return BadRequest("There was a critical issue. Please try again.");
_logger.LogInformation($"Created a new library: {library.Name}");
_taskScheduler.ScanLibrary(library.Id);
return Ok();
@ -158,13 +160,13 @@ namespace API.Controllers
[HttpGet("series")]
public async Task<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId, bool forUser = false)
{
// TODO: Move to series?
int userId = 0;
if (forUser)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id));
userId = user.Id;
}
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId));
return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, userId));
}
[Authorize(Policy = "RequireAdminRole")]

View file

@ -51,14 +51,10 @@ namespace API.Controllers
}
[HttpGet("volumes")]
public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId, bool forUser = true)
public async Task<ActionResult<IEnumerable<VolumeDto>>> GetVolumes(int seriesId)
{
if (forUser)
{
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id));
}
return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId)); // TODO: Refactor out forUser = false since everything is user based
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
return Ok(await _unitOfWork.SeriesRepository.GetVolumesDtoAsync(seriesId, user.Id));
}
[HttpGet("volume")]

View file

@ -26,11 +26,8 @@ namespace API.Controllers
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(username);
_unitOfWork.UserRepository.Delete(user);
if (await _unitOfWork.Complete())
{
return Ok();
}
if (await _unitOfWork.Complete()) return Ok();
return BadRequest("Could not delete the user.");
}
@ -44,14 +41,7 @@ namespace API.Controllers
[HttpGet("has-library-access")]
public async Task<ActionResult<bool>> HasLibraryAccess(int libraryId)
{
// TODO: refactor this to use either userexists or usermanager
var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername());
if (user == null) return BadRequest("Could not validate user");
var libs = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(user.UserName);
var libs = await _unitOfWork.LibraryRepository.GetLibraryDtosForUsernameAsync(User.GetUsername());
return Ok(libs.Any(x => x.Id == libraryId));
}
}