Пример #1
0
        public async Task <PostReactionDto> AddPostReactionAsync(Guid postId, PostReactionToAddDto postReaction)
        {
            _logger.LogDebug("Trying to add reaction {postReaction}.", postReaction);
            if (postReaction is null)
            {
                throw new ArgumentNullException(nameof(PostReaction));
            }
            if (postId == Guid.Empty || postReaction.FromWho == Guid.Empty)
            {
                throw new ArgumentNullException(nameof(Guid));
            }
            try
            {
                PostReaction reactionToAdd = _mapper.Map <PostReaction>(postReaction);
                reactionToAdd.PostId = postId;
                PostReaction addedReaction = await _postReactionRepository.AddAsync(reactionToAdd);

                await _postReactionRepository.SaveAsync();

                return(_mapper.Map <PostReactionDto>(addedReaction));
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error occured during adding the reaction.");
                throw;
            }
        }
Пример #2
0
        public async Task <IActionResult> addReactionToPost(int userId, int postId, PostReactionToAddDto postReactionToAddDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized("You are not the user"));
            }


            await this._db.addReactionToPost(userId, postReactionToAddDto.postId, postReactionToAddDto.reactionId);

            if (!await _db.saveAll())
            {
                return(BadRequest("Something bad happened"));
            }
            return(Ok("Added"));
        }
Пример #3
0
        public async Task <ActionResult <PostReactionDto> > AddPostReaction(string userId, string postId, [FromBody] PostReactionToAddDto reactionToAdd)
        {
            if (Guid.TryParse(postId, out Guid gPostId) && Guid.TryParse(userId, out Guid gUserId))
            {
                try
                {
                    if (_postService.CheckIfPostExists(gPostId) && await _userService.CheckIfUserExists(gUserId))
                    {
                        PostReactionDto addedReaction = await _postReactionService.AddPostReactionAsync(gPostId, reactionToAdd);

                        return(CreatedAtRoute("GetReaction",
                                              new
                        {
                            userId,
                            postId = addedReaction.PostId,
                            reactionId = addedReaction.Id
                        },
                                              addedReaction));
                    }
                    else
                    {
                        return(NotFound($"User: {userId} or post {postId} not found."));
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error occured during adding the reaction. Post id: {postId}", postId);
                    return(StatusCode(StatusCodes.Status500InternalServerError));
                }
            }
            else
            {
                return(BadRequest($"{userId} or {postId} is not valid guid."));
            }
        }
 public PostReactionsControllerAddReactionTests() : base()
 {
     _postReactionToAdd = _fixture.Create <PostReactionToAddDto>();
 }