Ejemplo n.º 1
0
        public ActionResult Register(Customer cust)
        {
            db.Customers.Add(cust);

            db.SaveChanges();
            Session["CurrentCustomerName"] = cust.Name;
            Session["CurrentCustomerId"]   = Convert.ToInt32(cust.CustomerId);
            return(RedirectToAction("Index", "Customers"));
        }
Ejemplo n.º 2
0
        public IHttpActionResult CreateCustomer(CustomerDto cust)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            var customer = Mapper.Map <CustomerDto, Customer>(cust);

            db.Customers.Add(customer);
            db.SaveChanges();

            cust.CustomerId = customer.CustomerId;
            return(Created(new Uri(Request.RequestUri + "/" + customer.CustomerId), cust));
        }
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            if (newRental.MovieIds.Count == 0)
            {
                return(BadRequest("Please select MovieIds"));
            }

            var customer = db.Customers.SingleOrDefault(s => s.CustomerId == newRental.CustomerId);

            if (customer == null)
            {
                return(BadRequest("Cusomer is not valid"));
            }


            var movies = db.Movies.Where(s => newRental.MovieIds.Contains(s.Id)).ToList();

            if (movies.Count != newRental.MovieIds.Count)
            {
                return(BadRequest("One or more movie are invalid."));
            }


            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,
                    DateRanted = DateTime.Now
                };

                db.Rentals.Add(rental);
            }
            db.SaveChanges();
            return(Ok());
        }
 public ActionResult AddMovie(Movie movie)
 {
     db.Movies.Add(movie);
     db.SaveChanges();
     return(RedirectToAction("Index", "Movies"));
 }