Skip to content

Commit 44379af

Browse files
authored
Merge pull request #30 from WindowsAppCommunity/Arlodotexe/imagescollection/implement-remove-image
Implement RemoveImageAsync method in ModifiableImagesCollection
2 parents ca9212b + 3e34168 commit 44379af

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

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)