private ICollection<ProductCommentWithRatingViewModel> PopulateCommentAndVote( ICollection<Comment> comments, ICollection<Vote> votes) { var result = new List<ProductCommentWithRatingViewModel>(); foreach (var comment in comments) { var commentAndVote = new ProductCommentWithRatingViewModel(); commentAndVote.Id = comment.Id; commentAndVote.CommentContent = comment.Content; commentAndVote.CommentCreatedOn = comment.CreatedOn; commentAndVote.UserName = comment.User.UserName; foreach (var vote in votes) { if (commentAndVote.UserName == vote.User.UserName) { commentAndVote.Rating = vote.VoteValue; break; } } result.Add(commentAndVote); } return result; }
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"); }
private void PopulateViewModel(ProductCommentWithRatingViewModel commentWithRating, CommentPostViewModel commentPostViewModel) { commentWithRating.CommentContent = commentPostViewModel.Content; commentWithRating.UserName = commentPostViewModel.UserName; commentWithRating.CommentCreatedOn = commentPostViewModel.CreatedOn; }