public async Task <IActionResult> Create(int id) { try { var cocktailDTO = await this.cocktailService.GetCocktailAsync(id); //var cocktailVM = cocktailMapper.MapToVMFromDTO(cocktailDTO); var currentUserId = int.Parse(userManager.GetUserId(HttpContext.User)); var reviewVM = new CreateCocktailReviewViewModel { CocktailId = cocktailDTO.Id, AuthorId = currentUserId }; //ViewData["BarId"] = new SelectList(_context.Bars, "Id", "Address"); //ViewData["UserId"] = new SelectList(_context.Users, "Id", "Id"); return(View(reviewVM)); } catch (Exception) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction("Index", "Cocktails", new { id })); } }
public CocktailReviewDTO MapToDTOFromVM(CreateCocktailReviewViewModel createCocktailReviewVM) { var reviewDTO = new CocktailReviewDTO { Comment = createCocktailReviewVM.Comment, CocktailId = createCocktailReviewVM.CocktailId, AuthorId = createCocktailReviewVM.AuthorId, Rating = createCocktailReviewVM.Rating }; return(reviewDTO); }
public async Task <IActionResult> Create(int id, CreateCocktailReviewViewModel reviewVM) { var sanitizer = new HtmlSanitizer(); reviewVM.Comment = sanitizer.Sanitize(reviewVM.Comment); if (ModelState.IsValid) { try { if (ModelState.IsValid) { var currentUserId = int.Parse(userManager.GetUserId(HttpContext.User)); reviewVM.AuthorId = currentUserId; //var userDTO = await this.userService.GetUserAsync(currentUserId); //var barDTO = await this.barService.GetBarAsync(reviewVM.BarId); var reviewDTO = this.cocktailReviewMapper.MapToDTOFromVM(reviewVM); reviewDTO.AuthorId = currentUserId; reviewDTO.CocktailId = id; bool isUnique = this.cocktailReviewService.CocktailReviewIsUnique(reviewDTO); if (!isUnique) { this.toaster.AddWarningToastMessage(ToastrConsts.ReviewExists); return(RedirectToAction("Index", "Cocktails", new { id = reviewDTO.CocktailId })); } var validationResult = this.cocktailReviewService.ValidateCocktailReview(reviewDTO); if (!validationResult.HasProperInputData) { this.toaster.AddWarningToastMessage(ToastrConsts.NullModel); return(RedirectToAction(nameof(Create), new { id })); } if (!validationResult.HasCorrectRating) { this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectRating); return(RedirectToAction(nameof(Create), new { id })); } if (!validationResult.HasCorrectCommentLength) { this.toaster.AddWarningToastMessage(ToastrConsts.CommentTooLong); return(RedirectToAction(nameof(Create), new { id })); } var result = await this.cocktailReviewService.CreateCocktailReviewAsync(reviewDTO); this.toaster.AddSuccessToastMessage(ToastrConsts.Success); return(RedirectToAction("Details", "Cocktails", new { id = result.CocktailId })); } } catch (Exception) { this.toaster.AddWarningToastMessage(ToastrConsts.GenericError); return(RedirectToAction("Index", "Cocktails", new { area = "" })); } } this.toaster.AddWarningToastMessage(ToastrConsts.IncorrectInput); return(RedirectToAction("Index", "Cocktails", new { area = "" })); }