diff --git a/src/CrowdParlay.Social.Infrastructure.Persistence/Services/ReactionsRepository.cs b/src/CrowdParlay.Social.Infrastructure.Persistence/Services/ReactionsRepository.cs index fdde850..92a4dc1 100644 --- a/src/CrowdParlay.Social.Infrastructure.Persistence/Services/ReactionsRepository.cs +++ b/src/CrowdParlay.Social.Infrastructure.Persistence/Services/ReactionsRepository.cs @@ -50,7 +50,7 @@ OPTIONAL MATCH (viewer)-[reaction:REACTED_TO]->(subject) DELETE reaction - WITH viewer, subject + WITH DISTINCT viewer, subject FOREACH (newReactionValue IN $reactions | CREATE (viewer)-[:REACTED_TO { Value: newReactionValue }]->(subject) ) diff --git a/tests/CrowdParlay.Social.IntegrationTests/Tests/ReactionsRepositoryTests.cs b/tests/CrowdParlay.Social.IntegrationTests/Tests/ReactionsRepositoryTests.cs index 7425219..b76db6e 100644 --- a/tests/CrowdParlay.Social.IntegrationTests/Tests/ReactionsRepositoryTests.cs +++ b/tests/CrowdParlay.Social.IntegrationTests/Tests/ReactionsRepositoryTests.cs @@ -44,4 +44,33 @@ public async Task SetReactions_OnlyWorksWithAllowedReactionSets() await reactWithNewInvalid.Should().ThrowAsync(); reactions.Should().BeEquivalentTo(thumbs); } + + [Fact(DisplayName = "Setting reactions overwrites existing reactions")] + public async Task SetReactions_OverwritesExistingReactions() + { + // Arrange + await using var scope = _services.CreateAsyncScope(); + var authorsRepository = scope.ServiceProvider.GetRequiredService(); + var discussionsRepository = scope.ServiceProvider.GetRequiredService(); + var reactionsRepository = scope.ServiceProvider.GetRequiredService(); + + var viewerId = Guid.NewGuid(); + await authorsRepository.EnsureCreatedAsync(viewerId); + var discussionId = await discussionsRepository.CreateAsync(viewerId, "Title", "Description"); + + // Act + await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet { "a" }); + await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet { "a", "b" }); + await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet { "a", "b", "c" }); + await reactionsRepository.SetAsync(discussionId, viewerId, new HashSet { "b", "d" }); + + // Assert + var discussion = await discussionsRepository.GetByIdAsync(discussionId, viewerId); + discussion.ViewerReactions.Should().BeEquivalentTo(["b", "d"]); + discussion.ReactionCounters.Should().BeEquivalentTo(new Dictionary + { + { "b", 1 }, + { "d", 1 } + }); + } } \ No newline at end of file