Skip to content

Commit eb97097

Browse files
author
Asylgareev Rustam
committed
Add Pagination
1 parent ff72f65 commit eb97097

7 files changed

Lines changed: 83 additions & 7 deletions

File tree

src/SmartHead.Essentials.Application/Controller/ApiControllerBase.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,24 @@ namespace SmartHead.Essentials.Application.Controller
66
{
77
[ApiController]
88
[Route("api/[controller]")]
9+
[ProducesErrorResponseType(typeof(void))]
910
public abstract class FormattedApiControllerBase : ControllerBase
1011
{
1112
[NonAction]
1213
public BadRequestObjectResult BadRequest(string subStatus)
1314
=> BadRequest(subStatus, null);
14-
1515
[NonAction]
1616
public override BadRequestObjectResult BadRequest(object error)
1717
=> BadRequest("SomethingWentWrong", error);
18-
1918
[NonAction]
2019
public override BadRequestObjectResult BadRequest(ModelStateDictionary modelState)
2120
=> InvalidModelStateResponseFactory.CreateFrom("InvalidModel", modelState);
22-
2321
[NonAction]
2422
public BadRequestObjectResult BadRequest(string subStatus, object errorContent, string debugData = null)
2523
=> new BadRequestObjectResult(new ErrorApiResponse(subStatus, errorContent, debugData));
26-
2724
[NonAction]
2825
public OkObjectResult Ok(object content, string debugData = null)
2926
=> new OkObjectResult(new SuccessApiResponse(content, debugData));
30-
3127
[NonAction]
3228
public CreatedAtRouteResult CreatedAt(object routeValues, object content, string debugData = null)
3329
=> new CreatedAtRouteResult(routeValues, new SuccessApiResponse(content, debugData));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace SmartHead.Essentials.Application.Pagination
5+
{
6+
public class PagedEnumerable<T>
7+
{
8+
public PagedEnumerable(IEnumerable<T> items, PaginationModel pagination)
9+
{
10+
Items = items ?? throw new ArgumentNullException(nameof(items));
11+
Pagination = pagination ?? throw new ArgumentNullException(nameof(pagination));
12+
}
13+
14+
public PaginationModel Pagination { get; set; }
15+
public IEnumerable<T> Items { get; set; }
16+
}
17+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace SmartHead.Essentials.Application.Pagination
4+
{
5+
public class PaginationModel
6+
{
7+
public PaginationModel(int count, int size, int page = 1)
8+
{
9+
PageNumber = page;
10+
TotalPages = (int)Math.Ceiling(count / (double)size);
11+
PageSize = size;
12+
}
13+
14+
public int PageNumber { get; }
15+
16+
public int TotalPages { get; }
17+
18+
public int PageSize { get; }
19+
20+
public bool HasPreviousPage => PageNumber > 1;
21+
22+
public bool HasNextPage => PageNumber < TotalPages;
23+
}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.ComponentModel.DataAnnotations;
2+
3+
namespace SmartHead.Essentials.Application.Pagination
4+
{
5+
public class QueryModel
6+
{
7+
[Required]
8+
public int Page { get; set; } = 1;
9+
}
10+
}

src/SmartHead.Essentials.Application/SmartHead.Essentials.Application.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<IsPackable>true</IsPackable>
88
<Authors>Rustam Asylgareev</Authors>
99
<PackageProjectUrl>https://github.com/smarthead-dev/SmartHead.Essentials</PackageProjectUrl>
10-
<Version>0.0.4</Version>
10+
<Version>0.0.5</Version>
1111
<Company>SmartHead</Company>
1212
<IsTestProject>false</IsTestProject>
1313
<Description>SmartHead.Essentials.Application</Description>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System.Linq;
2+
using SmartHead.Essentials.Application.Pagination;
3+
4+
namespace SmartHead.Essentials.Extensions
5+
{
6+
public static class EnumerableExtensions
7+
{
8+
public static PagedEnumerable<TModel> GetPage<TModel>(
9+
this IQueryable<TModel> query,
10+
int page = 1,
11+
int size = 10)
12+
{
13+
if (page < 1) page = 1;
14+
if (size < 1) size = 10;
15+
16+
var paginatedQueryable = query
17+
.Skip((page - 1) * size)
18+
.Take(size);
19+
20+
var paginationModel = new PaginationModel(query.Count(), size, page);
21+
22+
return new PagedEnumerable<TModel>(paginatedQueryable, paginationModel);
23+
}
24+
}
25+
}

src/SmartHead.Essentials.Extensions/SmartHead.Essentials.Extensions.csproj

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<IsPackable>true</IsPackable>
77
<Authors>Rustam Asylgareev</Authors>
88
<PackageProjectUrl>https://github.com/smarthead-dev/SmartHead.Essentials</PackageProjectUrl>
9-
<Version>0.0.2</Version>
9+
<Version>0.0.3</Version>
1010
<Company>SmartHead</Company>
1111
<IsTestProject>false</IsTestProject>
1212
<Description>SmartHead.Essentials.Extensions</Description>
@@ -18,4 +18,8 @@
1818
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.1.1" />
1919
</ItemGroup>
2020

21+
<ItemGroup>
22+
<ProjectReference Include="..\SmartHead.Essentials.Application\SmartHead.Essentials.Application.csproj" />
23+
</ItemGroup>
24+
2125
</Project>

0 commit comments

Comments
 (0)