示例#1
0
 public OperationDetails Create(CommentViewModelBLL comment)
 {
     Mapper.Initialize(cfg => cfg.CreateMap <CommentViewModelBLL, Comment>()
                       .ForMember("UserId", opt => opt.MapFrom(c => c.PerformerId)));
     _commentRepository.Create(Mapper.Map <CommentViewModelBLL, Comment>(comment));
     return(new OperationDetails(true, "Sending comment succeeded", string.Empty));
 }
        public ActionResult Send(CreateCommentViewModel comment)
        {
            if (!User.IsInRole("user"))
            {
                throw new Exception("User is not authorized");
            }

            bool commentIsEmpty = comment.Text == null || comment.Text.Equals(string.Empty);

            Mapper.Initialize(cfg => cfg.CreateMap <CreateCommentViewModel, CommentViewModelBLL>()
                              .ForMember("Date", opt => opt.MapFrom(c => DateTime.Now))
                              .ForMember("CustomerId", opt => opt.MapFrom(c => User.Identity.GetUserId <int>()))
                              .ForMember("Rating", opt => opt.MapFrom(c => c.Rating.ToCharArray().Where(r => r == '★').Count()))
                              );
            CommentViewModelBLL commentDto = Mapper.Map <CreateCommentViewModel, CommentViewModelBLL>(comment);

            if (ModelState.IsValid)
            {
                OperationDetails operationDetails = _commentService.Create(commentDto);
                _unitOfWork.Save();
                if (!operationDetails.Succedeed)
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }

                var user = _userService.FindById(comment.PerformerId);
                if (user.CommentsBll != null)
                {
                    user.Rating = (int)Math.Round((float)(user.CommentsBll.Select(c => c.Rating).Sum() / user.CommentsBll.Count));
                }
                operationDetails = _userService.Update(user);
                _unitOfWork.Save();
                if (!operationDetails.Succedeed)
                {
                    ModelState.AddModelError(operationDetails.Property, operationDetails.Message);
                }
            }
            else
            {
                throw new Exception("Model is not valid");
            }

            return(RedirectToAction("Details", "Performers", new { id = comment.PerformerId, emptyComment = commentIsEmpty }));
        }