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

fix #61 delete commands when delete a device model #63

Merged
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 @@ -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