Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic pagination on generic repository #1304 #1305

Merged
merged 4 commits into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ namespace AzureIoTHub.Portal.Infrastructure.Repositories
using Domain.Base;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Shared.Models.v1._0;
using System.Linq.Dynamic.Core;

public class GenericRepository<T> : IRepository<T> where T : EntityBase
{
Expand Down Expand Up @@ -50,5 +53,32 @@ public void Delete(object id)

_ = this.context.Set<T>().Remove(existing);
}

public async Task<PaginatedResult<T>> GetPaginatedListAsync(
int pageNumber,
int pageSize,
string[]? orderBy = null,
Expression<Func<T, bool>>? expression = null,
CancellationToken cancellationToken = default
)
{
IQueryable<T> query = this.context.Set<T>();
if (expression != null) query = query.Where(expression);

var ordering = orderBy?.Any() == true ? string.Join(",", orderBy) : null;

query = !string.IsNullOrWhiteSpace(ordering) ? query.OrderBy(ordering) : query.OrderBy(a => a.Id);

var count = await query
.AsNoTracking()
.CountAsync(cancellationToken: cancellationToken);

var items = await query
.Skip(pageNumber * pageSize)
.Take(pageSize)
.ToDynamicListAsync<T>(cancellationToken: cancellationToken);

return new PaginatedResult<T>(items, count, pageNumber, pageSize);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
namespace AzureIoTHub.Portal.Tests.Unit.Infrastructure.Repositories
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AzureIoTHub.Portal.Domain.Entities;
using AzureIoTHub.Portal.Infrastructure.Repositories;
using AzureIoTHub.Portal.Tests.Unit.UnitTests.Bases;
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Moq;
using NUnit.Framework;
Expand Down Expand Up @@ -150,5 +153,84 @@ public async Task DeleteTest()
// Assert
Assert.IsNull(context.Set<DeviceModelProperty>().Find(entityId));
}



[Test]
public async Task GetPaginatedListAsync_CustomFilter_ExpectedPageReturned()
{
// Arrange
var context = SetupDbContext();

await context.AddRangeAsync(new List<DeviceTag>
{
new()
{
Id = "device_tag_01",
Label = "Location 1",
Required = false,
Searchable = true
},
new()
{
Id = "device_tag_02",
Label = "Location 2",
Required = true,
Searchable = true
},
new()
{
Id = "device_tag_03",
Label = "Machine",
Required = true,
Searchable = true
},
new()
{
Id = "device_tag_04",
Label = "Location",
Required = false,
Searchable = false
},
});

_ = await context.SaveChangesAsync();

var instance = new GenericRepository<DeviceTag>(context);

// Order by label desc
var orderBy = new[] { $"{nameof(DeviceTag.Label)} desc" };

// Filter only tags with labels containing "location" and are searchable
var deviceTagPredicate = PredicateBuilder.True<DeviceTag>()
.And(tag => tag.Label.ToLowerInvariant().Contains("location"))
.And(tag => tag.Searchable);

// Act
var result = await instance.GetPaginatedListAsync(0, 10, orderBy, deviceTagPredicate);

// Assert
_ = result.Data.Count.Should().Be(2);
_ = result.TotalCount.Should().Be(2);
_ = result.PageSize.Should().Be(10);
_ = result.HasNextPage.Should().BeFalse();
_ = result.HasPreviousPage.Should().BeFalse();
_ = result.CurrentPage.Should().Be(0);
_ = result.TotalPages.Should().Be(1);

var firstDeviceTag = result.Data.First();

_ = firstDeviceTag.Id.Should().Be("device_tag_02");
_ = firstDeviceTag.Label.Should().Be("Location 2");
_ = firstDeviceTag.Required.Should().BeTrue();
_ = firstDeviceTag.Searchable.Should().BeTrue();

var secondDeviceTag = result.Data.Skip(1).First();

_ = secondDeviceTag.Id.Should().Be("device_tag_01");
_ = secondDeviceTag.Label.Should().Be("Location 1");
_ = secondDeviceTag.Required.Should().BeFalse();
_ = secondDeviceTag.Searchable.Should().BeTrue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public PaginatedResult(List<T> data = default, int count = 0, int page = 0, int

public bool HasPreviousPage => CurrentPage > 0;

public bool HasNextPage => CurrentPage < TotalPages;
public bool HasNextPage => CurrentPage < (TotalPages - 1);
}
}
4 changes: 4 additions & 0 deletions src/AzureIoTHubPortal.Domain/IRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
namespace AzureIoTHub.Portal.Domain
{
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Base;
using Portal.Shared.Models.v1._0;

public interface IRepository<T> where T : EntityBase
{
Expand All @@ -18,5 +20,7 @@ public interface IRepository<T> where T : EntityBase
void Update(T obj);

void Delete(object id);

Task<PaginatedResult<T>> GetPaginatedListAsync(int pageNumber, int pageSize, string[]? orderBy = null, Expression<Func<T, bool>>? expression = null, CancellationToken cancellationToken = default);
}
}