Ejemplo n.º 1
0
        Comment ICommentsService.CreateComment(Comment comment)
        {
            // Validate the stars quantity (range 1-5)
            if (comment.Stars < 1 || comment.Stars > 5)
            {
                throw new AlreadyExistsException("The valid star range is between 1 and 5");
            }
            // Validating that it's the only comment for the idea
            var newComment = _commentsRepository.GetCommentGivenAnUserAndAnIdea(comment.GivenBy, comment.GivenTo);

            if (newComment != null)
            {
                throw new AlreadyExistsException("This user has already commented on this idea");
            }

            // Validating that Comment is posted to ideas of a different user.
            var idea = _ideasRepository.GetIdeaById(comment.GivenTo);

            if (idea != null)
            {
                if (comment.GivenBy == idea.ProposedBy)
                {
                    throw new AlreadyExistsException("Comment can only be posted to ideas that were not created by the same user");
                }
            }

            // Increasing +1 the number of Comments in Ideas
            if (idea != null)
            {
                if (!_ideasRepository.IncreaseNumberOfComments(idea.Id))
                {
                    throw new NotFoundException("Cannot find idea");
                }
            }

            // Update the average of stars ----------------------------------------------------------------
            if (idea != null)
            {
                long newAverageOfStars = _commentsRepository.GetNewAverageRegardingTheCurrentComment(idea.Id, comment.Stars);
                if (!_ideasRepository.UpdateAverageStars(idea.Id, newAverageOfStars))
                {
                    throw new NotFoundException("Cannot find idea");
                }
            }
            // --------------------------------------------------------------------------------------------

            newComment = _commentsRepository.CreateComment(comment);
            return(newComment);
        }