public IActionResult CreateNewRentals([FromForm] NewRental newRental) { var customer = _context.Customers.Single( c => c.Id == newRental.customerId); var movies = _context.Movies.Where( m => newRental.movieIds.Contains(m.Id)).ToList(); foreach (var movie in movies) { if (movie.NumberAvailable == 0) { return(BadRequest("Movie is not available.")); } movie.NumberAvailable--; var rental = new Rental { Customer = customer, Movie = movie, DateRented = DateTime.Now }; _context.Rentals.Add(rental); } _context.SaveChanges(); return(Ok()); }
public IActionResult Save(Customer customer) { if (!ModelState.IsValid) { var viewModel = new CustomerFormViewModel { Customer = customer, MembershipTypes = _context.MembershipTypes.ToList() }; return(View("CustomerForm", viewModel)); } if (customer.Id == 0) { _context.Customers.Add(customer); } else { var customerInDb = _context.Customers.Single(c => c.Id == customer.Id); customerInDb.Name = customer.Name; customerInDb.Birthdate = customer.Birthdate; customerInDb.MembershipTypeId = customer.MembershipTypeId; customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter; } _context.SaveChanges(); return(RedirectToAction("Index", "Customers")); }
public ActionResult Save(Movie movie) { if (!ModelState.IsValid) { var viewModel = new MovieFormViewModel(movie) { Genres = _context.Genres.ToList() }; return(View("MovieForm", viewModel)); } if (movie.Id == 0) { movie.DateAdded = DateTime.Now; _context.Movies.Add(movie); } else { var movieInDb = _context.Movies.Single(m => m.Id == movie.Id); movieInDb.Name = movie.Name; movieInDb.GenreId = movie.GenreId; movieInDb.NumberInStock = movie.NumberInStock; movieInDb.ReleaseDate = movie.ReleaseDate; } _context.SaveChanges(); return(RedirectToAction("Index", "Movies")); }
public IActionResult Snimi(int studentid, string ime, string prezime, int opstinaId) { Student s; if (studentid != 0) { s = _db._studenti.Find(studentid); } else { s = new Student(); _db._studenti.Add(s); } s.Ime = ime; s.OpstinaId = opstinaId; s.Prezime = prezime; _db.SaveChanges(); return(RedirectToAction("Index")); }