-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pagination for repository and response message
- Loading branch information
Showing
8 changed files
with
134 additions
and
0 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
19 changes: 19 additions & 0 deletions
19
Source/BSN.Commons.PresentationInfrastructure/Converters/JsonForceDefaultConverter.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using System; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.Converters | ||
{ | ||
public class JsonForceDefaultConverter<T> : JsonConverter<T> | ||
{ | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
return JsonSerializer.Deserialize<T>(ref reader); | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
{ | ||
JsonSerializer.Serialize(writer, value); | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Source/BSN.Commons.PresentationInfrastructure/Responses/GenericResponseBase.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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace BSN.Commons.Responses | ||
{ | ||
public class GenericResponseBase<T> : Response where T : class | ||
{ | ||
public T Data { get; set; } | ||
|
||
public GenericResponseBase() { } | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
Source/BSN.Commons.PresentationInfrastructure/Responses/GenericResponseBaseWithPagination.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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace BSN.Commons.Responses | ||
{ | ||
public class GenericResponseBaseWithPagination<T> : GenericResponseBase<T> where T : class | ||
{ | ||
public PaginationMetadata Meta { get; set; } | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
Source/BSN.Commons.PresentationInfrastructure/Responses/PaginationMetadata.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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace BSN.Commons.Responses | ||
{ | ||
public class PaginationMetadata | ||
{ | ||
public uint Page { get; set; } | ||
|
||
public uint PageCount { get; set; } | ||
|
||
public uint PageSize { get; set; } | ||
|
||
public uint RecordCount { get; set; } | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
Source/BSN.Commons.PresentationInfrastructure/Responses/Response.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using BSN.Commons.Converters; | ||
using BSN.Commons.PresentationInfrastructure; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace BSN.Commons.Responses | ||
{ | ||
/// <summary> | ||
/// During serialization and deserialization operations, enumeration values are always converted to and from strings. | ||
/// This is possible through registering the 'JsonStringEnumConverter' in our DI infrastructure. There is once exception, | ||
/// namely the 'StatusCode' property of the 'ResponseBase' class which should keep it's default numeral value when being converted. | ||
/// </summary> | ||
public class Response : ResponseBase | ||
{ | ||
public new bool IsSuccess => (int)StatusCode >= 200 && (int)StatusCode <= 299; | ||
|
||
[JsonConverter(typeof(JsonForceDefaultConverter<ResponseStatusCode>))] | ||
public new ResponseStatusCode StatusCode { get; set; } | ||
} | ||
} |
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,36 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace BSN.Commons.Dto | ||
{ | ||
/// <summary> | ||
/// Stores the paginated data for the given entity type. | ||
/// </summary> | ||
/// <typeparam name="T">Type of Entity for which pagination is being implemented.</typeparam> | ||
public class PagedEntityCollection<T> | ||
{ | ||
/// <summary> | ||
/// Current page number | ||
/// </summary> | ||
public uint CurrentPage { get; set; } | ||
|
||
/// <summary> | ||
/// Total number of pages | ||
/// </summary> | ||
public uint PageCount { get; set; } | ||
|
||
/// <summary> | ||
/// Number of records per page | ||
/// </summary> | ||
public uint PageSize { get; set; } | ||
|
||
/// <summary> | ||
/// Total number of records that exist for the query | ||
/// </summary> | ||
public uint RecordCount { get; set; } | ||
|
||
/// <summary> | ||
/// IEnumerable of the paginated data | ||
/// </summary> | ||
public IEnumerable<T> Results { get; set; } | ||
} | ||
} |
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,30 @@ | ||
using BSN.Commons.Dto; | ||
using System; | ||
using System.Linq; | ||
|
||
namespace BSN.Commons.Extensions | ||
{ | ||
public static partial class IQueryableExtensions | ||
{ | ||
public static PagedEntityCollection<T> Paginate<T>(this IQueryable<T> query, uint pageNumber, uint pageSize) | ||
{ | ||
if (pageNumber == 0) | ||
throw new ArgumentException("Must be greater than zero.", nameof(pageNumber)); | ||
|
||
if (pageSize == 0) | ||
throw new ArgumentException("Must be greater than zero.", nameof(pageSize)); | ||
|
||
var result = new PagedEntityCollection<T> | ||
{ | ||
CurrentPage = pageNumber, | ||
PageSize = pageSize, | ||
RecordCount = (uint)query.Count(), | ||
Results = query.Skip((int)((pageNumber - 1) * pageSize)).Take((int)pageSize).ToList() | ||
}; | ||
|
||
result.PageCount = (uint)Math.Ceiling((double)result.RecordCount / pageSize); | ||
|
||
return result; | ||
} | ||
} | ||
} |