SPEAKER 1: Let's let our user buy some stocks.
The first thing that you'll want to do in Buy
is display a form where the user will input
the stock and the number of shares that they want to buy of that stock.
Then, checking that everything is valid, you'll
want to add that stock to the user's portfolio and update their cash.
So let's talk about the display form.
Yet again, it's going to be another template
that we create where, within this form, we'll ask for the symbol of the stock
and the number of shares, checking for valid input.
From there, let's add the stock to the user's portfolio.
But we'll only want to allow them to buy it if they can afford that stock.
If I'm dealing with my user with ID number one,
then I could execute the following SQL query,
selecting the Cash column from the user's table where ID equals one.
From there, I can see whether they can afford it or not.
Now, looking at the structure of the user's table,
we only have three columns--
user name, ID, and the hash of their password.
We don't have the capacity to keep track of the user's purchase.
So let's make a new SQL table.
For this, we want to keep track of who bought what at what price, and when.
So make sure to use the appropriate SQLite
types when you're creating this table, defining
unique indices on any fields that should be unique and non-unique for all
of the others.
Feel free to call your table portfolio, transactions, history,
or any other appropriate name.
You could also create two tables if you choose
to structure your database that way.
All right, now that we've added the stock to the user's portfolio,
let's make sure to subtract the right amount of cash.
Remember that a user's cash is stored in the user's table.
So if I'm again dealing with my user with ID number one
then I would execute this following query, updating the user's table,
setting the cash column to cash minus $50,
let's say, where that user ID is equal to one.
And with that, you've bought your first stock.