diff --git a/API/Controllers/LibraryController.cs b/API/Controllers/LibraryController.cs index 7fa186532..34bb2e94e 100644 --- a/API/Controllers/LibraryController.cs +++ b/API/Controllers/LibraryController.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using API.DTOs; using API.Entities; using API.Extensions; +using API.Helpers; using API.Interfaces; using API.Interfaces.Services; using AutoMapper; @@ -166,10 +167,16 @@ namespace API.Controllers } [HttpGet("series")] - public async Task>> GetSeriesForLibrary(int libraryId) + public async Task>> GetSeriesForLibrary(int libraryId, [FromQuery] UserParams userParams) { + // TODO: Move this to SeriesController var user = await _unitOfWork.UserRepository.GetUserByUsernameAsync(User.GetUsername()); - return Ok(await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id)); + var series = + await _unitOfWork.SeriesRepository.GetSeriesDtoForLibraryIdAsync(libraryId, user.Id, userParams); + + Response.AddPaginationHeader(series.CurrentPage, series.PageSize, series.TotalCount, series.TotalPages); + + return Ok(series); } [Authorize(Policy = "RequireAdminRole")] diff --git a/API/Data/SeriesRepository.cs b/API/Data/SeriesRepository.cs index 45b67894f..c501fdc61 100644 --- a/API/Data/SeriesRepository.cs +++ b/API/Data/SeriesRepository.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Threading.Tasks; using API.DTOs; using API.Entities; +using API.Helpers; using API.Interfaces; using AutoMapper; using AutoMapper.QueryableExtensions; @@ -63,21 +64,25 @@ namespace API.Data .ToListAsync(); } - public async Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId) + public async Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId, UserParams userParams) { var sw = Stopwatch.StartNew(); - var series = await _context.Series + + + + var query = _context.Series .Where(s => s.LibraryId == libraryId) .OrderBy(s => s.SortName) .ProjectTo(_mapper.ConfigurationProvider) - .ToListAsync(); - - - await AddSeriesModifiers(userId, series); + .AsNoTracking(); + + + // TODO: Refactor this into JOINs + //await AddSeriesModifiers(userId, series); _logger.LogDebug("Processed GetSeriesDtoForLibraryIdAsync in {ElapsedMilliseconds} milliseconds", sw.ElapsedMilliseconds); - return series; + return await PagedList.CreateAsync(query, userParams.PageNumber, userParams.PageSize); } public async Task> SearchSeries(int[] libraryIds, string searchQuery) diff --git a/API/Extensions/HttpExtensions.cs b/API/Extensions/HttpExtensions.cs index 5a4b4238d..468bac728 100644 --- a/API/Extensions/HttpExtensions.cs +++ b/API/Extensions/HttpExtensions.cs @@ -10,7 +10,12 @@ namespace API.Extensions int itemsPerPage, int totalItems, int totalPages) { var paginationHeader = new PaginationHeader(currentPage, itemsPerPage, totalItems, totalPages); - response.Headers.Add("Pagination", JsonSerializer.Serialize(paginationHeader)); + var options = new JsonSerializerOptions() + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase + }; + + response.Headers.Add("Pagination", JsonSerializer.Serialize(paginationHeader, options)); response.Headers.Add("Access-Control-Expose-Headers", "Pagination"); } } diff --git a/API/Interfaces/ISeriesRepository.cs b/API/Interfaces/ISeriesRepository.cs index 6c78341a3..3fc58824a 100644 --- a/API/Interfaces/ISeriesRepository.cs +++ b/API/Interfaces/ISeriesRepository.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using API.DTOs; using API.Entities; +using API.Helpers; namespace API.Interfaces { @@ -11,13 +12,14 @@ namespace API.Interfaces void Update(Series series); Task GetSeriesByNameAsync(string name); Series GetSeriesByName(string name); + /// /// Adds user information like progress, ratings, etc /// /// /// /// - Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId); + Task> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId, UserParams userParams); /// /// Does not add user information like progress, ratings, etc.