Ejemplo n.º 1
0
        public async Task <IActionResult> Create([FromBody] CocktailCommentViewModel viewModel)
        {
            try
            {
                var user = await this.userManager.GetUserAsync(User);

                var userName = user.Email.Split('@')[0];

                viewModel.UserId   = user.Id;
                viewModel.UserName = userName;
                var commentDto = this.modelMapper.MapFrom(viewModel);

                var newCommentDto = await this.cocktailCommentService.CreateAsync(commentDto);

                var newCommentVm = this.modelMapper.MapFrom(newCommentDto);

                this.toastNotification.AddSuccessToastMessage("Comment magically posted.");

                return(PartialView("_AddCocktailCommentPartial", newCommentVm));
            }
            catch (Exception)
            {
                this.toastNotification.AddErrorToastMessage("Text must be between 2 and 500 symbols.");
            }

            return(View(viewModel));
        }
        public async Task <IActionResult> AddComment(CocktailViewModel cocktail)
        {
            int userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));
            var author = HttpContext.User.Identity.Name;

            var cocktailComment = new CocktailCommentViewModel
            {
                Text       = cocktail.CurrentComment,
                CocktailId = cocktail.Id,
                UserId     = userId,
                Author     = author
            };

            var cocktailCommentDto = this.cocktailCommentVmMapper.MapDTO(cocktailComment);

            var comment = await this.cocktailCommentService.CreateCocktailCommentAsync(cocktailCommentDto);

            var currentCocktail = await this.cocktailService.GetCokctailAsync(comment.CocktailId);

            var cocktailVm  = this.cocktailVmMapper.MapViewModel(currentCocktail);
            var dtoComments = await this.cocktailCommentService.GetCocktailCommentsAsync(cocktailVm.Id);

            cocktailVm.Comments      = this.cocktailCommentVmMapper.MapViewModel(dtoComments);
            cocktailVm.AverageRating = this.cocktailRatingService.GetAverageCocktailRating(cocktailComment.Id);

            return(RedirectToAction("Details", "Cocktails", new { area = "", id = cocktailVm.Id }));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> CommentOnCocktail(Guid id, CocktailCommentViewModel viewModel)
        {
            viewModel.UserId     = Guid.Parse(manager.GetUserId(HttpContext.User));
            viewModel.CocktailId = id;

            await cocktailCommentServices.CreateComment(viewModel.GetDtoFromVM());

            var cocktailId = viewModel.CocktailId;

            return(RedirectToAction("Details", "Cocktail", cocktailId));
        }
 public static CocktailCommentsDTO GetDtoFromVM(this CocktailCommentViewModel item)
 {
     if (item == null)
     {
         throw new ArgumentNullException();
     }
     return(new CocktailCommentsDTO
     {
         UserId = item.UserId,
         CocktailId = item.CocktailId,
         Comments = item.Comment,
     });
 }