Skip to content

Commit

Permalink
Merge pull request #63 from michelin/issue_#61_delete_commands_from_d…
Browse files Browse the repository at this point in the history
…atabase_when_a_model_device_is_delete

fix #61 delete commands when delete a device model
  • Loading branch information
audserraCGI authored Dec 15, 2021
2 parents 45d7bf5 + 6da4d8c commit fe30c22
Showing 1 changed file with 18 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ public async Task<IActionResult> Put([FromForm] string deviceModel, [FromForm] I
[HttpDelete("{deviceModelID}")]
public async Task<IActionResult> 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<TableEntity>(t => t.RowKey == deviceModelID);
Expand All @@ -145,6 +147,10 @@ public async Task<IActionResult> Delete(string deviceModelID)

var deviceModel = this.deviceModelMapper.CreateDeviceModel(query.Single());

var queryCommand = this.tableClientFactory
.GetDeviceCommands()
.Query<TableEntity>(t => t.PartitionKey == deviceModel.ModelId);

foreach (var twin in deviceList)
{
if (DeviceHelper.RetrieveTagValue(twin, "modelId") == deviceModel.ModelId)
Expand All @@ -153,6 +159,18 @@ public async Task<IActionResult> 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);
Expand Down

0 comments on commit fe30c22

Please sign in to comment.