public IHttpActionResult RentMovie(RentalRequestContract request)
        {
            if (!RentalService.Instance.AuthenticateUser(request.UserID, ActionContext.Request?.Headers?.Authorization))
            {
                return(Unauthorized());
            }
            //authenicate user
            RentalResponseContract response = RentalService.Instance.HandleRentalRequest(request);

            return(Ok(response));
        }
Beispiel #2
0
 private byte GetRentalDuration(RentalRequestContract request)
 {
     //physical rentals should be a week long, whereas digital is 2 days
     //We can replace this logic with maybe user defined lengths
     //or a database call to read from a type-table and see various lengths based on movie type
     if (request.IsPhysicalRental)
     {
         return(7 * 24);
     }
     else
     {
         return(48);
     }
 }
Beispiel #3
0
        public RentalResponseContract HandleRentalRequest(RentalRequestContract request)
        {
            if (request == null)
            {
                throw new Exception();
            }

            var response = new RentalResponseContract();

            byte rentalDurationHours = GetRentalDuration(request);

            long?rentalID;

            using (var conn = new SqlConnection(ConfigSettings.ConnectionString))
            {
                conn.Open();
                //transaction necessary to make sure multiple don't checkout the same dvd/vhs
                SqlTransaction transaction = conn.BeginTransaction();

                try
                {
                    int?inventoryID = null;
                    if (request.IsPhysicalRental)
                    {
                        inventoryID = RentalRepository.Instance.CheckoutMovie(request.MovieID, conn, transaction);

                        if (inventoryID == null)
                        {
                            throw new Exception("Movie out of stock. Could not fulfill physical rental request");
                        }
                    }

                    var rental = new RentalEntity();
                    rental.MovieID             = request.MovieID;
                    rental.UserID              = request.UserID;
                    rental.InventoryID         = inventoryID;
                    rental.RentalDurationHours = rentalDurationHours;

                    rentalID = RentalRepository.Instance.RentMovie(rental, conn, transaction);

                    if (rentalID == null)
                    {
                        throw new Exception("Something went wrong during movie checkout");
                    }

                    response.IsSuccess      = true;
                    response.ConfirmationID = rentalID;
                    //This is probably where you would fire-off a service bus event
                    //to another service dedicated to mailing off a movie
                    transaction.Commit();
                    conn.Close();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    conn.Close();
                    response.IsSuccess = false;
                    response.Message   = ex.Message;
                }
            }

            return(response);
        }