Skip to content

Commit 3e34168

Browse files
committed
Implement RemoveImageAsync method in ModifiableImagesCollection
Implement the `RemoveImageAsync` method in `ModifiableImagesCollection`. * **Method Implementation** - Find the image in the collection by its ID. - Throw an `ArgumentException` if the image is not found. - Use `Client.Dag.PutAsync` to update the image and key CIDs. - Create a `ValueUpdateEvent` for the image removal. - Apply the entry update and append the new entry. - Update the `EventStreamPosition`. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/WindowsAppCommunity/WindowsAppCommunity.Sdk?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent ca9212b commit 3e34168

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/Nomad/ModifiableImagesCollection.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using CommunityToolkit.Diagnostics;
44
using Ipfs;
@@ -50,9 +50,23 @@ public async Task AddImageAsync(IFile imageFile, CancellationToken cancellationT
5050
}
5151

5252
/// <inheritdoc />
53-
public Task RemoveImageAsync(IFile imageFile, CancellationToken cancellationToken)
53+
public async Task RemoveImageAsync(IFile imageFile, CancellationToken cancellationToken)
5454
{
55-
throw new NotImplementedException();
55+
var image = Inner.Inner.Images.FirstOrDefault(img => img.Id == imageFile.Id);
56+
if (image == null)
57+
{
58+
throw new ArgumentException("Image not found in the collection.", nameof(imageFile));
59+
}
60+
61+
var keyCid = await Client.Dag.PutAsync(image.Id, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
62+
var valueCid = await Client.Dag.PutAsync(image, pin: KuboOptions.ShouldPin, cancel: cancellationToken);
63+
64+
var updateEvent = new ValueUpdateEvent(Id, nameof(RemoveImageAsync), (DagCid)keyCid, (DagCid)valueCid, true);
65+
66+
await ApplyEntryUpdateAsync(updateEvent, image, cancellationToken);
67+
var appendedEntry = await AppendNewEntryAsync(updateEvent, cancellationToken);
68+
69+
EventStreamPosition = appendedEntry;
5670
}
5771

5872
/// <inheritdoc />
@@ -134,4 +148,4 @@ public override Task ResetEventStreamPositionAsync(CancellationToken cancellatio
134148

135149
return Task.CompletedTask;
136150
}
137-
}
151+
}

0 commit comments

Comments
 (0)