1 00:00:00,000 --> 00:00:00,499 2 00:00:00,499 --> 00:00:03,705 SPEAKER 1: Let's let our user buy some stocks. 3 00:00:03,705 --> 00:00:05,580 The first thing that you'll want to do in Buy 4 00:00:05,580 --> 00:00:08,100 is display a form where the user will input 5 00:00:08,100 --> 00:00:12,150 the stock and the number of shares that they want to buy of that stock. 6 00:00:12,150 --> 00:00:15,120 Then, checking that everything is valid, you'll 7 00:00:15,120 --> 00:00:20,050 want to add that stock to the user's portfolio and update their cash. 8 00:00:20,050 --> 00:00:21,840 So let's talk about the display form. 9 00:00:21,840 --> 00:00:24,090 Yet again, it's going to be another template 10 00:00:24,090 --> 00:00:28,590 that we create where, within this form, we'll ask for the symbol of the stock 11 00:00:28,590 --> 00:00:32,940 and the number of shares, checking for valid input. 12 00:00:32,940 --> 00:00:36,240 From there, let's add the stock to the user's portfolio. 13 00:00:36,240 --> 00:00:40,800 But we'll only want to allow them to buy it if they can afford that stock. 14 00:00:40,800 --> 00:00:43,980 If I'm dealing with my user with ID number one, 15 00:00:43,980 --> 00:00:47,220 then I could execute the following SQL query, 16 00:00:47,220 --> 00:00:52,710 selecting the Cash column from the user's table where ID equals one. 17 00:00:52,710 --> 00:00:55,870 From there, I can see whether they can afford it or not. 18 00:00:55,870 --> 00:00:58,050 Now, looking at the structure of the user's table, 19 00:00:58,050 --> 00:00:59,810 we only have three columns-- 20 00:00:59,810 --> 00:01:03,330 user name, ID, and the hash of their password. 21 00:01:03,330 --> 00:01:07,840 We don't have the capacity to keep track of the user's purchase. 22 00:01:07,840 --> 00:01:10,290 So let's make a new SQL table. 23 00:01:10,290 --> 00:01:15,450 For this, we want to keep track of who bought what at what price, and when. 24 00:01:15,450 --> 00:01:17,760 So make sure to use the appropriate SQLite 25 00:01:17,760 --> 00:01:20,760 types when you're creating this table, defining 26 00:01:20,760 --> 00:01:25,770 unique indices on any fields that should be unique and non-unique for all 27 00:01:25,770 --> 00:01:27,150 of the others. 28 00:01:27,150 --> 00:01:30,570 Feel free to call your table portfolio, transactions, history, 29 00:01:30,570 --> 00:01:32,670 or any other appropriate name. 30 00:01:32,670 --> 00:01:35,580 You could also create two tables if you choose 31 00:01:35,580 --> 00:01:37,770 to structure your database that way. 32 00:01:37,770 --> 00:01:40,870 All right, now that we've added the stock to the user's portfolio, 33 00:01:40,870 --> 00:01:44,640 let's make sure to subtract the right amount of cash. 34 00:01:44,640 --> 00:01:48,000 Remember that a user's cash is stored in the user's table. 35 00:01:48,000 --> 00:01:51,930 So if I'm again dealing with my user with ID number one 36 00:01:51,930 --> 00:01:56,590 then I would execute this following query, updating the user's table, 37 00:01:56,590 --> 00:02:00,870 setting the cash column to cash minus $50, 38 00:02:00,870 --> 00:02:05,010 let's say, where that user ID is equal to one. 39 00:02:05,010 --> 00:02:08,300 And with that, you've bought your first stock. 40 00:02:08,300 --> 00:02:09,449