Skip to content

Commit

Permalink
Simplify delete relationship snippet (#12717)
Browse files Browse the repository at this point in the history
* simplify delete relationship snippet
  • Loading branch information
Basel Rustum authored and prmathur-microsoft committed Jul 8, 2020
1 parent 436bcca commit 43df8aa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,27 +178,18 @@ public async Task RunSamplesAsync(DigitalTwinsClient client)

#endregion Snippet:DigitalTwinsSampleGetIncomingRelationships

#region Snippet:DigitalTwinsSampleDeleteAllRelationships
// Delete the contains relationship, created earlier in the sample code, from building to floor.

#region Snippet:DigitalTwinsSampleDeleteRelationship

// Delete all relationships from building to floor. These relationships were created using the BasicRelationship type.
AsyncPageable<string> buildingRelationshipsToDelete = client.GetRelationshipsAsync("buildingTwinId");
await foreach (var relationshipToDelete in buildingRelationshipsToDelete)
{
BasicRelationship relationship = JsonSerializer.Deserialize<BasicRelationship>(relationshipToDelete);
Response deleteRelationshipResponse = await client.DeleteRelationshipAsync(relationship.SourceId, relationship.Id);
Console.WriteLine($"Deleted relationship with Id {relationship.Id}. Status response: {deleteRelationshipResponse.Status}.");
}
Response deleteBuildingRelationshipResponse = await client.DeleteRelationshipAsync("buildingTwinId", "buildingFloorRelationshipId");
Console.WriteLine($"Deleted relationship with Id buildingFloorRelationshipId. Status response: {deleteBuildingRelationshipResponse.Status}.");

// Delete all relationships from floor to building. These relationships were created using the CustomRelationship type.
AsyncPageable<string> floorRelationshipsToDelete = client.GetRelationshipsAsync("floorTwinId");
await foreach (var relationshipToDelete in floorRelationshipsToDelete)
{
CustomRelationship relationship = JsonSerializer.Deserialize<CustomRelationship>(relationshipToDelete);
Response deleteRelationshipResponse = await client.DeleteRelationshipAsync(relationship.SourceId, relationship.Id);
Console.WriteLine($"Deleted relationship with Id {relationship.Id}. Status response: {deleteRelationshipResponse.Status}.");
}
#endregion Snippet:DigitalTwinsSampleDeleteRelationship

#endregion Snippet:DigitalTwinsSampleDeleteAllRelationships
// Delete the containedIn relationship, created earlier in the sample code, from floor to building.
Response deleteFloorRelationshipResponse = await client.DeleteRelationshipAsync("floorTwinId", "floorBuildingRelationshipId");
Console.WriteLine($"Deleted relationship with Id floorBuildingRelationshipId. Status response: {deleteFloorRelationshipResponse.Status}.");

// Clean up.
try
Expand Down
21 changes: 3 additions & 18 deletions sdk/digitaltwins/Azure.DigitalTwins.Core/samples/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,24 +416,9 @@ await foreach (IncomingRelationship incomingRelationship in incomingRelationship

To delete all outgoing relationships for a digital twin, simply iterate over the relationships and delete them iteratively.

```C# Snippet:DigitalTwinsSampleDeleteAllRelationships
// Delete all relationships from building to floor. These relationships were created using the BasicRelationship type.
AsyncPageable<string> buildingRelationshipsToDelete = client.GetRelationshipsAsync("buildingTwinId");
await foreach (var relationshipToDelete in buildingRelationshipsToDelete)
{
BasicRelationship relationship = JsonSerializer.Deserialize<BasicRelationship>(relationshipToDelete);
Response deleteRelationshipResponse = await client.DeleteRelationshipAsync(relationship.SourceId, relationship.Id);
Console.WriteLine($"Deleted relationship with Id {relationship.Id}. Status response: {deleteRelationshipResponse.Status}.");
}

// Delete all relationships from floor to building. These relationships were created using the CustomRelationship type.
AsyncPageable<string> floorRelationshipsToDelete = client.GetRelationshipsAsync("floorTwinId");
await foreach (var relationshipToDelete in floorRelationshipsToDelete)
{
CustomRelationship relationship = JsonSerializer.Deserialize<CustomRelationship>(relationshipToDelete);
Response deleteRelationshipResponse = await client.DeleteRelationshipAsync(relationship.SourceId, relationship.Id);
Console.WriteLine($"Deleted relationship with Id {relationship.Id}. Status response: {deleteRelationshipResponse.Status}.");
}
```C# Snippet:DigitalTwinsSampleDeleteRelationship
Response deleteBuildingRelationshipResponse = await client.DeleteRelationshipAsync("buildingTwinId", "buildingFloorRelationshipId");
Console.WriteLine($"Deleted relationship with Id buildingFloorRelationshipId. Status response: {deleteBuildingRelationshipResponse.Status}.");
```

## Create, list, and delete event routes of digital twins
Expand Down

0 comments on commit 43df8aa

Please sign in to comment.