Miccore.Net.Pagination 1.0.0

dotnet add package Miccore.Net.Pagination --version 1.0.0                
NuGet\Install-Package Miccore.Net.Pagination -Version 1.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Miccore.Net.Pagination" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Miccore.Net.Pagination --version 1.0.0                
#r "nuget: Miccore.Net.Pagination, 1.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Miccore.Net.Pagination as a Cake Addin
#addin nuget:?package=Miccore.Net.Pagination&version=1.0.0

// Install Miccore.Net.Pagination as a Cake Tool
#tool nuget:?package=Miccore.Net.Pagination&version=1.0.0                

Miccore .Net Pagination

Require .Net core 6.0

Package library for pagination when using query and entity framework core

Intallation

dotnet CLI installation


dotnet add package Miccore.Net.Pagination

Package manager


Install-Package Miccore.Net.Pagination

In the following points, we will take an example of adding paging to a list of notifications in the server.

We use here an architecture based on the service-repository pattern. So we'll start from the repositories to get to the controller

Using in repository

Include all namespaces

  using Miccore.Pagination.Models;
  using Microsoft.Extensions.DependencyInjection;

Add logic to the function

...
public  class  NotificationRepository : INotificationRepository {
	...
	public  async  Task<PaginationModel<NotificationDtoModel>> GetAllAsync(PaginationQuery query)
	{
		var  notifications  =  await  _context.Notifications.PaginateAsync(query);
		return  notifications;
	}
	...
}
public  interface  INotificationRepository{
...
Task<PaginationModel<NotificationDtoModel>> GetAllAsync(PaginationQuery query);
...
}

Using in Service

We are using here a service mapper like AutoMapper first of all we have to add Mapper Profile.

if you don't use it, the next code is not for you

Include models

 using Miccore.Pagination.Models;

set Profile


...
public  class  NotificationProfile : Profile
{
	...
	public  NotificationProfile()
	{
		CreateMap<PaginationModel<NotificationDomainModel>, PaginationModel<NotificationDtoModel>>().ReverseMap();
	}
	...
}

After setting profile or not, you have now to set the service and interface

...
public  class  NotificationService : INotificationService {
	...
	public  async  Task<PaginationModel<NotificationDtoModel>> GetAllNotificationAsync(PaginationQuery query)
	{
		var  notifications  =  await  _notificationRepository.GetAllAsync(query);
		return  _mapper.Map<PaginationModel<NotificationDomainModel>>(notifications);
	}
	...
}
public  interface  INotificationService{
...
Task<PaginationModel<NotificationDtoModel>> GetAllNotificationAsync(PaginationQuery query);
...
}

Using in controller

like in the service, if you are using AutoMapper, you have to set the profile of mapper before map element in the function

Include elements in controller

 using Miccore.Pagination.Models;
 using Miccore.Pagination.Service;

Update controller function

...
public  class  NotificationController : BaseController {
	...
	[HttpGet(template: "", Name =  nameof(GetAllNotification))]
	public  async  Task<ActionResult<PaginationModel<NotificationViewModel>>> GetAllNotification([FromQuery] PaginationQuery query)
	{
		try
		{
			var  notifications  =  await  _notificationService.GetAllNotificationsAsync(query);
			var  response  =  _mapper.Map<PaginationModel<NotificationViewModel>>(notifications);
			// return only items if paginate commande is false
			if(!query.paginate){
				return  HandleSuccessResponse(response.Items);
			}
			// add the previous and next url of pages if exists
			response.AddRouteLink(Url.RouteUrl(nameof(GetAllNotification)), query);
			return  HandleSuccessResponse(response);
		}
		catch (Exception  ex)
		{
			return  HandleErrorResponse(HttpStatusCode.InternalServerError, ex.Message);
		}
	}
	...
}

🥳 🤩 Well done, you can now test your feature

Models content

Pagination Model definition

public  class  PaginationModel<TModel>{
	const  int MaxPageSize =  100;
	private  int _pageSize;
	public  int PageSize {
		get  =>  _pageSize;
		set  =>  _pageSize  = ( value  >  MaxPageSize) ?  MaxPageSize  :  value;
	}
	public  int CurrentPage {get; set;}
	public  int TotalItems {get; set;}
	public  int TotalPages { get; set;}
	public  List<TModel> Items {get; set;}
	public  string Prev {get; set;}
	public  string Next {get; set;}
	public  PaginationModel(){
		Items  =  new  List<TModel>();
	}
}

Pagination Query

public  class  PaginationQuery{
	[DefaultValue(false)]
	public  bool paginate { get; set;}
	[DefaultValue(1)]
	public  int page { get; set;}
	[DefaultValue(10)]
	public  int limit { get; set;}
}

paginate says whether the returned content should be paginated or not

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 17,530 2/26/2022