public bool AddRating(User user, SeeReviewModel model) { try { Book _book = _db.Books.Find(model.Book.BookId); Rating rating = Rating.Create(_book, user, model._rating.Review, model._rating.Grade); _book.Ratings.Add(rating); _db.Books.Update(_book); _db.SaveChanges(); int score = 0; var ratings = _db.Ratings.Where(book => book.Book == _book); foreach (Rating rate in ratings) { score += rate.Grade; } _book.Score = score / ratings.Count(); _db.Books.Update(_book); _db.SaveChanges(); return(true); } catch { return(false); } }
public async Task <RatingDto> Handle(CreateRatingCommand request, CancellationToken cancellationToken) { var rating = Rating.Create(request.RatingDate, request.Comment, request.Value, request.PlaceId, request.UserId); await _ratingRepository.CreateAsync(rating); return(new RatingDto(rating.RatingId)); }
public async Task <Rating> CreateAsync(RatingStatus ratingStatus, ActivityPlan activityPlan, User owner) { var rating = activityPlan.Ratings.FirstOrDefault(r => r.OwnerId == owner.Id && r.ActivityPlanId == activityPlan.Id); if (rating != null) { rating.EditRating(ratingStatus, owner.Id); } else { rating = await CreateAsync(Rating.Create(ratingStatus, activityPlan, owner)); } return(rating); }
public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (ModelState.IsValid) { var user = new User { UserName = model.FirstName + model.LastName, Email = model.Email, PhoneNumber = model.PhoneNumber, FirstName = model.FirstName, LastName = model.LastName }; var result = await _userManager.CreateAsync(user, model.Password); if (result.Succeeded) { _logger.LogInformation("User created a new account with password."); var userResult = await _unitOfWork.Users.GetUserByUsernameAsync(model.FirstName + model.LastName); _userContext.Id = userResult.Id; await _unitOfWork.Ratings.InsertAsync(Rating.Create(userResult.Id)); await _unitOfWork.CompleteAsync(); var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme); await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl); await _signInManager.SignInAsync(user, isPersistent : false); _logger.LogInformation("User created a new account with password."); return(RedirectToAction("Activities", "Home")); } AddErrors(result); } // If we got this far, something failed, redisplay form return(View(model)); }