WEBVTT 00:02.150 --> 00:07.460 So we have this form displaying and as I said a couple of times now, we need to get rid of this amount 00:07.460 --> 00:12.650 field and we need to put the price as perhaps a hidden field. 00:12.650 --> 00:15.980 We'll display it, of course, because you don't want people to buy something, just guessing what it 00:15.980 --> 00:16.610 might cost. 00:16.910 --> 00:20.240 But we'll also have to put the price in a hidden field, and that's a good start. 00:20.240 --> 00:25.640 But as I suggested last time, that's not enough because a malicious user could simply open a JavaScript 00:25.640 --> 00:31.370 terminal, modify the price to whatever they want it to be in the Web page, and then submit it with 00:31.370 --> 00:32.990 the price of their choosing. 00:32.990 --> 00:34.100 And that's not ideal. 00:35.780 --> 00:37.970 So what we're going to do is a couple of things. 00:38.000 --> 00:45.020 First of all, if I was to modify this form and put a price, say, ten dollars, display it to the 00:45.020 --> 00:49.620 user and also include that as a hidden field, well, that's kind of awkward. 00:49.640 --> 00:54.410 It's fine if you're only selling one product, but if you're selling multiple products, you probably 00:54.410 --> 01:00.470 want to look up the product from the database, populate a struct with all the necessary information 01:00.470 --> 01:03.650 and pass that as data to this this page. 01:03.680 --> 01:06.940 So let's do that and we'll do it, first of all, by marking things up. 01:06.950 --> 01:08.150 So let's go to our code. 01:08.810 --> 01:15.140 And what I'm going to do is create a new item inside my internal folder, create a new folder. 01:15.350 --> 01:22.610 I'm going to call models and I'm putting it in there because I want to share these models between the 01:22.610 --> 01:23.480 front and back end. 01:23.480 --> 01:28.550 So I create a new file in here and I'll call it Models Dutko and we'll set up a few things we know we're 01:28.550 --> 01:29.620 going to need a little while. 01:30.050 --> 01:37.010 So the package name is models and the first thing I'll do is to find a type, the type that I'll be 01:37.010 --> 01:42.140 using when I share my database connection pool with my various handlers and whatever else they need 01:42.140 --> 01:42.450 to be. 01:42.860 --> 01:50.660 So I'll call this type DB model and it's a struct and it's just going to hold one field, which I'll 01:50.660 --> 01:58.790 call DB and that will be a pointer to sequel DB and I'll let Visual Studio Code do my imports and it 01:58.790 --> 01:59.120 did. 01:59.240 --> 02:01.640 OK, so let's give this description. 02:02.540 --> 02:08.150 DB model is the type for database connection. 02:09.790 --> 02:10.270 Values. 02:12.730 --> 02:18.520 Don't create a type called morals, which is, you know, this is absolutely not necessary, but I find 02:18.520 --> 02:19.330 it really convenient. 02:19.360 --> 02:20.830 So just follow along. 02:21.460 --> 02:30.880 This is going to hold DBE, which is a model, the type which is defined OK and models is the that are 02:30.890 --> 02:38.350 spelled out right is the wrapper for all models. 02:39.180 --> 02:47.020 OK and Downbelow, that will create a function that I'll be using shortly called new models. 02:48.280 --> 02:54.370 And this is where I'll create and hand around the DB model to every part of my application that needs 02:54.370 --> 02:54.510 it. 02:54.550 --> 03:02.330 So it takes one argument DB, which is a pointer to school DB and it returns models, the type we just 03:02.350 --> 03:03.360 find on the 911. 03:03.670 --> 03:15.010 So we just return models that type with the field DB equal to DB model and we populate that with our 03:15.010 --> 03:15.790 database connection. 03:15.900 --> 03:22.630 Did the argument this past of this and give it its comma or. 03:25.660 --> 03:31.540 Now I'm going to create a tape to hold a widget, and this ultimately will be populated with information 03:31.540 --> 03:35.590 from the database, but right now we're just going to create the tape so I can manually construct one 03:35.590 --> 03:36.850 and hand it to that page. 03:37.270 --> 03:38.950 So I'll create a tape, which I'll call widget. 03:39.340 --> 03:43.710 It's a struct and I'll assume that it's going to have an ID, which will be an IT. 03:43.750 --> 03:49.210 And in JSON, if we're using that or call it Jason, I'll call it Jason. 03:49.210 --> 03:50.410 I'd like that. 03:52.010 --> 03:52.820 That's going to have a name. 03:52.900 --> 04:03.520 So name String and then Jason, I'll call that name and a description will also be a string. 04:03.520 --> 04:06.400 And in Jason, I'll call that not surprisingly, description. 04:08.830 --> 04:14.110 It will also have inventory level, how many widgets do we have? 04:14.260 --> 04:16.440 That'll be an Int and adjacent. 04:17.870 --> 04:21.880 I'll call that inventory, underscore the level, 04:24.730 --> 04:28.570 obviously going to have to have a price, which will be an inch, and we'll store that as an end because 04:28.570 --> 04:32.020 we're not going to store decimal values to avoid floating point errors. 04:32.680 --> 04:34.000 Jason Price. 04:36.070 --> 04:44.320 And I'll also give you two things I always do created, which will be timed on time and in Jason. 04:44.320 --> 04:47.650 I'm not going to hand that information around, so I'll just use the dash. 04:49.180 --> 04:56.470 And I also have updated it and that information will be stored in the database, which lets me know 04:56.470 --> 05:01.810 when something was created and when something was updated, format everything. 05:02.170 --> 05:03.790 And I'll give this its comment. 05:07.320 --> 05:13.800 Widget is the type for all widgets and new models. 05:17.960 --> 05:25.670 Returns a model type with database connection. 05:27.650 --> 05:32.660 OK, so now we have a Type four widget and we have all these other things will be using a little bit 05:32.660 --> 05:33.170 later on. 05:33.170 --> 05:35.540 But this is what I really wanted to have this time around. 05:36.860 --> 05:45.140 So now let's come back to our handler and find the one that displays the single item for purchase right 05:45.140 --> 05:45.350 here. 05:46.160 --> 05:53.360 So what I'll do here is create a new object or a new variable called widget, and that will be assigned 05:54.290 --> 05:55.700 model's widget. 05:58.430 --> 06:00.230 I did call that models, didn't I? 06:00.290 --> 06:02.730 Let's find a package models. 06:02.770 --> 06:03.020 Yeah. 06:05.080 --> 06:07.000 So I didn't get models. 06:08.610 --> 06:10.080 Well, just manually import that. 06:15.600 --> 06:20.060 I don't know why sometimes visual studio does it and sometimes it doesn't, but it's no big deal. 06:21.090 --> 06:24.570 And I call this my app model's. 06:29.320 --> 06:35.400 Or you not finding that models, models don't go models internal. 06:36.220 --> 06:36.970 It all looks right. 06:38.560 --> 06:40.350 Let's just say this and see what happens. 06:41.340 --> 06:48.000 And it's saying, could not import my models, no record or see what I actually called this project, 06:48.150 --> 06:49.080 I called it my app. 06:51.560 --> 06:55.700 OK, let's just see if it'll manually do it for us or automatically do it for us. 06:55.710 --> 06:59.490 Maybe it just hasn't caught up to us down here. 07:02.010 --> 07:10.500 Let's try this again, Richard, as a sign the value of from models dot, there it is now it found it 07:10.620 --> 07:16.280 OK and I'll populate its fields, so I'll give it an idea of one. 07:17.160 --> 07:18.780 I'll give it a name of. 07:21.290 --> 07:22.310 Customer widget. 07:24.780 --> 07:26.640 I'll give it a description of 07:29.790 --> 07:39.600 a very nice widget and I'll give it an inventory level of ten, we have ten in stock and I'll give it 07:39.600 --> 07:44.590 a price of ten dollars, which will be stored as 10 00. 07:46.080 --> 07:46.470 OK. 07:48.990 --> 07:55.980 Now, I'll create a data type to hold that widget so I can pass it to the template, so I will say data 07:56.010 --> 08:01.500 is a sign the value of make a map string interface 08:05.430 --> 08:08.880 and I'll populate that with data dot widget. 08:11.490 --> 08:22.650 Equals widget, and I will hand that off in here by creating a reference to template data and we'll 08:22.650 --> 08:25.860 go we'll give it data is equal to. 08:28.160 --> 08:34.570 Now, when this recompile and hopefully it's recompiling up here got confused, we'll just start again. 08:37.070 --> 08:42.910 There it is now I can pass that information off to my template, so my template now has this. 08:43.430 --> 08:49.730 And that means, of course, that I already know when this page is rendered, what its ideas, its description, 08:49.730 --> 08:52.910 its name, its price and how many we have for sale as well. 08:52.910 --> 08:57.790 So we can actually hide the buy now button if we don't have anything left in stock. 08:58.520 --> 09:00.220 So that's a really convenient way of doing this. 09:00.230 --> 09:06.770 Now, obviously, what we're ultimately going to do is create a table in our database to hold our inventory. 09:06.770 --> 09:13.580 All of our items populate that with widget, our custom widget, and we'll go grab that from the database 09:13.580 --> 09:15.080 before we render this page. 09:15.110 --> 09:17.760 But right now, we're just marking it up and that's sufficient for the moment. 09:18.320 --> 09:22.760 So in the next lecture, we'll modify the template to use the data that we're passing it.