So now we want to work on our transactions so that we can pull a widget from the database and we can
store transaction information in the database because at some point we might have to refund that transaction.
And it is generally good practice to keep track of things like credit card transactions.
So I'm looking at Main Dutko, and one of the first things we're going to have to do is to populate
our application type with the database information.
Now, obviously, we have to make a change to our application struct.
So what we'll put in there is we'll call it.
I'm going to use all caps just because I like it for database and it's going to be a type models DB
model.
So let me format that.
If we look at that DB model type, as you can see, all that is, is a wrapper for our connection.
Cool.
So let's go back to Mango and close model Stocco and all we have to do down here is after we get our
database connection pool and the Deferred Khong close, we just add that right here.
So we'll put in DB and set the two models, DV model and DB and it's that's a that's the variable name
we used right up there, online line seventy one and put a comma there.
OK, so that will actually give us access to the DB type which is of type demo models, DB model.
Now let's go over to our models Doug file and all we do now is create some functions with the appropriate
receiver.
So we'll create one that will query a table that doesn't exist yet, but don't worry about that.
So we'll create a function and we'll give it the receiver and I'll call my receiver M for model and
it's a type A pointer to the model and I'll call it get widget.
We'll create a function to get a single widget just as a test case, and it will take one parameter
ID, which is the type end and it will return a widget and potentially an error.
And as is good practice with Web applications, I'm going to use the context every time I connected
the database and set a reasonable time.
And so I created variables X and cancel and those will be assigned the value of from the context package
right there.
I'm going to set it with time and I'll get the context from the background where it's always available.
Context, dot background.
And I'll set a time out of three times timed second three seconds is enough.
If my crews are taking more than three seconds, something is not right and we defer cancel.
Now, I'm going to attempt to get a single widget, so I'll create a variable to store that information
and our widget and its a type widget.
And now I will use the query row context from our DB to get that information.
I don't do this all on one line and we're going to have to fix this later because right now I'm just
doing a simple test to make sure that everything works.
So accretive variable roll.
I'll call from my receiver, the DB model, and I'm going to call query row context.
And that requires the context which we just created back there on line 30.
And then a query, I'll say select ID and name from widgets where ID equals and the placeholder question
mark that both Maisky Will and Maria DB require an ID is the substitution for that question mark and
I'll just give myself some more room here.
Error we'll check for an error is assigned the value of rodent scan.
So we're scanning the variable into our widget.
So we'll scan to into a reference to widget ID and a reference to widget dot name check for an error.
If error is not equal to nil, then I'll return the widget even though I will ignore it and the error.
Otherwise everything worked.
Return widget and no, no, this database table widgets doesn't exist.
So I'm going to switch to my minuscule client and in my case it's SQL Ace and I'll connect to my database
that we set up a few lectures ago and I'll create a new table.
And I'll call this table, which is.
And the structure, I want an ID and I'm just going to add one more field right now because we'll do
this in a more intelligent way shortly.
Name will be a marker of two fifty five and then I'll populated with one row.
So one row, I'll let the ID be set by itself and I'll call the name widget with a capital W there.
So now we have a means of testing this to make sure it all works, but we need to set up a handler to
do that.
Now I'm going to set the handler up in the back end just because I can.
So let's go back to API and we'll have to make the same changes in API Dargo that we did in our main
Dongo, which means my application has to have a DB of type models, DB model format and then down here
a simply populate the DB will be of type models that DB model and DB set to.
OK, so that's now available there.
Now let's set up a simple handler in the API to actually connect to that function we just created.
So over here in Handler Dash API, Dutko will create a new function.
And what's the receiver called?
Application or created func with a receiver of app pointer to application.
And I'll call this get widget ID and it's a handler, so it'll take a response writer for Retter Response
Writer and a pointer to a request HDB request.
And I'm going to get the ID from the URL and since we're using the Cerreta, that's pretty simple as
a sign the value of the euro param.
And I'm looking from the request.
I'm looking for Heidi.
That's what I'll have to call it when I set up to read to this and widget ID because I want this to
be an ID and potentially an error which I'll ignore for now is a sign the value of Sterkel A2.
I will convert our string to an ID and now I'll try calling the database and because my back and my
front end share code, this is pretty simple.
I'll create a variable widget, I'll check for an error.
That's a sign the value of from App Divi and we won't get widget and we'll hand it widget and we'll
check for an error if error is not equal to nil.
For now, I'll just say after error, log the print line error and I'll return, which is not very nice,
but we'll fix that later on and we'll write this out as Jason.
And this is an easy way to make sure it all works out an error or assign the value of Jason Marshall
and Marshall in debt to make it pretty, which you shouldn't do in production.
But for development, it's fine.
We're going to Marshall Widget with no prefix and a few spaces to format everything nicely.
We'll check for an error.
I'll just copy this error checking code and pasted here.
And finally we'll write this that header set content type applications.
Jason and we wrote it.
I write it.
And I'm missing parentheses after her there.
OK, so we have this get widget by ID, but we need to set up a route to it.
So let's go to Route's API and we'll just create a room right here.
Yep.
And we're going to go to slash API slash widget slash and then some ID and I have to use the same value
here ID as I did in my handler here where I'm looking for ID online seventy-nine.
So that's our route, our URL and we want to go to app to get Widget by ID.
OK, so let me open my terminal, clear the screen, stop everything good.
And I'll just say make start and I will compile both the front and back end and now I should be able
to go to this route on localhost four thousand one.
So let's try it.
I'll open a new tab and go to localhost four thousand and one API slash, get or slash widget slash
one and there's one thing in my test table and there it is.
And if you look at the raw data, that should all be nicely formatted, Jason.
And it is so now we have a means of using our database and writing our queries, and that's pretty straightforward.
So if we think about it, we can go back to our code and I'll stop everything.
Stop.
We'll simply go to our models, don't go file and write one routine and export it by giving it function,
name a capital letter for all of the things we need to do.
And that would be, number one, get a widget.
And we have this right here.
We'll have to fix up the table and make the query correct.
But we'll do that shortly.
And we'll also want a means of storing transactions, retrieving transactions, all those things.
And the fact that I have these database functions in the shared code base between my Fronton and my
back end means I can actually get the content from the database and serve it up as HTML or get the content
from the database and served up as JSON or whatever format I need to use.
OK, so in the next lecture will install a tool to run database migration's and we'll set up a proper
widgets table and a proper transaction table and we'll get started on that in the next lecture.