public async Task <PostDto> Handle(Command request, CancellationToken cancellationToken) { var post = await context.Posts .Include(p => p.AppUser) .Include(p => p.Likes) .ThenInclude(l => l.AppUser) .FirstOrDefaultAsync(p => p.PostId == request.PostId); if (post == null) { throw new RestException(HttpStatusCode.NotFound, new { post = "Post not found" }); } var user = await userManager.FindByNameAsync(userAccessor.GetCurrentUser()); var checkLike = await context.Likes.FirstOrDefaultAsync(l => l.Post == post && l.AppUser == user); if (checkLike == null) { var like = new Domain.Like { Post = post, AppUser = user }; context.Likes.Add(like); } else { context.Likes.Remove(checkLike); } var result = await context.SaveChangesAsync() > 0; var postDto = mapper.Map <Post, PostDto>(post); if (result) { return(postDto); } throw new Exception("Problem Liking the post"); }
/// <inheritdoc /> public void Like(Like like) => this.Likes.Add(like);
/// <inheritdoc /> public void Like(Like like) { // Null Pattern }