Skip to content

Commit

Permalink
fix delete device
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeM-2ISA authored and kbeaugrand committed Mar 6, 2023
1 parent 4323ea0 commit b6ba6fe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
6 changes: 4 additions & 2 deletions src/AzureIoTHub.Portal.Server/Services/DeviceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace AzureIoTHub.Portal.Server.Services
using Domain.Exceptions;
using Domain.Repositories;
using Infrastructure;
using Microsoft.Extensions.Logging;
using Models.v10;

public class DeviceService : DeviceServiceBase<DeviceDetails>
Expand All @@ -37,8 +38,9 @@ public DeviceService(IMapper mapper,
IDeviceTagService deviceTagService,
IDeviceModelImageManager deviceModelImageManager,
IDeviceTwinMapper<DeviceListItem, DeviceDetails> deviceTwinMapper,
PortalDbContext portalDbContext)
: base(portalDbContext, mapper, externalDevicesService, deviceTagService, deviceModelImageManager, deviceTwinMapper)
PortalDbContext portalDbContext,
ILogger<DeviceService> logger)
: base(portalDbContext, mapper, externalDevicesService, deviceTagService, deviceModelImageManager, deviceTwinMapper, logger)
{
this.mapper = mapper;
this.unitOfWork = unitOfWork;
Expand Down
16 changes: 14 additions & 2 deletions src/AzureIoTHub.Portal.Server/Services/DeviceServiceBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace AzureIoTHub.Portal.Server.Services
using Infrastructure;
using Infrastructure.Repositories;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Devices.Common.Exceptions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Models.v10;
using Shared.Models.v1._0;
using Shared.Models.v10.Filters;
Expand All @@ -33,20 +35,23 @@ public abstract class DeviceServiceBase<TDto> : IDeviceService<TDto>
private readonly IDeviceTagService deviceTagService;
private readonly IDeviceModelImageManager deviceModelImageManager;
private readonly IDeviceTwinMapper<DeviceListItem, TDto> deviceTwinMapper;
private readonly ILogger<DeviceServiceBase<TDto>> logger;

protected DeviceServiceBase(PortalDbContext portalDbContext,
IMapper mapper,
IExternalDeviceService externalDevicesService,
IDeviceTagService deviceTagService,
IDeviceModelImageManager deviceModelImageManager,
IDeviceTwinMapper<DeviceListItem, TDto> deviceTwinMapper)
IDeviceTwinMapper<DeviceListItem, TDto> deviceTwinMapper,
ILogger<DeviceServiceBase<TDto>> logger)
{
this.portalDbContext = portalDbContext;
this.mapper = mapper;
this.externalDevicesService = externalDevicesService;
this.deviceTagService = deviceTagService;
this.deviceModelImageManager = deviceModelImageManager;
this.deviceTwinMapper = deviceTwinMapper;
this.logger = logger;
}

public async Task<PaginatedResult<DeviceListItem>> GetDevices(string searchText = null, bool? searchStatus = null, bool? searchState = null, int pageSize = 10,
Expand Down Expand Up @@ -181,7 +186,14 @@ public async Task<TDto> UpdateDevice(TDto device)

public virtual async Task DeleteDevice(string deviceId)
{
await this.externalDevicesService.DeleteDevice(deviceId);
try
{
await this.externalDevicesService.DeleteDevice(deviceId);
}
catch (DeviceNotFoundException e)
{
this.logger.LogWarning($"Unable to delete the device with ID {deviceId} because it doesn't exist on IoT Hub {e.Message}");
}

await DeleteDeviceInDatabase(deviceId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public LoRaWanDeviceService(
PortalDbContext portalDbContext,
IDeviceModelImageManager deviceModelImageManager,
IDeviceTwinMapper<DeviceListItem, LoRaDeviceDetails> deviceTwinMapper)
: base(portalDbContext, mapper, externalDevicesService, deviceTagService, deviceModelImageManager, deviceTwinMapper)
: base(portalDbContext, mapper, externalDevicesService, deviceTagService, deviceModelImageManager, deviceTwinMapper, logger)
{
this.logger = logger;
this.mapper = mapper;
Expand Down

0 comments on commit b6ba6fe

Please sign in to comment.