-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from quizer-app/develop
Add pagination to quizes
- Loading branch information
Showing
9 changed files
with
119 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 7 additions & 1 deletion
8
src/Quizer.Application/Quizes/Queries/GetQuizes/GetQuizesQuery.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
using ErrorOr; | ||
using MediatR; | ||
using Quizer.Application.Utils; | ||
using Quizer.Domain.QuizAggregate; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizes; | ||
|
||
public record GetQuizesQuery( | ||
string? UserId = null) : IRequest<ErrorOr<List<Quiz>>>; | ||
string? SearchTerm, | ||
string? SortColumn, | ||
string? SortOrder, | ||
int PageNumber, | ||
int PageSize, | ||
string? UserName) : IRequest<ErrorOr<PagedList<Quiz>>>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 6 additions & 4 deletions
10
src/Quizer.Application/Quizes/Queries/GetQuizes/GetQuizesValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,16 @@ | ||
using FluentValidation; | ||
using Quizer.Application.Common.Validation; | ||
|
||
namespace Quizer.Application.Quizes.Queries.GetQuizes; | ||
|
||
public class GetQuizesValidator : AbstractValidator<GetQuizesQuery> | ||
{ | ||
public GetQuizesValidator() | ||
{ | ||
RuleFor(q => q.UserId) | ||
.BeValidGuid() | ||
.When(q => !string.IsNullOrEmpty(q.UserId)); | ||
RuleFor(q => q.PageSize) | ||
.GreaterThanOrEqualTo(1) | ||
.LessThanOrEqualTo(100); | ||
|
||
RuleFor(q => q.PageNumber) | ||
.GreaterThanOrEqualTo(1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Quizer.Application.Utils; | ||
|
||
public class PagedList<T> | ||
{ | ||
public List<T> Items { get; private set; } | ||
public int PageNumber { get; private set; } | ||
public int PageSize { get; private set; } | ||
public int TotalCount { get; private set; } | ||
|
||
public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); | ||
public bool HasNextPage => PageNumber < TotalPages; | ||
public bool HasPreviousPage => PageNumber > 1; | ||
|
||
private PagedList(List<T> items, int pageNumber, int pageSize, int totalCount) | ||
{ | ||
Items = items; | ||
PageNumber = pageNumber; | ||
PageSize = pageSize; | ||
TotalCount = totalCount; | ||
} | ||
|
||
public static async Task<PagedList<T>> CreateAsync( | ||
IQueryable<T> query, | ||
int pageNumber, | ||
int pageSize) | ||
{ | ||
var totalCount = await query.CountAsync(); | ||
var items = await query | ||
.Skip((pageNumber - 1) * pageSize) | ||
.Take(pageSize) | ||
.ToListAsync(); | ||
|
||
return new(items, pageNumber, pageSize, totalCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Quizer.Contracts.Common; | ||
|
||
public record PaginatedResponse<T>( | ||
List<T> Items, | ||
int PageNumber, | ||
int PageSize, | ||
int TotalCount, | ||
int TotalPages, | ||
bool HasNextPage, | ||
bool HasPreviousPage); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters