diff --git a/API/Controllers/ReaderController.cs b/API/Controllers/ReaderController.cs index e53a5402f..f360151ad 100644 --- a/API/Controllers/ReaderController.cs +++ b/API/Controllers/ReaderController.cs @@ -62,7 +62,7 @@ public class ReaderController : BaseApiController { if (await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey) == 0) return BadRequest(); var chapter = await _cacheService.Ensure(chapterId); - if (chapter == null) return BadRequest("There was an issue finding pdf file for reading"); + if (chapter == null) return NoContent(); // Validate the user has access to the PDF var series = await _unitOfWork.SeriesRepository.GetSeriesForChapter(chapter.Id, @@ -101,7 +101,7 @@ public class ReaderController : BaseApiController if (page < 0) page = 0; if (await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey) == 0) return BadRequest(); var chapter = await _cacheService.Ensure(chapterId, extractPdf); - if (chapter == null) return BadRequest("There was an issue finding image file for reading"); + if (chapter == null) return NoContent(); try { @@ -125,7 +125,7 @@ public class ReaderController : BaseApiController { if (await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey) == 0) return BadRequest(); var chapter = await _cacheService.Ensure(chapterId, true); - if (chapter == null) return BadRequest("There was an issue extracting images from chapter"); + if (chapter == null) return NoContent(); var images = _cacheService.GetCachedPages(chapterId); var path = await _readerService.GetThumbnail(chapter, pageNum, images); @@ -148,7 +148,7 @@ public class ReaderController : BaseApiController { if (page < 0) page = 0; var userId = await _unitOfWork.UserRepository.GetUserIdByApiKeyAsync(apiKey); - if (userId == 0) return BadRequest(); + if (userId == 0) return Unauthorized(); var totalPages = await _cacheService.CacheBookmarkForSeries(userId, seriesId); if (page > totalPages) @@ -185,7 +185,7 @@ public class ReaderController : BaseApiController { if (chapterId <= 0) return ArraySegment.Empty; var chapter = await _cacheService.Ensure(chapterId, extractPdf); - if (chapter == null) return BadRequest("Could not find Chapter"); + if (chapter == null) return NoContent(); return Ok(_cacheService.GetCachedFileDimensions(_cacheService.GetCachePath(chapterId))); } @@ -203,7 +203,7 @@ public class ReaderController : BaseApiController { if (chapterId <= 0) return Ok(null); // This can happen occasionally from UI, we should just ignore var chapter = await _cacheService.Ensure(chapterId, extractPdf); - if (chapter == null) return BadRequest("Could not find Chapter"); + if (chapter == null) return NoContent(); var dto = await _unitOfWork.ChapterRepository.GetChapterInfoDtoAsync(chapterId); if (dto == null) return BadRequest("Please perform a scan on this series or library and try again"); diff --git a/API/Controllers/SeriesController.cs b/API/Controllers/SeriesController.cs index c729e7017..a26718297 100644 --- a/API/Controllers/SeriesController.cs +++ b/API/Controllers/SeriesController.cs @@ -64,16 +64,9 @@ public class SeriesController : BaseApiController public async Task> GetSeries(int seriesId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); - try - { - return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, userId)); - } - catch (Exception e) - { - _logger.LogError(e, "There was an issue fetching {SeriesId}", seriesId); - throw new KavitaException("This series does not exist"); - } - + var series = await _unitOfWork.SeriesRepository.GetSeriesDtoByIdAsync(seriesId, userId); + if (series == null) return NoContent(); + return Ok(series); } [Authorize(Policy = "RequireAdminRole")] @@ -114,13 +107,16 @@ public class SeriesController : BaseApiController public async Task> GetVolume(int volumeId) { var userId = await _unitOfWork.UserRepository.GetUserIdByUsernameAsync(User.GetUsername()); - return Ok(await _unitOfWork.VolumeRepository.GetVolumeDtoAsync(volumeId, userId)); + var vol = await _unitOfWork.VolumeRepository.GetVolumeDtoAsync(volumeId, userId); + if (vol == null) return NoContent(); + return Ok(vol); } [HttpGet("chapter")] public async Task> GetChapter(int chapterId) { var chapter = await _unitOfWork.ChapterRepository.GetChapterDtoAsync(chapterId); + if (chapter == null) return NoContent(); return Ok(await _unitOfWork.ChapterRepository.AddChapterModifiers(User.GetUserId(), chapter)); } diff --git a/API/Data/Repositories/SeriesRepository.cs b/API/Data/Repositories/SeriesRepository.cs index 04215c9ca..32cd0deea 100644 --- a/API/Data/Repositories/SeriesRepository.cs +++ b/API/Data/Repositories/SeriesRepository.cs @@ -80,7 +80,7 @@ public interface ISeriesRepository /// Task SearchSeries(int userId, bool isAdmin, IList libraryIds, string searchQuery); Task> GetSeriesForLibraryIdAsync(int libraryId, SeriesIncludes includes = SeriesIncludes.None); - Task GetSeriesDtoByIdAsync(int seriesId, int userId); + Task GetSeriesDtoByIdAsync(int seriesId, int userId); Task GetSeriesByIdAsync(int seriesId, SeriesIncludes includes = SeriesIncludes.Volumes | SeriesIncludes.Metadata); Task> GetSeriesByIdsAsync(IList seriesIds); Task GetChapterIdsForSeriesAsync(IList seriesIds); @@ -442,11 +442,13 @@ public class SeriesRepository : ISeriesRepository return result; } - public async Task GetSeriesDtoByIdAsync(int seriesId, int userId) + public async Task GetSeriesDtoByIdAsync(int seriesId, int userId) { var series = await _context.Series.Where(x => x.Id == seriesId) .ProjectTo(_mapper.ConfigurationProvider) - .SingleAsync(); + .SingleOrDefaultAsync(); + + if (series == null) return null; var seriesList = new List() {series}; await AddSeriesModifiers(userId, seriesList); diff --git a/API/Data/Repositories/VolumeRepository.cs b/API/Data/Repositories/VolumeRepository.cs index a849ab7f0..95470a963 100644 --- a/API/Data/Repositories/VolumeRepository.cs +++ b/API/Data/Repositories/VolumeRepository.cs @@ -9,6 +9,7 @@ using API.Extensions; using API.Services; using AutoMapper; using AutoMapper.QueryableExtensions; +using Kavita.Common; using Microsoft.EntityFrameworkCore; namespace API.Data.Repositories;