Skip to content

Kavita is a fast, feature rich, cross platform reading server. Built with the goal of being a full solution for all your reading needs. Setup your own server and share your reading collection with your friends and family.

Implemented pagination on GetSeriesForLibrary

Joseph Milazzo222959981f4a
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<ActionResult<IEnumerable<Series>>> GetSeriesForLibrary(int libraryId)
+ public async Task<ActionResult<IEnumerable<Series>>> 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<IEnumerable<SeriesDto>> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId)
+ public async Task<PagedList<SeriesDto>> 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<SeriesDto>(_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<SeriesDto>.CreateAsync(query, userParams.PageNumber, userParams.PageSize);
}
public async Task<IEnumerable<SearchResultDto>> 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<Series> GetSeriesByNameAsync(string name);
Series GetSeriesByName(string name);
+
/// <summary>
/// Adds user information like progress, ratings, etc
/// </summary>
/// <param name="libraryId"></param>
/// <param name="userId"></param>
/// <returns></returns>
- Task<IEnumerable<SeriesDto>> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId);
+ Task<PagedList<SeriesDto>> GetSeriesDtoForLibraryIdAsync(int libraryId, int userId, UserParams userParams);
/// <summary>
/// Does not add user information like progress, ratings, etc.