/// <summary> /// Add a review and associate it with a restaurant. /// </summary> /// <param name="review">The review</param> /// <param name="restaurant">The restaurant</param> public void AddReview(Domain.Model.Review review, Domain.Model.Restaurant restaurant = null) { if (restaurant.Id != 0) { _logger.LogWarning("Review to be added has an ID ({reviewId}) already: ignoring.", review.Id); } _logger.LogInformation("Adding review to restaurant with ID {restaurantId}", restaurant.Id); if (restaurant != null) { // get the db's version of that restaurant // (can't use Find with Include) Restaurant restaurantEntity = _dbContext.Restaurant .Include(r => r.Review) .First(r => r.Id == restaurant.Id); Review newEntity = Mapper.Map(review); restaurantEntity.Review.Add(newEntity); // also, modify the parameters restaurant.Reviews.Add(review); } else { Review newEntity = Mapper.Map(review); _dbContext.Add(newEntity); } }
// GET: Items/Edit/5 public IActionResult Edit(int id) { // we pass the current values into the Edit view // so that the input fields can be pre-populated instead of blank // (important for good UX) Domain.Model.Review review = RepoRev.GetReviewById(id); var viewModel = new ReviewViewModel { ReviewId = review.Id, UserName = RepoPers.GetPersonById(review.PersonId).Username, Password = RepoPers.GetPersonById(review.PersonId).Password, SellerName = RepoSell.GetSellerById(review.SellerId).Name, Score = review.Score, Comment = review.Comment }; List <string> mySellers = new List <string> (); foreach (var val in RepoSell.GetSellersByName().ToList()) { mySellers.Add(val.Name); } ViewData["SellerName"] = new SelectList(mySellers); List <string> myPeople = new List <string> (); foreach (var val in RepoPers.GetPeopleByName().ToList()) { myPeople.Add(val.Username); } ViewData["UserName"] = new SelectList(myPeople); return(View(viewModel)); }
public void UpdateReview(Domain.Model.Review inputReview) { _logger.LogInformation($"Updating review with ID {inputReview.Id}"); Context.Review currentEntity = _dbContext.Reviews.Find(inputReview.Id); Context.Review newEntity = Mapper.UnMapReview(inputReview); _dbContext.Entry(currentEntity).CurrentValues.SetValues(newEntity); }
/// <summary> /// Update a review. /// </summary> /// <param name="review">The review with changed values</param> public void UpdateReview(Domain.Model.Review review) { _logger.LogInformation("Updating review with ID {reviewId}", review.Id); Review currentEntity = _dbContext.Review.Find(review.Id); Review newEntity = Mapper.Map(review); _dbContext.Entry(currentEntity).CurrentValues.SetValues(newEntity); }
/// <summary> /// Maps a review business model to a DAO for Entity Framework, /// not including the restaurant. /// </summary> /// <param name="review">The review business model.</param> /// <returns>The review DAO.</returns> public static Model.Review Map(Domain.Model.Review review) { return(new Model.Review { Id = review.Id, ReviewerName = review.ReviewerName, Score = review.Score ?? throw new ArgumentException("review score cannot be null.", nameof(review)), Text = review.Text });
public static Context.Review UnMapReview(Domain.Model.Review review) { return(new Context.Review { ReviewId = review.Id, PersonId = review.PersonId, SellerId = review.SellerId, Score = review.Score, Comment = review.Comment, }); }
public void AddReview(Domain.Model.Review inputReview) { if (inputReview.Id != 0) { _logger.LogWarning($"Review to be added has an ID ({inputReview.Id}) already: ignoring."); } _logger.LogInformation("Adding review"); Context.Review entity = Mapper.UnMapReview(inputReview); entity.ReviewId = 0; _dbContext.Add(entity); }
// GET: Items/Delete/5 public IActionResult Delete(int id) { Domain.Model.Review review = RepoRev.GetReviewById(id); var viewModel = new ReviewViewModel { ReviewId = review.Id, UserName = RepoPers.GetPersonById(review.PersonId).Username, Password = RepoPers.GetPersonById(review.PersonId).Password, SellerName = RepoSell.GetSellerById(review.SellerId).Name, Score = review.Score, Comment = review.Comment }; return(View(viewModel)); }
public IActionResult Create([Bind("UserName,Password,SellerName,Score,Comment")] ReviewViewModel viewModel) { try { if (ModelState.IsValid) { if (viewModel.Password != RepoPers.GetPeopleByName(viewModel.UserName).First(p => p.Username.ToLower() == viewModel.UserName.ToLower()).Password) { return(View(viewModel)); } var review = new Domain.Model.Review { Id = viewModel.ReviewId, Comment = viewModel.Comment, Score = viewModel.Score, PersonId = RepoPers.GetPeopleByName(viewModel.UserName).First(p => p.Username.ToLower() == viewModel.UserName.ToLower()).Id, SellerId = RepoSell.GetSellersByName(viewModel.SellerName).First(p => p.Name.ToLower() == viewModel.SellerName.ToLower()).Id }; List <string> mySellers = new List <string> (); foreach (var val in RepoSell.GetSellersByName().ToList()) { mySellers.Add(val.Name); } ViewData["SellerName"] = new SelectList(mySellers); List <string> myPeople = new List <string> (); foreach (var val in RepoPers.GetPeopleByName().ToList()) { myPeople.Add(val.Username); } ViewData["UserName"] = new SelectList(myPeople); RepoRev.AddReview(review); RepoRev.Save(); return(RedirectToAction(nameof(Index))); } return(View(viewModel)); } catch { return(View(viewModel)); } }
public IActionResult Edit([FromRoute] int id, [Bind("UserName,SellerName,Score,Comment,Password")] ReviewViewModel viewModel) { try { if (ModelState.IsValid) { Domain.Model.Review review = RepoRev.GetReviewById(id); review.Comment = viewModel.Comment; review.Score = viewModel.Score; review.PersonId = RepoPers.GetPeopleByName(viewModel.UserName).First(p => p.Username.ToLower() == viewModel.UserName.ToLower()).Id; review.SellerId = RepoSell.GetSellersByName(viewModel.SellerName).First(p => p.Name.ToLower() == viewModel.SellerName.ToLower()).Id; RepoRev.UpdateReview(review); RepoRev.Save(); return(RedirectToAction(nameof(Index))); } return(View(viewModel)); } catch (Exception) { return(View(viewModel)); } }