Example #1
0
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            var customer = _context.Customers.Single(c => c.Id == newRental.CustomerId);
            var room     = _context.Rooms.Single(m => m.Id == newRental.RoomId);


            if (!room.IsOccupied == false)
            {
                return(BadRequest("This room is already occupied at the moment"));
            }
            room.IsOccupied = true;
            var rental = new Rental
            {
                Customer   = customer,
                Room       = room,
                DateRented = DateTime.Now
            };

            _context.Rentals.Add(rental);
            _context.SaveChanges();


            return(Ok());
        }
        public IHttpActionResult createNewRentals(NewRentalDto newRental)
        //public IHttpActionResult createNewRentals(int CustomerId, int[] MovieIds)
        {
            Customer customer = _context.Customers.SingleOrDefault(c => c.Id == newRental.CustomerId);
            var      movies   = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList();

            foreach (var movie in movies)
            {
                if (movie.NumAvailable == 0)
                {
                    return(BadRequest());
                }
                movie.NumAvailable--;
                var rental = new Rental()
                {
                    Customer   = customer,
                    DateRented = DateTime.Now,
                    Movie      = movie
                };
                _context.Rentals.Add(rental);
                _context.SaveChanges();
            }
            return(Ok());
        }
Example #3
0
 public IHttpActionResult Put(NewRentalDto dto)
 {
     throw new NotImplementedException();
 }
Example #4
0
 public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
 {
     throw new NotImplementedException();
 }
 public ActionResult Rent(NewRentalDto newrentaldto)
 {
     return(View());
 }
Example #6
0
 public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
 {
     throw new Exception();
 }
Example #7
0
        public IHttpActionResult CreateNewRentals(NewRentalDto newRental)
        {
            /* Defensive approach used when multiple users access the same API, especially if they are all from different companies
             *
             * if (newRental.MovieIds.Count == 0)
             * {
             *  return BadRequest("No Movie Ids have been given.");
             * }
             *
             * var customer = _context.Customers.SingleOrDefault(c => c.Id == newRental.CustomerId);
             *
             * if(customer == null)
             * {
             *  return BadRequest("CustomerId is not valid.");
             * }
             *
             * var movies = _context.Movies.Where(m => newRental.MovieIds.Contains(m.Id)).ToList();
             *
             * if(movies.Count != newRental.MovieIds.Count)
             * {
             *  return BadRequest("One or more MovieIds 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,
             *      DateRented = DateTime.Now
             *  };
             *
             *  _context.Rentals.Add(rental);
             * }
             * _context.SaveChanges();
             *
             * return Ok();*/

            // Optimistic approach, used when you have an API just to have communication between the backend and frontend of your application

            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());
        }