Ejemplo n.º 1
0
        /// <summary>
        /// Inserts a new comment to the database
        /// </summary>
        /// <param name="commentViewModel">The comment view model</param>
        /// <returns>The inserted comment Guid</returns>
        public Guid InsertComment(CreateCommentViewModel commentViewModel)
        {
            Comment commentEntity;

            //if the parent is a submission ( top level comment )
            if (commentViewModel.ParentCommentId == Guid.Empty)
            {
                commentEntity = new Comment
                {
                    Id               = Guid.NewGuid(),
                    Text             = commentViewModel.Text,
                    ParentSubmission = _submissionRepository.GetSubmissionById(commentViewModel.ParentSubmissionId),
                    Author           = _userRepository.GetUserById(commentViewModel.AuthorId)
                };
            }
            else // if the parent is another comment ( this comment is a reply )
            {
                commentEntity = new Comment
                {
                    Id            = Guid.NewGuid(),
                    Text          = commentViewModel.Text,
                    ParentComment = _commentRepository.GetCommentById(commentViewModel.ParentCommentId),
                    Author        = _userRepository.GetUserById(commentViewModel.AuthorId)
                };

                Comment AddToNrOfReplies = this._commentRepository.GetCommentById(commentViewModel.ParentCommentId);
                AddToNrOfReplies.NrOfReplies++;
            }

            this._commentRepository.AddComment(commentEntity);
            this._commentRepository.SaveChanges();
            return(commentEntity.Id);
        }