-
-
Save dcomartin/fd7b0f0c1ff35e802a82ac74cb8dd1eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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