Skip to content

Commit

Permalink
add pagination for repository and response message
Browse files Browse the repository at this point in the history
  • Loading branch information
RikoLearn committed Apr 24, 2023
1 parent fbfec26 commit 485ae7c
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<ItemGroup>
<PackageReference Include="System.ComponentModel.Annotations" Version="4.7.0" />
<PackageReference Include="System.Text.Json" Version="7.0.2" />
</ItemGroup>

</Project>
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);
}
}
}
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() { }
}
}
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; }
}
}
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; }
}
}
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; }
}
}
36 changes: 36 additions & 0 deletions Source/BSN.Commons/Dto/PagedEntityCollection.cs
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; }
}
}
30 changes: 30 additions & 0 deletions Source/BSN.Commons/Extensions/IQueryableExtensions.cs
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;
}
}
}

0 comments on commit 485ae7c

Please sign in to comment.