diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/DevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/DevicesControllerTests.cs index e1e63009e..efe98b365 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/DevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/DevicesControllerTests.cs @@ -15,6 +15,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v1._0 using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; + using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; @@ -101,6 +102,12 @@ public async Task GetListStateUnderTestExpectedBehavior() It.IsAny>())) .ReturnsAsync(expectedPaginatedDevices); + var locationUrl = "http://location/devices"; + + _ = this.mockUrlHelper + .Setup(x => x.RouteUrl(It.IsAny())) + .Returns(locationUrl); + // Act var result = await devicesController.SearchItems(); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs index fc4d92d6c..b235af93e 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/EdgeDevicesControllerTests.cs @@ -66,7 +66,6 @@ public async Task GetAllDeviceShouldReturnOkResult() }).ToList(), TotalCount = 100, PageSize = 10, - TotalPages = 10, CurrentPage = 0 }; diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs index b152593b8..cfca4ace4 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsControllerTest.cs @@ -15,6 +15,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v1._0.LoRaWAN using FluentAssertions; using Hellang.Middleware.ProblemDetails; using Microsoft.AspNetCore.Mvc; + using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Moq; @@ -75,6 +76,12 @@ public async Task GetAllDeviceConcentratorShouldReturnList() It.IsAny(), It.IsAny(), It.IsAny())) .ReturnsAsync(expectedPaginatedConcentrator); + var locationUrl = "http://location/concentrators"; + + _ = this.mockUrlHelper + .Setup(x => x.RouteUrl(It.IsAny())) + .Returns(locationUrl); + // Act var result = await concentratorsController.GetAllDeviceConcentrator().ConfigureAwait(false); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs index 9d1f95c48..2d4eece2d 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Controllers/v1.0/LoRaWAN/LoRaWANDevicesControllerTests.cs @@ -15,6 +15,7 @@ namespace AzureIoTHub.Portal.Tests.Unit.Server.Controllers.v1._0.LoRaWAN using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Controllers; + using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.AspNetCore.Routing; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Primitives; @@ -31,6 +32,7 @@ public class LoRaWANDevicesControllerTests private Mock> mockLogger; private Mock mockLoRaWANCommandService; private Mock> mockDeviceService; + private Mock mockUrlHelper; [SetUp] public void SetUp() @@ -40,6 +42,7 @@ public void SetUp() this.mockLogger = this.mockRepository.Create>(); this.mockLoRaWANCommandService = this.mockRepository.Create(); this.mockDeviceService = this.mockRepository.Create>(); + this.mockUrlHelper = this.mockRepository.Create(); } private LoRaWANDevicesController CreateLoRaWANDevicesController() @@ -47,7 +50,10 @@ private LoRaWANDevicesController CreateLoRaWANDevicesController() return new LoRaWANDevicesController( this.mockLogger.Object, this.mockLoRaWANCommandService.Object, - this.mockDeviceService.Object); + this.mockDeviceService.Object) + { + Url = this.mockUrlHelper.Object + }; } [Test] @@ -106,6 +112,12 @@ public async Task GetListStateUnderTestExpectedBehavior() It.IsAny>())) .ReturnsAsync(expectedPaginatedDevices); + var locationUrl = "http://location/devices"; + + _ = this.mockUrlHelper + .Setup(x => x.RouteUrl(It.IsAny())) + .Returns(locationUrl); + // Act var result = await devicesController.SearchItems(); diff --git a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsController.cs b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsController.cs index 33f7f3faa..53ee44e3c 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/v1.0/LoRaWAN/LoRaWANConcentratorsController.cs @@ -11,7 +11,7 @@ namespace AzureIoTHub.Portal.Server.Controllers.V10.LoRaWAN using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; - using Microsoft.AspNetCore.Routing; + using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Logging; using Services; @@ -61,10 +61,27 @@ public async Task>> GetAllDeviceC pageNumber, orderBy); + var nextPage = string.Empty; + + if (paginatedDevices.HasNextPage) + { + nextPage = Url.RouteUrl(new UrlRouteContext + { + RouteName = "GET LoRaWAN Concentrator list", + Values = new + { + pageSize, + pageNumber = pageNumber + 1, + orderBy + } + }); + } + return new PaginationResult { Items = paginatedDevices.Data, TotalItems = paginatedDevices.TotalCount, + NextPage = nextPage }; } diff --git a/src/AzureIoTHub.Portal/Server/Mappers/ConcentratorProfile.cs b/src/AzureIoTHub.Portal/Server/Mappers/ConcentratorProfile.cs index f237c1c69..0a59862c7 100644 --- a/src/AzureIoTHub.Portal/Server/Mappers/ConcentratorProfile.cs +++ b/src/AzureIoTHub.Portal/Server/Mappers/ConcentratorProfile.cs @@ -8,6 +8,7 @@ namespace AzureIoTHub.Portal.Server.Mappers using Models.v10.LoRaWAN; using Microsoft.Azure.Devices.Shared; using AzureIoTHub.Portal.Server.Helpers; + using Shared.Models.v1._0; public class ConcentratorProfile : Profile { @@ -32,6 +33,8 @@ public ConcentratorProfile() .ForMember(dest => dest.ClientThumbprint, opts => opts.MapFrom(src => DeviceHelper.RetrieveClientThumbprintValue(src))) .ForMember(dest => dest.LoraRegion, opts => opts.MapFrom(src => src.Tags["loraRegion"])) .ForMember(dest => dest.DeviceType, opts => opts.MapFrom(src => src.Tags["deviceType"])); + + _ = CreateMap, PaginatedResult>(); } } } diff --git a/src/AzureIoTHub.Portal/Server/Services/LoRaWANConcentratorService.cs b/src/AzureIoTHub.Portal/Server/Services/LoRaWANConcentratorService.cs index 0d64430ca..f73ab56c7 100644 --- a/src/AzureIoTHub.Portal/Server/Services/LoRaWANConcentratorService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/LoRaWANConcentratorService.cs @@ -3,7 +3,6 @@ namespace AzureIoTHub.Portal.Server.Services { - using System.Linq; using System.Threading.Tasks; using AutoMapper; using Domain; @@ -62,14 +61,8 @@ public async Task> GetAllDeviceConcentrator( string[] orderBy = null) { var paginatedConcentrator = await this.concentratorRepository.GetPaginatedListAsync(pageNumber, pageSize, orderBy); - var paginatedConcentratorDto = new PaginatedResult - { - Data = paginatedConcentrator.Data.Select(x => mapper.Map(x)).ToList(), - TotalCount = paginatedConcentrator.TotalCount, - CurrentPage = paginatedConcentrator.CurrentPage, - PageSize = pageSize - }; - return paginatedConcentratorDto; + + return this.mapper.Map>(paginatedConcentrator); } public async Task GetConcentrator(string deviceId) diff --git a/src/AzureIoTHub.Portal/Shared/Models/v1.0/PaginatedResult.cs b/src/AzureIoTHub.Portal/Shared/Models/v1.0/PaginatedResult.cs index 2b0afb995..7dfbb034e 100644 --- a/src/AzureIoTHub.Portal/Shared/Models/v1.0/PaginatedResult.cs +++ b/src/AzureIoTHub.Portal/Shared/Models/v1.0/PaginatedResult.cs @@ -14,7 +14,6 @@ public PaginatedResult(List data = default, int count = 0, int page = 0, int Data = data; CurrentPage = page; PageSize = pageSize; - TotalPages = (int)Math.Ceiling(count / (double)pageSize); TotalCount = count; } @@ -22,12 +21,12 @@ public PaginatedResult(List data = default, int count = 0, int page = 0, int public int CurrentPage { get; set; } - public int TotalPages { get; set; } - public int TotalCount { get; set; } public int PageSize { get; set; } + public int TotalPages => (int)Math.Ceiling(TotalCount / (double)PageSize); + public bool HasPreviousPage => CurrentPage > 0; public bool HasNextPage => CurrentPage < (TotalPages - 1);