public IActionResult Create(CreateCustomerViewModel model)
 {
     if (ModelState.IsValid)
     {
         _context.Customers.Add(model.Customer);
         _context.SaveChanges();
         return(RedirectToAction(nameof(Index)));
     }
     model.MembershipTypes = _context.MembershipTypes.ToList();
     return(View(model));
 }
Esempio n. 2
0
        public ActionResult <CustomerDto> CreateCustomer(CustomerDto customerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var customer = Mapper.Map <CustomerDto, Customer>(customerDto);

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

            customerDto.Id = customer.Id;

            return(Created("", customerDto));
        }
        public IActionResult Save(Movie movie)
        {
            if (ModelState.IsValid)
            {
                if (movie.Id == 0)
                {
                    movie.DateAdded = DateTime.Now;
                    _context.Movies.Add(movie);
                }
                else
                {
                    var existingMovie = _context.Movies.FirstOrDefault(m => m.Id == movie.Id);
                    existingMovie.Name        = movie.Name;
                    existingMovie.GenreId     = movie.GenreId;
                    existingMovie.ReleaseDate = movie.ReleaseDate;
                    existingMovie.Stock       = movie.Stock;
                }

                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }
            var viewModel = new SaveMovieViewModel(movie)
            {
                Genres = _context.Genres.ToList()
            };

            return(View(viewModel));
        }
Esempio n. 4
0
        public ActionResult CreateMovie(MovieDto movieDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var movie = Mapper.Map <MovieDto, Movie>(movieDto);

            movie.DateAdded = DateTime.Now;

            _context.Movies.Add(movie);
            _context.SaveChanges();

            movieDto.Id = movie.Id;

            return(Created("", movieDto));
        }
        public IActionResult CreateNewRentals(NewRentalDto newRentalDto)
        {
            if (newRentalDto.MovieIds.Count == 0)
            {
                return(BadRequest("No Movie Ids have been given."));
            }

            var customer = _context.Customers.SingleOrDefault(c => c.Id == newRentalDto.CustomerId);

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


            var movies = _context.Movies.Where(m => newRentalDto.MovieIds.Contains(m.Id)).ToList();

            if (movies.Count != newRentalDto.MovieIds.Count)
            {
                return(BadRequest("One or move MovieIds are invalid."));
            }

            foreach (var movie in movies)
            {
                if (movie.Available == 0)
                {
                    return(BadRequest("Not available this movie"));
                }

                movie.Available--;

                var rental = new Rental
                {
                    Customer   = customer,
                    Movie      = movie,
                    DateRented = DateTime.Now
                };

                _context.Rentals.Add(rental);
            }

            _context.SaveChanges();

            return(Ok());
        }