Simplify code in AddReaction
This commit is contained in:
@@ -48,30 +48,21 @@ public class AddReaction(
|
||||
{
|
||||
var content = await context.Contents.SingleAsync(x => x.Id == req.ContentId, ct);
|
||||
var reactionEnum = req.Reaction.ToEnum<Reaction>();
|
||||
var currentReaction = content.Reactions.SingleOrDefault(x => x.UserId == req.UserId);
|
||||
|
||||
var hasReacted = content.Reactions.Any(x => x.UserId == req.UserId);
|
||||
|
||||
if (hasReacted)
|
||||
// Already reacted or reaction didn't change, do nothing
|
||||
if (currentReaction != null && currentReaction.Reaction == reactionEnum)
|
||||
{
|
||||
var currentReaction = content.Reactions.Single(x => x.UserId == req.UserId);
|
||||
if (currentReaction.Reaction == reactionEnum) return;
|
||||
return;
|
||||
}
|
||||
|
||||
if (reactionEnum.HasValue)
|
||||
// User has already reacted, remove the existing reaction
|
||||
if (currentReaction != null)
|
||||
{
|
||||
content.Reactions.Remove(currentReaction);
|
||||
|
||||
var reaction = new ContentReaction
|
||||
{
|
||||
Reaction = reactionEnum.Value,
|
||||
UserId = req.UserId,
|
||||
UserName = req.UserName
|
||||
};
|
||||
|
||||
content.Reactions.Add(reaction);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// If the new reaction is valid, add or update the reaction
|
||||
if (reactionEnum.HasValue)
|
||||
{
|
||||
var reaction = new ContentReaction
|
||||
@@ -83,7 +74,6 @@ public class AddReaction(
|
||||
|
||||
content.Reactions.Add(reaction);
|
||||
}
|
||||
}
|
||||
|
||||
await context.SaveChangesAsync(ct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user