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

Bugfix: Add missing next page on concentrators listing #1365

Merged
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 @@ -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;
Expand Down Expand Up @@ -101,6 +102,12 @@ public async Task GetListStateUnderTestExpectedBehavior()
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync(expectedPaginatedDevices);

var locationUrl = "http://location/devices";

_ = this.mockUrlHelper
.Setup(x => x.RouteUrl(It.IsAny<UrlRouteContext>()))
.Returns(locationUrl);

// Act
var result = await devicesController.SearchItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ public async Task GetAllDeviceShouldReturnOkResult()
}).ToList(),
TotalCount = 100,
PageSize = 10,
TotalPages = 10,
CurrentPage = 0
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,12 @@ public async Task GetAllDeviceConcentratorShouldReturnList()
It.IsAny<int>(), It.IsAny<int>(), It.IsAny<string[]>()))
.ReturnsAsync(expectedPaginatedConcentrator);

var locationUrl = "http://location/concentrators";

_ = this.mockUrlHelper
.Setup(x => x.RouteUrl(It.IsAny<UrlRouteContext>()))
.Returns(locationUrl);

// Act
var result = await concentratorsController.GetAllDeviceConcentrator().ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,6 +32,7 @@ public class LoRaWANDevicesControllerTests
private Mock<ILogger<LoRaWANDevicesController>> mockLogger;
private Mock<ILoRaWANCommandService> mockLoRaWANCommandService;
private Mock<IDeviceService<LoRaDeviceDetails>> mockDeviceService;
private Mock<IUrlHelper> mockUrlHelper;

[SetUp]
public void SetUp()
Expand All @@ -40,14 +42,18 @@ public void SetUp()
this.mockLogger = this.mockRepository.Create<ILogger<LoRaWANDevicesController>>();
this.mockLoRaWANCommandService = this.mockRepository.Create<ILoRaWANCommandService>();
this.mockDeviceService = this.mockRepository.Create<IDeviceService<LoRaDeviceDetails>>();
this.mockUrlHelper = this.mockRepository.Create<IUrlHelper>();
}

private LoRaWANDevicesController CreateLoRaWANDevicesController()
{
return new LoRaWANDevicesController(
this.mockLogger.Object,
this.mockLoRaWANCommandService.Object,
this.mockDeviceService.Object);
this.mockDeviceService.Object)
{
Url = this.mockUrlHelper.Object
};
}

[Test]
Expand Down Expand Up @@ -106,6 +112,12 @@ public async Task GetListStateUnderTestExpectedBehavior()
It.IsAny<Dictionary<string, string>>()))
.ReturnsAsync(expectedPaginatedDevices);

var locationUrl = "http://location/devices";

_ = this.mockUrlHelper
.Setup(x => x.RouteUrl(It.IsAny<UrlRouteContext>()))
.Returns(locationUrl);

// Act
var result = await devicesController.SearchItems();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -61,10 +61,27 @@ public async Task<ActionResult<PaginationResult<ConcentratorDto>>> 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<ConcentratorDto>
{
Items = paginatedDevices.Data,
TotalItems = paginatedDevices.TotalCount,
NextPage = nextPage
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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<Concentrator>, PaginatedResult<ConcentratorDto>>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

namespace AzureIoTHub.Portal.Server.Services
{
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Domain;
Expand Down Expand Up @@ -62,14 +61,8 @@ public async Task<PaginatedResult<ConcentratorDto>> GetAllDeviceConcentrator(
string[] orderBy = null)
{
var paginatedConcentrator = await this.concentratorRepository.GetPaginatedListAsync(pageNumber, pageSize, orderBy);
var paginatedConcentratorDto = new PaginatedResult<ConcentratorDto>
{
Data = paginatedConcentrator.Data.Select(x => mapper.Map<ConcentratorDto>(x)).ToList(),
TotalCount = paginatedConcentrator.TotalCount,
CurrentPage = paginatedConcentrator.CurrentPage,
PageSize = pageSize
};
return paginatedConcentratorDto;

return this.mapper.Map<PaginatedResult<ConcentratorDto>>(paginatedConcentrator);
}

public async Task<ConcentratorDto> GetConcentrator(string deviceId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,19 @@ public PaginatedResult(List<T> data = default, int count = 0, int page = 0, int
Data = data;
CurrentPage = page;
PageSize = pageSize;
TotalPages = (int)Math.Ceiling(count / (double)pageSize);
TotalCount = count;
}

public List<T> Data { get; set; }

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);
Expand Down