From 6da4d8c04ca7782ccf2adea6958aa4a0e8ddc14f Mon Sep 17 00:00:00 2001 From: ben salim Date: Wed, 15 Dec 2021 10:58:37 +0100 Subject: [PATCH] fix #61 --- .../Controllers/DeviceModelsController.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs b/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs index 3f9137d64..abd277666 100644 --- a/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs +++ b/src/AzureIoTHub.Portal/Server/Controllers/DeviceModelsController.cs @@ -133,7 +133,9 @@ public async Task Put([FromForm] string deviceModel, [FromForm] I [HttpDelete("{deviceModelID}")] public async Task Delete(string deviceModelID) { + // we get all devices var deviceList = await this.devicesService.GetAllDevice(); + // we get the device model with a query var query = this.tableClientFactory .GetDeviceTemplates() .Query(t => t.RowKey == deviceModelID); @@ -145,6 +147,10 @@ public async Task Delete(string deviceModelID) var deviceModel = this.deviceModelMapper.CreateDeviceModel(query.Single()); + var queryCommand = this.tableClientFactory + .GetDeviceCommands() + .Query(t => t.PartitionKey == deviceModel.ModelId); + foreach (var twin in deviceList) { if (DeviceHelper.RetrieveTagValue(twin, "modelId") == deviceModel.ModelId) @@ -153,6 +159,18 @@ public async Task Delete(string deviceModelID) } } + var commands = queryCommand.Select(item => this.deviceModelCommandMapper.GetDeviceModelCommand(item)).ToList(); + + // if we have command + if (commands.Count > 0) + { + foreach (var item in commands) + { + _ = await this.tableClientFactory + .GetDeviceCommands().DeleteEntityAsync(deviceModelID, item.CommandId); + } + } + var result = await this.tableClientFactory .GetDeviceTemplates() .DeleteEntityAsync(DefaultPartitionKey, deviceModelID);