internal long?RentMovie(RentalEntity rental, SqlConnection conn, SqlTransaction transaction)
        {
            long?rentalID;

            using (var command = new SqlCommand("dbo.RentMovie", conn, transaction))
            {
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.AddWithValue("@UserID", rental.UserID);
                command.Parameters.AddWithValue("@MovieID", rental.MovieID);
                command.Parameters.AddWithValue("@InventoryID", rental.InventoryID);
                command.Parameters.AddWithValue("@RentalDurationHours", rental.RentalDurationHours);

                var outputParameter = new SqlParameter();
                outputParameter.DbType        = DbType.Int64;
                outputParameter.ParameterName = "@RentalID";
                outputParameter.Direction     = ParameterDirection.Output;

                command.Parameters.Add(outputParameter);

                command.ExecuteNonQuery();

                rentalID = outputParameter.Value as Int64?;
            }

            return(rentalID);
        }
示例#2
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);
        }