Пример #1
0
            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");
            }
Пример #2
0
 /// <inheritdoc />
 public void Like(Like like) => this.Likes.Add(like);
Пример #3
0
 /// <inheritdoc />
 public void Like(Like like)
 {
     // Null Pattern
 }