public async Task <IActionResult> RateCocktail(CocktailReviewViewModel cocktailVm) { if (ModelState.IsValid) { try { var cocktailDto = cocktailVm.MapToCocktailDto(); string userId = this.User.FindFirstValue(ClaimTypes.NameIdentifier); var cocktailName = await _cocktailServices.GetCocktailNameById(cocktailDto.Id); await _reviewServices.CreateCocktailReview(userId, cocktailDto); _toast.AddSuccessToastMessage($"You successfully rated \"{cocktailName}\" cocktail"); return(RedirectToAction("ListCocktails", "Cocktails", new { area = "Cocktails" })); } catch (Exception ex) { _toast.AddErrorToastMessage(ex.Message); ViewBag.ErrorTitle = ""; return(View("Error")); } } else { _toast.AddErrorToastMessage("You submitted invalid review! " + "Both Rating and Descrition are required parameters. Description must be between 5 and 500 symbols."); return(RedirectToAction("RateCocktail", cocktailVm.CocktailID)); } }
public static CocktailDto MapToCocktailDto(this CocktailReviewViewModel cocktailVm) { var cocktailDto = new CocktailDto(); cocktailDto.Id = cocktailVm.Id; cocktailDto.Description = cocktailVm.Description; cocktailDto.Rating = (double)Math.Round((decimal)(cocktailVm.Rating ?? 0), 2); return(cocktailDto); }
public CocktailReviewDTO MapToDTOFromVM(CocktailReviewViewModel cocktailReviewVM) { var reviewDTO = new CocktailReviewDTO { Comment = cocktailReviewVM.Comment, CocktailId = cocktailReviewVM.CocktailId, AuthorId = cocktailReviewVM.AuthorId, CocktailName = cocktailReviewVM.CocktailName, Author = cocktailReviewVM.Author, Rating = cocktailReviewVM.Rating }; return(reviewDTO); }
public CocktailReviewViewModel MapToVMFromDTO(CocktailReviewDTO cocktailReviewDTO) { var reviewVM = new CocktailReviewViewModel { Rating = cocktailReviewDTO.Rating, Comment = cocktailReviewDTO.Comment, CocktailId = cocktailReviewDTO.CocktailId, AuthorId = cocktailReviewDTO.AuthorId, CocktailName = cocktailReviewDTO.CocktailName, Author = cocktailReviewDTO.Author }; return(reviewVM); }
public static CocktailReviewViewModel CocktailReviewsDTOMapToVM(this CocktailReviewDTO cocktailReviewDTO) { var cocktailReview = new CocktailReviewViewModel(); cocktailReview.Id = cocktailReviewDTO.Id; cocktailReview.Rating = cocktailReviewDTO.Rating; cocktailReview.Comment = cocktailReviewDTO.Comment; cocktailReview.CocktailId = cocktailReviewDTO.CocktailId; cocktailReview.UserId = cocktailReviewDTO.UserId; cocktailReview.UserName = cocktailReviewDTO.UserName; cocktailReview.ReviewedOn = cocktailReviewDTO.ReviewedOn; return(cocktailReview); }
public static CocktailReviewViewModel MapToViewModel(this CocktailReviewDTO reviewDTO) { var reviewVm = new CocktailReviewViewModel(); reviewVm.Id = reviewDTO.Id; reviewVm.CocktailID = reviewDTO.CocktailID; reviewVm.Description = reviewDTO.Description; reviewVm.LikeCount = reviewDTO.LikeCount; reviewVm.LikedByUsers = reviewDTO.LikedByUsers; reviewVm.Rating = reviewDTO.Rating; reviewVm.UserName = reviewDTO.UserName; reviewVm.ReviewDate = reviewDTO.ReviewDate; reviewVm.UserId = reviewDTO.UserID; reviewVm.ReviewDate = reviewDTO.ReviewDate; return(reviewVm); }
public static CocktailReviewViewModel MapToCocktailReviewViewModel(this CocktailDto cocktailDto) { var cocktailVm = new CocktailReviewViewModel(); cocktailVm.Id = cocktailDto.Id; cocktailVm.CocktailID = cocktailDto.CocktailId; cocktailVm.Name = cocktailDto.Name; cocktailVm.Rating = (double)Math.Round((decimal)(cocktailDto.Rating ?? 0), 2); cocktailVm.Description = cocktailDto.Description; cocktailVm.Image = cocktailDto.Image; cocktailVm.ReviewDate = cocktailDto.ReviewDate; //cocktailVm.Ingredients = cocktailDto.Ingredients; cocktailVm.LikeCount = cocktailDto.LikeCount; cocktailVm.LikedByUsers = cocktailDto.LikedByUsers; return(cocktailVm); }
public async Task <IActionResult> Edit(int id, CocktailReviewViewModel reviewVM) { var sanitizer = new HtmlSanitizer(); reviewVM.Comment = sanitizer.Sanitize(reviewVM.Comment); if (ModelState.IsValid) { try { var currentUserId = int.Parse(userManager.GetUserId(HttpContext.User)); var reviewDTO = this.cocktailReviewMapper.MapToDTOFromVM(reviewVM); var validationResult = this.cocktailReviewService.ValidateCocktailReview(reviewDTO); if (!validationResult.HasProperInputData) { this.toaster.AddWarningToastMessage(ToastrConsts.NullModel); return(RedirectToAction("Edit", "CocktailReviews", new { id })); } if (!validationResult.HasCorrectRating) { this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectRating); return(RedirectToAction("Edit", "CocktailReviews", new { id })); } if (!validationResult.HasCorrectCommentLength) { this.toaster.AddWarningToastMessage(ToastrConsts.CommentTooLong); return(RedirectToAction("Edit", "CocktailReviews", new { id })); } var result = await this.cocktailReviewService.UpdateCocktailReviewAsync(id, currentUserId, reviewDTO); this.toaster.AddSuccessToastMessage(ToastrConsts.Success); return(RedirectToAction("Details", "Cocktails", new { id })); } catch (Exception) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction("Index", "Cocktails", new { area = "" })); } } this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectInput); return(RedirectToAction("Index", "Cocktails", new { area = "" })); }