🐛 Post reaction fixes
This commit is contained in:
@ -216,8 +216,8 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
|
||||
|
||||
[HttpPost("{id:long}/reactions")]
|
||||
[Authorize]
|
||||
[RequiredPermission("global", "posts.reactions.create")]
|
||||
public async Task<ActionResult<PostReaction>> CreatePostReaction(long id, [FromBody] PostReactionRequest request)
|
||||
[RequiredPermission("global", "posts.react")]
|
||||
public async Task<ActionResult<PostReaction>> ReactPost(long id, [FromBody] PostReactionRequest request)
|
||||
{
|
||||
HttpContext.Items.TryGetValue("CurrentUser", out var currentUserValue);
|
||||
if (currentUserValue is not Account.Account currentUser) return Unauthorized();
|
||||
@ -241,9 +241,9 @@ public class PostController(AppDatabase db, PostService ps, RelationshipService
|
||||
PostId = post.Id,
|
||||
AccountId = currentUser.Id
|
||||
};
|
||||
await ps.ModifyPostVotes(post, reaction, isExistingReaction);
|
||||
var isRemoving = await ps.ModifyPostVotes(post, reaction, isExistingReaction);
|
||||
|
||||
if (isExistingReaction) return NoContent();
|
||||
if (isRemoving) return NoContent();
|
||||
return Ok(reaction);
|
||||
}
|
||||
|
||||
|
@ -174,27 +174,39 @@ public class PostService(AppDatabase db, FileService fs, ActivityService act)
|
||||
/// <param name="post">Post that modifying</param>
|
||||
/// <param name="reaction">The new / target reaction adding / removing</param>
|
||||
/// <param name="isRemoving">Indicate this operation is adding / removing</param>
|
||||
public async Task ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving)
|
||||
public async Task<bool> ModifyPostVotes(Post post, PostReaction reaction, bool isRemoving)
|
||||
{
|
||||
var isExistingReaction = await db.Set<PostReaction>()
|
||||
.AnyAsync(r => r.PostId == post.Id && r.AccountId == reaction.AccountId);
|
||||
if (isExistingReaction) return;
|
||||
|
||||
if (!isRemoving)
|
||||
if (isRemoving)
|
||||
await db.PostReactions
|
||||
.Where(r => r.PostId == post.Id && r.Symbol == reaction.Symbol && r.AccountId == reaction.AccountId)
|
||||
.ExecuteDeleteAsync();
|
||||
else
|
||||
db.PostReactions.Add(reaction);
|
||||
|
||||
if (isExistingReaction)
|
||||
{
|
||||
db.Add(reaction);
|
||||
switch (reaction.Attitude)
|
||||
{
|
||||
case PostReactionAttitude.Positive:
|
||||
post.Upvotes++;
|
||||
break;
|
||||
case PostReactionAttitude.Negative:
|
||||
post.Downvotes++;
|
||||
break;
|
||||
}
|
||||
if (!isRemoving)
|
||||
await db.SaveChangesAsync();
|
||||
return isRemoving;
|
||||
}
|
||||
|
||||
switch (reaction.Attitude)
|
||||
{
|
||||
case PostReactionAttitude.Positive:
|
||||
if (isRemoving) post.Upvotes--;
|
||||
else post.Upvotes++;
|
||||
break;
|
||||
case PostReactionAttitude.Negative:
|
||||
if (isRemoving) post.Downvotes--;
|
||||
else post.Downvotes++;
|
||||
break;
|
||||
}
|
||||
|
||||
await db.SaveChangesAsync();
|
||||
return isRemoving;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, int>> GetPostReactionMap(long postId)
|
||||
|
Reference in New Issue
Block a user