So we have this form displaying and as I said a couple of times now, we need to get rid of this amount
field and we need to put the price as perhaps a hidden field.
We'll display it, of course, because you don't want people to buy something, just guessing what it
might cost.
But we'll also have to put the price in a hidden field, and that's a good start.
But as I suggested last time, that's not enough because a malicious user could simply open a JavaScript
terminal, modify the price to whatever they want it to be in the Web page, and then submit it with
the price of their choosing.
And that's not ideal.
So what we're going to do is a couple of things.
First of all, if I was to modify this form and put a price, say, ten dollars, display it to the
user and also include that as a hidden field, well, that's kind of awkward.
It's fine if you're only selling one product, but if you're selling multiple products, you probably
want to look up the product from the database, populate a struct with all the necessary information
and pass that as data to this this page.
So let's do that and we'll do it, first of all, by marking things up.
So let's go to our code.
And what I'm going to do is create a new item inside my internal folder, create a new folder.
I'm going to call models and I'm putting it in there because I want to share these models between the
front and back end.
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
going to need a little while.
So the package name is models and the first thing I'll do is to find a type, the type that I'll be
using when I share my database connection pool with my various handlers and whatever else they need
to be.
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
call DB and that will be a pointer to sequel DB and I'll let Visual Studio Code do my imports and it
did.
OK, so let's give this description.
DB model is the type for database connection.
Values.
Don't create a type called morals, which is, you know, this is absolutely not necessary, but I find
it really convenient.
So just follow along.
This is going to hold DBE, which is a model, the type which is defined OK and models is the that are
spelled out right is the wrapper for all models.
OK and Downbelow, that will create a function that I'll be using shortly called new models.
And this is where I'll create and hand around the DB model to every part of my application that needs
it.
So it takes one argument DB, which is a pointer to school DB and it returns models, the type we just
find on the 911.
So we just return models that type with the field DB equal to DB model and we populate that with our
database connection.
Did the argument this past of this and give it its comma or.
Now I'm going to create a tape to hold a widget, and this ultimately will be populated with information
from the database, but right now we're just going to create the tape so I can manually construct one
and hand it to that page.
So I'll create a tape, which I'll call widget.
It's a struct and I'll assume that it's going to have an ID, which will be an IT.
And in JSON, if we're using that or call it Jason, I'll call it Jason.
I'd like that.
That's going to have a name.
So name String and then Jason, I'll call that name and a description will also be a string.
And in Jason, I'll call that not surprisingly, description.
It will also have inventory level, how many widgets do we have?
That'll be an Int and adjacent.
I'll call that inventory, underscore the level,
obviously going to have to have a price, which will be an inch, and we'll store that as an end because
we're not going to store decimal values to avoid floating point errors.
Jason Price.
And I'll also give you two things I always do created, which will be timed on time and in Jason.
I'm not going to hand that information around, so I'll just use the dash.
And I also have updated it and that information will be stored in the database, which lets me know
when something was created and when something was updated, format everything.
And I'll give this its comment.
Widget is the type for all widgets and new models.
Returns a model type with database connection.
OK, so now we have a Type four widget and we have all these other things will be using a little bit
later on.
But this is what I really wanted to have this time around.
So now let's come back to our handler and find the one that displays the single item for purchase right
here.
So what I'll do here is create a new object or a new variable called widget, and that will be assigned
model's widget.
I did call that models, didn't I?
Let's find a package models.
Yeah.
So I didn't get models.
Well, just manually import that.
I don't know why sometimes visual studio does it and sometimes it doesn't, but it's no big deal.
And I call this my app model's.
Or you not finding that models, models don't go models internal.
It all looks right.
Let's just say this and see what happens.
And it's saying, could not import my models, no record or see what I actually called this project,
I called it my app.
OK, let's just see if it'll manually do it for us or automatically do it for us.
Maybe it just hasn't caught up to us down here.
Let's try this again, Richard, as a sign the value of from models dot, there it is now it found it
OK and I'll populate its fields, so I'll give it an idea of one.
I'll give it a name of.
Customer widget.
I'll give it a description of
a very nice widget and I'll give it an inventory level of ten, we have ten in stock and I'll give it
a price of ten dollars, which will be stored as 10 00.
OK.
Now, I'll create a data type to hold that widget so I can pass it to the template, so I will say data
is a sign the value of make a map string interface
and I'll populate that with data dot widget.
Equals widget, and I will hand that off in here by creating a reference to template data and we'll
go we'll give it data is equal to.
Now, when this recompile and hopefully it's recompiling up here got confused, we'll just start again.
There it is now I can pass that information off to my template, so my template now has this.
And that means, of course, that I already know when this page is rendered, what its ideas, its description,
its name, its price and how many we have for sale as well.
So we can actually hide the buy now button if we don't have anything left in stock.
So that's a really convenient way of doing this.
Now, obviously, what we're ultimately going to do is create a table in our database to hold our inventory.
All of our items populate that with widget, our custom widget, and we'll go grab that from the database
before we render this page.
But right now, we're just marking it up and that's sufficient for the moment.
So in the next lecture, we'll modify the template to use the data that we're passing it.