Skip to content

Commit

Permalink
refactor(samples): Refactor the delete digital twin sample snippet (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
abhipsaMisra authored Jun 11, 2020
1 parent 8d8a6fd commit f1bc212
Showing 1 changed file with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,21 @@ public async Task DeleteTwinsAsync()

foreach (KeyValuePair<string, string> twin in twins)
{
var digitalTwinId = twin.Key;

try
{
// Delete all relationships
AsyncPageable<string> relationships = client.GetRelationshipsAsync(twin.Key);
AsyncPageable<string> relationships = client.GetRelationshipsAsync(digitalTwinId);
await foreach (var relationshipJson in relationships)
{
BasicRelationship relationship = JsonSerializer.Deserialize<BasicRelationship>(relationshipJson);
await client.DeleteRelationshipAsync(twin.Key, relationship.Id);
await client.DeleteRelationshipAsync(digitalTwinId, relationship.Id);
Console.WriteLine($"Found and deleted relationship with Id {relationship.Id}.");
}

// Delete any incoming relationships
AsyncPageable<IncomingRelationship> incomingRelationships = client.GetIncomingRelationshipsAsync(twin.Key);
AsyncPageable<IncomingRelationship> incomingRelationships = client.GetIncomingRelationshipsAsync(digitalTwinId);

await foreach (IncomingRelationship incomingRelationship in incomingRelationships)
{
Expand All @@ -217,7 +219,6 @@ public async Task DeleteTwinsAsync()
}

// Now the digital twin should be safe to delete
string digitalTwinId = twin.Key;

#region Snippet:DigitalTwinsSampleDeleteTwin

Expand All @@ -233,7 +234,7 @@ public async Task DeleteTwinsAsync()
}
catch (RequestFailedException ex)
{
FatalError($"Failed to delete {twin.Key} due to {ex.Message}");
FatalError($"Failed to delete {digitalTwinId} due to {ex.Message}");
}
}
}
Expand Down

0 comments on commit f1bc212

Please sign in to comment.