ZAMYLA CHAN: For buy, we'll also want to display
a form for users to specify which stock they want to buy
and the number of shares of that stock they'd like to purchase.
So again, it sounds like it's time to add another template to our directory.
Then we'll want to add that stock to the user's portfolio
if they have enough cash and update their cash as appropriate.
For the display form, let's ask for the symbol of the stock
and the number of shares-- of course checking if this is valid input.
Now that we know how many shares of a particular stock
the user wants to purchase, let's see if they can actually afford it.
Select the cash column from the table users where the ID number equals 1.
And if that cash exceeds the total value of the shares
times the price of that stock, then I can go ahead and buy that stock.
If they can indeed afford it, then let's add that stock to the user's portfolio.
In the portfolio we'll want to keep track of how many shares of each stock
the user has.
Now the user's table that we have within our SQL database
doesn't have the capability to store that information yet
because our user's table simply has the user name, the user
ID, and the hash of their password.
So in addition to our users table we're going to want to add another one,
whether we call that "portfolio" or "transactions" or "history"
or any other appropriate name that reflects the type of table it is.
Within this table or perhaps combination of tables--
you could have to if you so choose-- we want
to keep track of who bought what at what price
and when using the appropriate SQLite types for the values
that we're inserting, defining unique indices on any fields that should
be unique, and then non-unique indices on any fields that we may be searching.
After successfully updating the user's virtual portfolio,
let's also update the cash to reflect the purchase that they just made.
Remember that a user's cash is stored in the user's table.
So, if I have my user with ID number one and they just
bought $50 worth of shares, then I'll execute the update query.
Update the user's table, setting cash equal
to cash minus 50 where my user is user with ID 1.
Now let's edit the landing page.