コード例 #1
0
        public async Task <IActionResult> PostComment(long targetObjectID, string comment)
        {
            var result     = string.Empty;
            var theComment = new CommentPostViewModel()
            {
                Details        = comment,
                TargetObjectID = targetObjectID
            };

            using (var httpClient = new HttpClient())
            {
                var requestUri = $"{Configuration.DefaultAPI}/comments/post";
                httpClient.DefaultRequestHeaders.Add("CurrentUserID", CurrentUserID.ToString());
                httpClient.DefaultRequestHeaders.Add("CurrentHomeID", CurrentHomeID.ToString());

                using (var response = await httpClient.PostAsJsonAsync(requestUri, theComment))
                {
                    string content = await response.Content.ReadAsStringAsync();

                    if (response.IsSuccessStatusCode)
                    {
                        result = content;
                    }
                }
            }

            return(Json(result));
        }
コード例 #2
0
 private void PopulateEntity(Comment newComment, CommentPostViewModel commentPostViewModel)
 {
     newComment.Content = commentPostViewModel.Content;
     newComment.ProductId = commentPostViewModel.ProductId;
     newComment.CreatedOn = commentPostViewModel.CreatedOn;
     newComment.ModifiedOn = commentPostViewModel.ModifiedOn;
 }
コード例 #3
0
        public static CommentDTO CommentPostViewModel_To_CommentDTO(CommentPostViewModel commentPostViewModel)
        {
            var        mapper     = new MapperConfiguration(cfg => cfg.CreateMap <CommentPostViewModel, CommentDTO>()).CreateMapper();
            CommentDTO commentDTO = mapper.Map <CommentPostViewModel, CommentDTO>(commentPostViewModel);

            return(commentDTO);
        }
コード例 #4
0
        public ActionResult Post([FromBody] CommentPostViewModel comment)
        {
            try
            {
                var currentUserID = 0l;
                var currentHomeID = 0l;
                var re            = Request;
                var headers       = re.Headers;

                if (headers.ContainsKey("CurrentUserID"))
                {
                    currentUserID = long.Parse(headers.GetCommaSeparatedValues("CurrentUserID").FirstOrDefault());
                }

                if (headers.ContainsKey("CurrentHomeID"))
                {
                    currentHomeID = long.Parse(headers.GetCommaSeparatedValues("CurrentHomeID").FirstOrDefault());
                }

                comment = new BaseCRUDService(currentUserID).Save(comment);
                new BaseCRUDService(currentUserID).AddUserAsParticipantToObject(comment.TargetObjectID, currentUserID, comment.ObjectTypeID);
                var result = new CommentsRenderer(currentUserID).BuildPostComment(comment);

                return(Ok(result));
            }
            catch (System.Exception ex)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError, $"{ex.Message}"));
            }
        }
コード例 #5
0
        public ActionResult CreateComment(CommentPostViewModel commentPostViewModel)
        {
            if (commentPostViewModel != null && ModelState.IsValid)
            {
                var userId = this.usersService.GetByUserName(commentPostViewModel.UserName).Id;
                commentPostViewModel.CreatedOn = DateTime.Now;

                var newComment = new Comment();
                this.PopulateEntity(newComment, commentPostViewModel);
                newComment.UserId = userId;
                this.commentsService.Insert(newComment);

                var voteEntity = this.usersService.GetByUserName(commentPostViewModel.UserName).Votes.FirstOrDefault(v => v.ProductId == commentPostViewModel.ProductId);
                int? rating = voteEntity != null ? (int?)voteEntity.VoteValue : null;

                var commentWithRating = new ProductCommentWithRatingViewModel();
                this.PopulateViewModel(commentWithRating, commentPostViewModel);
                commentWithRating.Rating = rating;
                this.ViewData["rating-id"] = newComment.Id;

                return this.PartialView("_ProductCommentWithRating", commentWithRating);
            }

            throw new HttpException(400, "Invalid comment");
        }
コード例 #6
0
        public string postComment(CommentPostViewModel postComment)
        {
            if (this.User.Identity.IsAuthenticated)
            {
                if (ModelState.IsValid)
                {
                    this.User.Identity.GetUserId();

                    CommentDTO commentDTO = MapperModule.CommentPostViewModel_To_CommentDTO(postComment);

                    commentDTO.userID = this.User.Identity.GetUserId();

                    try
                    {
                        logger.Info("User try add comment");
                        commentService.PostComment(commentDTO);
                        logger.Info("Comment succesfully added");
                    }
                    catch (DbEntityValidationException ex)
                    {
                        logger.Error(ex, "Comment not saved to the Database validation of entities fails");
                        return(JsonConvert.SerializeObject(new ErrorResponse {
                            success = false, message = ex.Message
                        }));
                    }
                    catch (DbUpdateException ex)
                    {
                        logger.Error(ex, "Comment not saved to the Database");
                        return(JsonConvert.SerializeObject(new ErrorResponse {
                            success = false, message = ex.Message
                        }));
                    }
                    catch (DataException ex)
                    {
                        logger.Error(ex, "Comment not saved to the Database");
                        return(JsonConvert.SerializeObject(new ErrorResponse {
                            success = false, message = ex.Message
                        }));
                    }

                    catch (SystemException ex)
                    {
                        logger.Error(ex, "Exeption occured when user post new comment");
                        return(JsonConvert.SerializeObject(new ErrorResponse {
                            success = false, message = ex.Message
                        }));
                    }


                    return(JsonConvert.SerializeObject(new { postComment.content, postComment.parent, success = true }));
                }
                return(JsonConvert.SerializeObject(new ErrorResponse {
                    success = false, message = "Send data is't valid"
                }));
            }
            return(JsonConvert.SerializeObject(new ErrorResponse {
                success = false, message = "Login please"
            }));
        }
コード例 #7
0
        public ActionResult Create()
        {
            CommentPostViewModel commentPostViewModel = new CommentPostViewModel();

            commentPostViewModel.Posts = _postService.GetAll().ToList();

            return(View(commentPostViewModel));
        }
コード例 #8
0
 public IEnumerable <CommentViewModel> Comment(CommentPostViewModel model)
 {
     if (ModelState.IsValid)
     {
         _service.AddComment(model, GetClientIp());
         return(_service.RetrieveCommentList(model.ArticleID));
     }
     throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState));
 }
        public ActionResult Details(int id)
        {
            var vm = new CommentPostViewModel();

            vm.Post     = _postRepository.GetPublishedPostById(id);
            vm.Comments = _commentRepository.GetAll(id, _userProfileRepository);

            return(View(vm));
        }
コード例 #10
0
        public ActionResult Edit(int id)
        {
            Comment comment = _commentService.Find(id);

            CommentPostViewModel commentPostViewModel = new CommentPostViewModel();

            commentPostViewModel.Posts   = _postService.GetAll().ToList();
            commentPostViewModel.Comment = comment;

            return(View(commentPostViewModel));
        }
コード例 #11
0
        public async Task <IActionResult> Create(CommentPostViewModel commentViewModel)
        {
            if (commentViewModel == null)
            {
                return(BadRequest());
            }

            commentViewModel.Comment.PostId = commentViewModel.PostId;
            commentRepository.Create(commentViewModel.Comment);
            await commentRepository.SaveAsync();

            return(Redirect(commentViewModel.ReturnUrl));
        }
コード例 #12
0
        public ActionResult Create(CommentPostViewModel commentPostView)
        {
            if (ModelState.IsValid)
            {
                commentPostView.Comment.CommentTime = DateTime.Now;
                commentPostView.Comment.PostId      = commentPostView.PostId;

                _commentService.Add(commentPostView.Comment);

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #13
0
        public ActionResult Edit(int id, CommentPostViewModel commentPostViewModel)
        {
            if (ModelState.IsValid)
            {
                Comment editingComment = _commentService.Find(id);
                editingComment.Name          = commentPostViewModel.Comment.Name;
                editingComment.Email         = commentPostViewModel.Comment.Email;
                editingComment.PostId        = commentPostViewModel.PostId;
                editingComment.CommentHeader = commentPostViewModel.Comment.CommentHeader;
                editingComment.CommentText   = commentPostViewModel.Comment.CommentText;

                _commentService.Update(editingComment);

                return(RedirectToAction("Index"));
            }

            return(View());
        }
コード例 #14
0
        /// <summary>
        /// コメントを登録する
        /// </summary>
        /// <param name="comment"></param>
        /// <param name="clientIp"></param>
        /// <returns></returns>
        public bool AddComment(CommentPostViewModel postedComment, string clientIp)
        {
            Comment comment = new Comment();

            comment.InjectFrom(postedComment);

            comment.AddedBy    = HttpUtility.HtmlEncode(comment.AddedBy);
            comment.AddedByWeb = HttpUtility.HtmlEncode(comment.AddedByWeb);
            comment.AddedByIP  = clientIp;
            comment.Body       = HttpUtility.HtmlEncode(comment.Body);

            comment.UpdatedBy   = comment.AddedBy;
            comment.AddedDate   = DateTime.Now;
            comment.UpdatedDate = DateTime.Now;

            _repository.AddComment(comment);
            return(_repository.SaveChanges() == 1);
        }
コード例 #15
0
        public async Task <ActionResult> Comment(CommentPostViewModel c)
        {
            if (ModelState.IsValid)
            {
                var model = new ApplicationComment
                {
                    CommentContent = c.Comment,
                    UserId         = c.UserId,
                    PostId         = c.PostId
                };

                db.Comments.Add(model);
                await db.SaveChangesAsync();

                c.PostId = model.Id;
                return(RedirectToAction("Index", "Post", model.PostId));
            }
            return(HttpNotFound());
        }
コード例 #16
0
 public string BuildPostComment(CommentPostViewModel comment)
 {
     return($"<strong>{BaseService.GetEntity<UserPostViewModel>(comment.CreatedByID).Name}</strong>: {comment.Details} <br/> <strong><span class=\"text-muted\" style=\"font-size: x-small;\">{comment.CreatedDate.ToString("dd'/'MM'/'yyyy HH:mm:ss")}</span></strong>");
 }
コード例 #17
0
 private void PopulateViewModel(ProductCommentWithRatingViewModel commentWithRating, CommentPostViewModel commentPostViewModel)
 {
     commentWithRating.CommentContent = commentPostViewModel.Content;
     commentWithRating.UserName = commentPostViewModel.UserName;
     commentWithRating.CommentCreatedOn = commentPostViewModel.CreatedOn;
 }