public async Task <IActionResult> DeleteConfirmed(int id) { UserServicesRatings userServicesRatings = await _colibriDbContext.UserServicesRatings.FindAsync(id); if (userServicesRatings == null) { return(NotFound()); } else { // remove the Entry from the DB _colibriDbContext.UserServicesRatings.Remove(userServicesRatings); // save the Changes asynchronously await _colibriDbContext.SaveChangesAsync(); // avoid Refreshing the POST Operation -> Redirect return(RedirectToAction(nameof(Index))); } }
//[ValidateAntiForgeryToken] public async Task <IActionResult> RateUserServicePost(int id, string command) { // Check the State Model Binding if (ModelState.IsValid) { // Security Claims System.Security.Claims.ClaimsPrincipal currentUser = this.User; // Claims Identity var claimsIdentity = (ClaimsIdentity)this.User.Identity; var claim = claimsIdentity.FindFirst(ClaimTypes.NameIdentifier); // to overwrite a Rating, first get the old One // get the Product from the DB var userServiceFromDb = await _colibriDbContext.UserServices .Where(m => m.Id == id) .FirstOrDefaultAsync(); // another Table ProductRatings for the Description var userServiceRatingFromDb = await _colibriDbContext.UserServicesRatings .Where(p => p.UserServiceId == id) .FirstOrDefaultAsync(); // current User var currentUserId = claim.Value; //bool userAlreadyRated = false; if (userServiceRatingFromDb != null) { // check if already rated if (userServiceRatingFromDb.ApplicationUserId == currentUserId) { // already rated! //userAlreadyRated = true; TempData["msg"] = "<script>alert('Already rated!');</script>"; TempData["returnButton"] = "<div><p><b>Already rated!</b></p></div>"; TempData["returnBackButton"] = "return"; TempData["showUserServiceRating"] = "showUserServiceRating"; TempData["userServiceId"] = userServiceRatingFromDb.Id; ViewData["BackToList"] = _localizer["BackToListText"]; ViewData["ShowAllRatings"] = _localizer["ShowAllRatingsText"]; ViewData["ShowRating"] = _localizer["ShowRatingText"]; return(View()); } //else if (userAlreadyRated) else { int tempUserServiceRating = 0; if (command.Equals("1")) { tempUserServiceRating = 1; } else if (command.Equals("2")) { tempUserServiceRating = 2; } else if (command.Equals("3")) { tempUserServiceRating = 3; } else if (command.Equals("4")) { tempUserServiceRating = 4; } else if (command.Equals("5")) { tempUserServiceRating = 5; } // go to the User Service Table // calculate the new ProductRating if (userServiceFromDb.NumberOfServiceRates == 0) { userServiceFromDb.ServiceRating = tempUserServiceRating; } else { userServiceFromDb.ServiceRating = Math.Round((userServiceFromDb.ServiceRating * userServiceFromDb.NumberOfServiceRates + tempUserServiceRating) / (userServiceFromDb.NumberOfServiceRates + 1), 2); } // Rating Create UserServicesRatings userServicesRatings = new UserServicesRatings() { UserServiceId = userServiceFromDb.Id, UserServiceName = userServiceFromDb.Name, // add the current User as the Creator of the Rating ApplicationUserId = claim.Value, ApplicationUserName = claim.Subject.Name, UserServiceRating = tempUserServiceRating, CreatedOn = System.DateTime.Now }; // update the UserServicesRating Entity _colibriDbContext.UserServicesRatings.Add(userServicesRatings); // increment the Number of User Service Rates of the User Service userServiceFromDb.NumberOfServiceRates += 1; // save the Changes in DB await _colibriDbContext.SaveChangesAsync(); } return(View(UserServicesAddToEntityViewModel)); } //else if (userServiceFromDb == null && !userAlreadyRated) else { int tempUserServiceRating = 0; if (command.Equals("1")) { tempUserServiceRating = 1; } else if (command.Equals("2")) { tempUserServiceRating = 2; } else if (command.Equals("3")) { tempUserServiceRating = 3; } else if (command.Equals("4")) { tempUserServiceRating = 4; } else if (command.Equals("5")) { tempUserServiceRating = 5; } // go to the User Service Table // calculate the new ProductRating if (userServiceFromDb.NumberOfServiceRates == 0) { userServiceFromDb.ServiceRating = tempUserServiceRating; } else { userServiceFromDb.ServiceRating = Math.Round((userServiceFromDb.ServiceRating * userServiceFromDb.NumberOfServiceRates + tempUserServiceRating) / (userServiceFromDb.NumberOfServiceRates + 1), 2); } // Rating Create UserServicesRatings userServicesRatings = new UserServicesRatings() { UserServiceId = userServiceFromDb.Id, UserServiceName = userServiceFromDb.Name, // add the current User as the Creator of the Rating ApplicationUserId = claim.Value, ApplicationUserName = claim.Subject.Name, UserServiceRating = tempUserServiceRating, CreatedOn = System.DateTime.Now }; // update the UserServicesRating Entity _colibriDbContext.UserServicesRatings.Add(userServicesRatings); // increment the Number of User Service Rates of the User Service userServiceFromDb.NumberOfServiceRates += 1; // save the Changes in DB await _colibriDbContext.SaveChangesAsync(); } return(RedirectToAction(nameof(Details))); } else { // one can simply return to the Form View again for Correction return(View(UserServicesAddToEntityViewModel)); } }