Example #1
0
        public IDictionary <ReactionState, int> AddReaction(long postId, CreateReactionDto createReactionDto)
        {
            if (!this.queryService.Query <Post>().Any(x => x.Id == postId))
            {
                throw new ApiException(this.apiResultService.NotFoundResult(AtanetEntityName.Post, postId));
            }

            var currentUserId = this.userService.GetCurrentUserId();

            if (!this.scoreService.Can(AtanetAction.VotePost, currentUserId))
            {
                var minScore = this.scoreService.GetMinScore(AtanetAction.VotePost);
                throw new ApiException(this.apiResultService.BadRequestResult($"Must have a score greater than {minScore} in order to vote for posts"));
            }

            var postCreator = this.queryService.Query <Post>().Where(x => x.Id == postId).Select(x => x.UserId).Single();

            if (postCreator == currentUserId)
            {
                throw new ApiException(this.apiResultService.BadRequestResult("You cannot react on a post you have authored"));
            }

            var state = this.GetReactionState(createReactionDto.ReactionState.Value);

            using (var unitOfWork = this.unitOfWorkFactory.CreateUnitOfWork())
            {
                var postReactionRepository = unitOfWork.CreateEntityRepository <PostReaction>();
                var existing = postReactionRepository.Query().FirstOrDefault(x => x.PostId == postId && x.UserId == currentUserId);
                if (existing == null)
                {
                    postReactionRepository.Create(new PostReaction
                    {
                        PostId        = postId,
                        ReactionState = state,
                        UserId        = currentUserId
                    });
                }
                else
                {
                    existing.ReactionState = state;
                    postReactionRepository.Update(existing);
                }

                unitOfWork.Save();
            }

            var reactions = this.queryService.Query <PostReaction>().Where(x => x.PostId == postId)
                            .GroupBy(x => x.ReactionState).ToDictionary(x => x.Key, x => x.Count());

            return(reactions);
        }
Example #2
0
        public async Task <ActionResult <ReactionDto> > CreateReaction(int userId, CreateReactionDto createReactionDto)
        {
            var reaction = new Reaction
            {
                ActionOf = createReactionDto.ActionOf,
                DoAction = (IPlanAction.ActionType)createReactionDto.DoAction,
                Strength = createReactionDto.Strength,
                ToWhat   = createReactionDto.ToWhat,
                Type     = (IPlanAction.ReactionType)createReactionDto.Type,
                User     = await _unitOfWork.UserRepository.GetUserByIdAsync(userId)
            };

            _unitOfWork.ReactionRepository.AddItem(reaction);
            if (await _unitOfWork.Complete())
            {
                return(Ok(_mapper.Map <ReactionDto>(reaction)));
            }

            return(BadRequest("Unable to create Reaction"));
        }
        public IActionResult AddPostReaction(long postId, [FromBody] CreateReactionDto createReactionDto)
        {
            var newReactionState = this.postReactionCreationService.AddReaction(postId, createReactionDto);

            return(this.apiResultService.Ok(newReactionState));
        }