Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 2, 2024 21:18
Show Gist options
  • Save dcomartin/fd7b0f0c1ff35e802a82ac74cb8dd1eb to your computer and use it in GitHub Desktop.
Save dcomartin/fd7b0f0c1ff35e802a82ac74cb8dd1eb to your computer and use it in GitHub Desktop.
private static async Task<Ok> Handle([AsParameters] Request request, IBus bus, AppDbContext db, ClaimsPrincipal claimsPrincipal, CancellationToken ct)
{
var userId = claimsPrincipal.GetUserId();
var doesLikeExist = await db.Likes.AnyAsync(x => x.PostId == request.Id && x.UserId == userId, ct);
if (doesLikeExist)
{
return TypedResults.Ok();
}
var like = new Like
{
PostId = request.Id,
UserId = userId
};
await db.Likes.AddAsync(like, ct);
await db.Posts.Where(x => x.Id == request.Id)
.ExecuteUpdateAsync(x => x.SetProperty(p => p.LikeCount, p => p.LikeCount + 1), cancellationToken: ct);
await db.SaveChangesAsync(ct);
await bus.Publish(new PostLiked(request.Id, userId));
return TypedResults.Ok();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment