So now that we have a query that seems to do the job we need to right the database function, that actually
uses that query, but of course, our query, although we're querying the orders table, we're actually
including transaction widget and customer information.
So that means here in models go, we need somewhere to put that information so we can just add members
or fields to the order struct to the type order.
So we'll add one for widget, which I'll call widget.
It's a type of widget.
And in Jason I'll call it widget little lower case and I'll duplicate that twice and I'll add transaction,
which is a type transaction.
And in JSON I'll call the transaction.
And finally, customer of type customer and in JSON we'll call the customer, let's call my office.
And now let's go to the very bottom of this file or anywhere else in this file.
But I'll put mine at the bottom and let's write a function that uses the query we just wrote.
So func and the receiver type DB model and I'll call this get all orders and it takes no parameters
right now and it returns a slice and I'll return a slice of pointers to order and potentially an error.
Now I'll copy my contact stuff from the function right above it.
So I don't miss type it as I always do and I'll put in a place to store our result where I'll call it
orders, which will be a slice of pointer to order.
And then we'll have our query and I'll use the tactics so I can put my query on multiple lines and I'll
go back to my database client where I have that query and copy it
and paste it right in here.
And just format things a little bit better.
All right, so that's my query.
Now let's execute that query.
So we're going to get back rows potentially.
There are assigned the value of IMG, GB, dot query context right there.
We handed the context and we handed our query and we check for an error if error is not equal to nil.
Then we return nil in the air, which is near and of course, we don't want a memory leak, so let's
put in a deferrals close
and we go through the results that we get from the database for Rose.
Next, we'll create a temporary variable Varro of type order to store things in and we'll check for
an error while we call Rose Dot School.
And we're going to scan this into our variable.
Oh, so ampersand.
So first of all, we're getting the ID and then I'll duplicate this a few times just to save some typing.
Next we get the widget ID.
Next, forget the transaction and
then we get the customer customary.
Then we get the status and
then we get the quantity and duplicate this a few more times,
then we get the amount.
Then we get created
and updated up and duplicate, there's a few more times,
then we get in the widget, which is now a member of this type, the ID
and the widget name.
Then the transaction, the I.D. and the transaction amount.
And the currency
and the last four
and the expiring month when expire a year.
And the payment intent.
And the bank return code,
then the customer
I.D. and first name
and last name
and the email.
And if we did it right, that should be everything for our scam.
And we check for an error.
If error is not equal to nil, no return nil.
And the error otherwise we can say orders, which is the variable we created right at the beginning
of this function, will append to that orders.
And since we have a slice of pointers, we have to return ampersand or append ampersand.
And that should be it, except for return orders.
And so that functions now available to us.
So in the next lecture, we'll create a handler that takes advantage of that function and returns.
Jason.