WEBVTT 00:02.310 --> 00:07.440 So now we want to work on our transactions so that we can pull a widget from the database and we can 00:07.440 --> 00:13.020 store transaction information in the database because at some point we might have to refund that transaction. 00:13.260 --> 00:17.350 And it is generally good practice to keep track of things like credit card transactions. 00:17.790 --> 00:22.590 So I'm looking at Main Dutko, and one of the first things we're going to have to do is to populate 00:22.860 --> 00:26.520 our application type with the database information. 00:26.640 --> 00:29.780 Now, obviously, we have to make a change to our application struct. 00:29.820 --> 00:32.840 So what we'll put in there is we'll call it. 00:33.330 --> 00:38.940 I'm going to use all caps just because I like it for database and it's going to be a type models DB 00:39.540 --> 00:39.990 model. 00:41.280 --> 00:42.980 So let me format that. 00:43.290 --> 00:48.920 If we look at that DB model type, as you can see, all that is, is a wrapper for our connection. 00:48.980 --> 00:49.220 Cool. 00:49.260 --> 00:56.460 So let's go back to Mango and close model Stocco and all we have to do down here is after we get our 00:56.520 --> 01:02.060 database connection pool and the Deferred Khong close, we just add that right here. 01:02.220 --> 01:13.020 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 01:13.020 --> 01:17.300 we used right up there, online line seventy one and put a comma there. 01:18.450 --> 01:26.220 OK, so that will actually give us access to the DB type which is of type demo models, DB model. 01:26.850 --> 01:34.470 Now let's go over to our models Doug file and all we do now is create some functions with the appropriate 01:34.470 --> 01:34.920 receiver. 01:35.520 --> 01:39.750 So we'll create one that will query a table that doesn't exist yet, but don't worry about that. 01:40.170 --> 01:44.760 So we'll create a function and we'll give it the receiver and I'll call my receiver M for model and 01:44.760 --> 01:49.080 it's a type A pointer to the model and I'll call it get widget. 01:49.350 --> 01:54.240 We'll create a function to get a single widget just as a test case, and it will take one parameter 01:54.330 --> 01:59.640 ID, which is the type end and it will return a widget and potentially an error. 02:01.560 --> 02:06.510 And as is good practice with Web applications, I'm going to use the context every time I connected 02:06.510 --> 02:08.280 the database and set a reasonable time. 02:08.280 --> 02:16.620 And so I created variables X and cancel and those will be assigned the value of from the context package 02:17.730 --> 02:18.510 right there. 02:19.830 --> 02:27.540 I'm going to set it with time and I'll get the context from the background where it's always available. 02:27.540 --> 02:28.920 Context, dot background. 02:31.440 --> 02:36.420 And I'll set a time out of three times timed second three seconds is enough. 02:36.420 --> 02:42.500 If my crews are taking more than three seconds, something is not right and we defer cancel. 02:44.670 --> 02:49.920 Now, I'm going to attempt to get a single widget, so I'll create a variable to store that information 02:49.950 --> 02:53.130 and our widget and its a type widget. 02:55.650 --> 03:01.250 And now I will use the query row context from our DB to get that information. 03:01.260 --> 03:04.440 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 03:04.440 --> 03:06.900 doing a simple test to make sure that everything works. 03:07.320 --> 03:08.520 So accretive variable roll. 03:08.670 --> 03:17.130 I'll call from my receiver, the DB model, and I'm going to call query row context. 03:18.180 --> 03:21.660 And that requires the context which we just created back there on line 30. 03:22.140 --> 03:32.550 And then a query, I'll say select ID and name from widgets where ID equals and the placeholder question 03:32.550 --> 03:40.770 mark that both Maisky Will and Maria DB require an ID is the substitution for that question mark and 03:40.770 --> 03:42.480 I'll just give myself some more room here. 03:43.440 --> 03:47.450 Error we'll check for an error is assigned the value of rodent scan. 03:47.460 --> 03:50.250 So we're scanning the variable into our widget. 03:50.250 --> 03:58.740 So we'll scan to into a reference to widget ID and a reference to widget dot name check for an error. 03:58.950 --> 04:07.680 If error is not equal to nil, then I'll return the widget even though I will ignore it and the error. 04:08.670 --> 04:10.770 Otherwise everything worked. 04:11.540 --> 04:17.550 Return widget and no, no, this database table widgets doesn't exist. 04:17.580 --> 04:25.410 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 04:25.410 --> 04:28.680 that we set up a few lectures ago and I'll create a new table. 04:32.810 --> 04:34.420 And I'll call this table, which is. 04:36.330 --> 04:40.470 And the structure, I want an ID and I'm just going to add one more field right now because we'll do 04:40.470 --> 04:42.250 this in a more intelligent way shortly. 04:42.690 --> 04:48.620 Name will be a marker of two fifty five and then I'll populated with one row. 04:49.290 --> 04:55.870 So one row, I'll let the ID be set by itself and I'll call the name widget with a capital W there. 04:57.840 --> 05:03.690 So now we have a means of testing this to make sure it all works, but we need to set up a handler to 05:03.690 --> 05:04.080 do that. 05:05.040 --> 05:08.920 Now I'm going to set the handler up in the back end just because I can. 05:09.300 --> 05:16.770 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 05:16.780 --> 05:29.490 Dongo, which means my application has to have a DB of type models, DB model format and then down here 05:30.520 --> 05:39.000 a simply populate the DB will be of type models that DB model and DB set to. 05:41.560 --> 05:43.350 OK, so that's now available there. 05:44.010 --> 05:49.800 Now let's set up a simple handler in the API to actually connect to that function we just created. 05:50.460 --> 05:57.030 So over here in Handler Dash API, Dutko will create a new function. 05:57.120 --> 05:58.410 And what's the receiver called? 05:59.400 --> 06:05.940 Application or created func with a receiver of app pointer to application. 06:06.450 --> 06:17.580 And I'll call this get widget ID and it's a handler, so it'll take a response writer for Retter Response 06:17.580 --> 06:22.200 Writer and a pointer to a request HDB request. 06:25.230 --> 06:30.510 And I'm going to get the ID from the URL and since we're using the Cerreta, that's pretty simple as 06:30.570 --> 06:33.720 a sign the value of the euro param. 06:35.760 --> 06:38.190 And I'm looking from the request. 06:38.370 --> 06:39.780 I'm looking for Heidi. 06:39.810 --> 06:45.540 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 06:45.540 --> 06:54.090 be an ID and potentially an error which I'll ignore for now is a sign the value of Sterkel A2. 06:54.090 --> 07:02.640 I will convert our string to an ID and now I'll try calling the database and because my back and my 07:02.640 --> 07:05.310 front end share code, this is pretty simple. 07:05.730 --> 07:08.100 I'll create a variable widget, I'll check for an error. 07:08.340 --> 07:17.670 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 07:17.670 --> 07:20.140 check for an error if error is not equal to nil. 07:21.000 --> 07:28.830 For now, I'll just say after error, log the print line error and I'll return, which is not very nice, 07:28.830 --> 07:33.150 but we'll fix that later on and we'll write this out as Jason. 07:33.160 --> 07:39.230 And this is an easy way to make sure it all works out an error or assign the value of Jason Marshall 07:39.240 --> 07:42.360 and Marshall in debt to make it pretty, which you shouldn't do in production. 07:42.360 --> 07:43.770 But for development, it's fine. 07:44.700 --> 07:50.310 We're going to Marshall Widget with no prefix and a few spaces to format everything nicely. 07:50.940 --> 07:52.050 We'll check for an error. 07:52.080 --> 07:55.620 I'll just copy this error checking code and pasted here. 07:56.700 --> 08:04.550 And finally we'll write this that header set content type applications. 08:05.280 --> 08:07.590 Jason and we wrote it. 08:07.600 --> 08:10.530 I write it. 08:13.620 --> 08:16.440 And I'm missing parentheses after her there. 08:17.620 --> 08:21.830 OK, so we have this get widget by ID, but we need to set up a route to it. 08:22.440 --> 08:27.420 So let's go to Route's API and we'll just create a room right here. 08:28.850 --> 08:29.190 Yep. 08:29.760 --> 08:36.810 And we're going to go to slash API slash widget slash and then some ID and I have to use the same value 08:36.810 --> 08:43.050 here ID as I did in my handler here where I'm looking for ID online seventy-nine. 08:44.100 --> 08:50.080 So that's our route, our URL and we want to go to app to get Widget by ID. 08:51.340 --> 08:58.020 OK, so let me open my terminal, clear the screen, stop everything good. 08:58.350 --> 09:04.560 And I'll just say make start and I will compile both the front and back end and now I should be able 09:04.560 --> 09:08.380 to go to this route on localhost four thousand one. 09:08.400 --> 09:09.000 So let's try it. 09:11.700 --> 09:24.490 I'll open a new tab and go to localhost four thousand and one API slash, get or slash widget slash 09:24.530 --> 09:28.570 one and there's one thing in my test table and there it is. 09:28.580 --> 09:31.900 And if you look at the raw data, that should all be nicely formatted, Jason. 09:31.910 --> 09:38.250 And it is so now we have a means of using our database and writing our queries, and that's pretty straightforward. 09:38.300 --> 09:42.560 So if we think about it, we can go back to our code and I'll stop everything. 09:43.800 --> 09:44.270 Stop. 09:48.290 --> 09:55.670 We'll simply go to our models, don't go file and write one routine and export it by giving it function, 09:55.670 --> 09:58.490 name a capital letter for all of the things we need to do. 09:58.490 --> 10:00.560 And that would be, number one, get a widget. 10:00.560 --> 10:01.680 And we have this right here. 10:01.700 --> 10:04.100 We'll have to fix up the table and make the query correct. 10:04.100 --> 10:05.090 But we'll do that shortly. 10:05.840 --> 10:10.700 And we'll also want a means of storing transactions, retrieving transactions, all those things. 10:10.970 --> 10:17.000 And the fact that I have these database functions in the shared code base between my Fronton and my 10:17.000 --> 10:23.180 back end means I can actually get the content from the database and serve it up as HTML or get the content 10:23.180 --> 10:27.740 from the database and served up as JSON or whatever format I need to use. 10:28.380 --> 10:35.390 OK, so in the next lecture will install a tool to run database migration's and we'll set up a proper 10:35.690 --> 10:40.760 widgets table and a proper transaction table and we'll get started on that in the next lecture.