diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs index 9e03dcb85..762b276fa 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/DeviceServiceTests.cs @@ -425,7 +425,13 @@ public async Task DeleteDevice_DeviceExist_DeviceDeleted() .Returns(Task.CompletedTask); _ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID)) - .ReturnsAsync(new Device()); + .ReturnsAsync(new Device + { + Tags = Fixture.CreateMany(5).ToList() + }); + + this.mockDeviceTagValueRepository.Setup(repository => repository.Delete(It.IsAny())) + .Verifiable(); this.mockDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID)) .Verifiable(); @@ -475,7 +481,10 @@ public async Task DeleteDevice_DbUpdateExceptionIsRaised_InternalServerErrorExce .Returns(Task.CompletedTask); _ = this.mockDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID)) - .ReturnsAsync(new Device()); + .ReturnsAsync(new Device + { + Tags = new List() + }); this.mockDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID)) .Verifiable(); diff --git a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs index 1f4d7da7d..b9fa61be9 100644 --- a/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs +++ b/src/AzureIoTHub.Portal.Tests.Unit/Server/Services/LoRaWanDeviceServiceTests.cs @@ -328,7 +328,13 @@ public async Task DeleteDevice_DeviceExist_DeviceDeleted() .Returns(Task.CompletedTask); _ = this.mockLorawanDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID)) - .ReturnsAsync(new LorawanDevice()); + .ReturnsAsync(new LorawanDevice + { + Tags = Fixture.CreateMany(5).ToList() + }); + + this.mockDeviceTagValueRepository.Setup(repository => repository.Delete(It.IsAny())) + .Verifiable(); this.mockLorawanDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID)) .Verifiable(); @@ -378,7 +384,10 @@ public async Task DeleteDevice_DbUpdateExceptionIsRaised_InternalServerErrorExce .Returns(Task.CompletedTask); _ = this.mockLorawanDeviceRepository.Setup(repository => repository.GetByIdAsync(deviceDto.DeviceID)) - .ReturnsAsync(new LorawanDevice()); + .ReturnsAsync(new LorawanDevice + { + Tags = new List() + }); this.mockLorawanDeviceRepository.Setup(repository => repository.Delete(deviceDto.DeviceID)) .Verifiable(); diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs index 1312f86f2..6a1defc9d 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceService.cs @@ -59,10 +59,8 @@ public override async Task GetDevice(string deviceId) return deviceDto; } - public override async Task CreateDevice(DeviceDetails device) + protected override async Task CreateDeviceInDatabase(DeviceDetails device) { - _ = base.CreateDevice(device); - try { var deviceEntity = this.mapper.Map(device); @@ -78,10 +76,8 @@ public override async Task CreateDevice(DeviceDetails device) } } - public override async Task UpdateDevice(DeviceDetails device) + protected override async Task UpdateDeviceInDatabase(DeviceDetails device) { - _ = await base.UpdateDevice(device); - try { var deviceEntity = await this.deviceRepository.GetByIdAsync(device.DeviceID); @@ -109,10 +105,8 @@ public override async Task UpdateDevice(DeviceDetails device) } } - public override async Task DeleteDevice(string deviceId) + protected override async Task DeleteDeviceInDatabase(string deviceId) { - await base.DeleteDevice(deviceId); - try { var deviceEntity = await this.deviceRepository.GetByIdAsync(deviceId); @@ -122,6 +116,11 @@ public override async Task DeleteDevice(string deviceId) return; } + foreach (var deviceTagEntity in deviceEntity.Tags) + { + this.deviceTagValueRepository.Delete(deviceTagEntity.Id); + } + this.deviceRepository.Delete(deviceId); await this.unitOfWork.SaveAsync(); diff --git a/src/AzureIoTHub.Portal/Server/Services/DeviceServiceBase.cs b/src/AzureIoTHub.Portal/Server/Services/DeviceServiceBase.cs index 9297493a3..7ee4c5890 100644 --- a/src/AzureIoTHub.Portal/Server/Services/DeviceServiceBase.cs +++ b/src/AzureIoTHub.Portal/Server/Services/DeviceServiceBase.cs @@ -131,7 +131,7 @@ public async Task> GetDevices(string searchText public abstract Task GetDevice(string deviceId); - public virtual async Task CreateDevice(TDto device) + public async Task CreateDevice(TDto device) { var newTwin = await this.externalDevicesService.CreateNewTwinFromDeviceId(device.DeviceID); @@ -140,10 +140,12 @@ public virtual async Task CreateDevice(TDto device) _ = await this.externalDevicesService.CreateDeviceWithTwin(device.DeviceID, false, newTwin, status); - return device; + return await CreateDeviceInDatabase(device); } - public virtual async Task UpdateDevice(TDto device) + protected abstract Task CreateDeviceInDatabase(TDto device); + + public async Task UpdateDevice(TDto device) { // Device status (enabled/disabled) has to be dealt with afterwards var currentDevice = await this.externalDevicesService.GetDevice(device.DeviceID); @@ -159,14 +161,20 @@ public virtual async Task UpdateDevice(TDto device) _ = await this.externalDevicesService.UpdateDeviceTwin(currentTwin); - return device; + return await UpdateDeviceInDatabase(device); } + protected abstract Task UpdateDeviceInDatabase(TDto device); + public virtual async Task DeleteDevice(string deviceId) { await this.externalDevicesService.DeleteDevice(deviceId); + + await DeleteDeviceInDatabase(deviceId); } + protected abstract Task DeleteDeviceInDatabase(string deviceId); + public virtual Task GetCredentials(string deviceId) { return this.externalDevicesService.GetEnrollmentCredentials(deviceId); diff --git a/src/AzureIoTHub.Portal/Server/Services/LoRaWanDeviceService.cs b/src/AzureIoTHub.Portal/Server/Services/LoRaWanDeviceService.cs index 85a14a7ef..1e88beb37 100644 --- a/src/AzureIoTHub.Portal/Server/Services/LoRaWanDeviceService.cs +++ b/src/AzureIoTHub.Portal/Server/Services/LoRaWanDeviceService.cs @@ -60,10 +60,8 @@ public override async Task GetDevice(string deviceId) return deviceDto; } - public override async Task CreateDevice(LoRaDeviceDetails device) + protected override async Task CreateDeviceInDatabase(LoRaDeviceDetails device) { - _ = base.CreateDevice(device); - try { var deviceEntity = this.mapper.Map(device); @@ -79,10 +77,8 @@ public override async Task CreateDevice(LoRaDeviceDetails dev } } - public override async Task UpdateDevice(LoRaDeviceDetails device) + protected override async Task UpdateDeviceInDatabase(LoRaDeviceDetails device) { - _ = await base.UpdateDevice(device); - try { var deviceEntity = await this.lorawanDeviceRepository.GetByIdAsync(device.DeviceID); @@ -110,10 +106,8 @@ public override async Task UpdateDevice(LoRaDeviceDetails dev } } - public override async Task DeleteDevice(string deviceId) + protected override async Task DeleteDeviceInDatabase(string deviceId) { - await base.DeleteDevice(deviceId); - try { var deviceEntity = await this.lorawanDeviceRepository.GetByIdAsync(deviceId); @@ -123,6 +117,11 @@ public override async Task DeleteDevice(string deviceId) return; } + foreach (var deviceTagEntity in deviceEntity.Tags) + { + this.deviceTagValueRepository.Delete(deviceTagEntity.Id); + } + this.lorawanDeviceRepository.Delete(deviceId); await this.unitOfWork.SaveAsync();