public ActionResult Review(ReviewProfileViewModel viewModel) { User user = db.Users.FirstOrDefault(u => u.Username.Equals(User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)); Customer customer = db.Customers.FirstOrDefault(u => u.UserId.Equals(user.Id)); Trip trip = db.Trips.Find(viewModel.TripId); //get trip customer tries to write review for Review review = new Review() //create the review object from retrived model { Customer = customer, Trip = trip, Description = viewModel.Description, Rating = viewModel.Rating, }; OrderItem orderItem = db.OrderItems.Find(viewModel.OrderItemId); orderItem.ReviewId = review.Id; try //add review to database and pass success message { db.Reviews.Add(review); db.OrderItems.AddOrUpdate(orderItem); db.SaveChanges(); TempData["MessageClass"] = "alert-success"; TempData["Message"] = "Successfully added the review."; } catch (Exception ex) //In case of errors pass unsuccessfull message { TempData["MessageClass"] = "alert-danger"; TempData["Message"] = "Unsuccessful - please try again later."; } return(RedirectToAction("Orders")); //Redirect back to orders page }
public ActionResult New(NewEnquiryViewModel viewModel) { if (ModelState.IsValid) { User user = db.Users.FirstOrDefault( u => u.Username.Equals(User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)); var Customer = db.Customers.FirstOrDefault(u => u.UserId.Equals(user.Id)); string title = "General"; if (viewModel.SelectedTitleOption != "0") { var tripId = int.Parse(viewModel.SelectedTitleOption); title = db.Trips.FirstOrDefault(u => u.Id.Equals(tripId)).Title; } Enquiry enquiry = new Enquiry() { Title = title, Question = viewModel.Question, Customer = Customer }; try { db.Enquiries.Add(enquiry); db.SaveChanges(); TempData["MessageClass"] = "alert-success"; TempData["Message"] = "Successfully sent the enquiry."; } catch (Exception ex) { TempData["MessageClass"] = "alert-warning"; TempData["Message"] = "Something went wrong. Try again later."; } System.Diagnostics.Debug.WriteLine(viewModel.TripId); return(RedirectToAction("Details", "Home", new { id = viewModel.TripId })); } viewModel.TitleOptions = new List <SelectListItem>() { new SelectListItem() { Value = "0", Text = "General" }, new SelectListItem() { Value = viewModel.TripId.ToString(), Text = db.Trips.Find(viewModel.TripId).Title } }; return(View(viewModel)); }
public ActionResult Checkout(PreferencesShoppingCartViewModel viewModel) { User user = db.Users.FirstOrDefault(u => u.Username.Equals(User.Identity.Name, StringComparison.CurrentCultureIgnoreCase)); ShoppingCart shoppingCart = db.Customers.FirstOrDefault(u => u.UserId.Equals(user.Id)).ShoppingCart; foreach (var preference in viewModel.CartItemPreferences) { CartItem cI = shoppingCart.CartItems.Find(x => x.Id.Equals(preference.CartItemId)); cI.SeatPreference = int.Parse(preference.Selected); db.CartItems.AddOrUpdate(cI); db.SaveChanges(); } Order order = shoppingCart.CreateOrder(); if (order != null) { TempData["Message"] = "Successfully ordered."; } else { TempData["Message"] = "Unsuccessful order."; return(RedirectToAction("Index")); } return(View(order)); }
public ActionResult Create(RegisterAuthenticationViewModel viewModel) { if (ModelState.IsValid) { if (!db.Users.Any(u => u.Username.Equals(viewModel.Username))) { Customer customer = new Customer() { User = new User() { FirstName = viewModel.FirstName, Surname = viewModel.Surname, Username = viewModel.Username, Password = viewModel.Password, UserRole = db.UserRoles.FirstOrDefault(u => u.Role.Equals("Customer")) }, CreditCard = new CreditCard() { NameOnCard = viewModel.NameOnCard, CardNumber = viewModel.CardNumber, CCV = viewModel.CCV, ExpiryDate = viewModel.ExpiryDate }, Email = viewModel.Email, ShoppingCart = new ShoppingCart() }; db.Customers.Add(customer); db.SaveChanges(); TempData["MessageClass"] = "alert-success"; TempData["Message"] = "Successful registration."; return(RedirectToAction("Index")); } ModelState.AddModelError("", viewModel.Username + " already exists in the system."); } return(RedirectToAction("Index")); }
public ActionResult Create(CreateTripViewModel viewModel) { if (ModelState.IsValid) { Trip trip = new Trip(); trip.Title = viewModel.Title; trip.Description = viewModel.Description; trip.From = viewModel.From; trip.To = viewModel.To; trip.DepartureDate = viewModel.DepartureDate; trip.ReturnDate = viewModel.DepartureDate; trip.Price = viewModel.Price; trip.Coach = new Coach(); trip.Coach.Capacity = viewModel.Capacity; db.Trips.Add(trip); db.SaveChanges(); return(RedirectToAction("Index")); } return(RedirectToAction("Index")); }