1 00:00:00,550 --> 00:00:02,351 Our app is looking great, but 2 00:00:02,351 --> 00:00:08,010 I think we can add a few more features to make this even more useful for our users. 3 00:00:08,010 --> 00:00:11,211 What happens if they make a mistake when entering a book or 4 00:00:11,211 --> 00:00:13,440 what if the price changes? 5 00:00:13,440 --> 00:00:16,750 Right now you can't do anything about it. 6 00:00:16,750 --> 00:00:20,270 Also, what if you want to remove a book? 7 00:00:20,270 --> 00:00:23,260 Let's introduce these two features to our app. 8 00:00:23,260 --> 00:00:26,150 We can add them as a part of our search feature. 9 00:00:26,150 --> 00:00:30,296 When someone searches for a book, we can give them the option to edit, 10 00:00:30,296 --> 00:00:33,250 delete or return to the main menu. 11 00:00:33,250 --> 00:00:37,370 Let's handle this like our other menu, by giving it its own function. 12 00:00:37,370 --> 00:00:39,698 To keep similar things together, 13 00:00:39,698 --> 00:00:43,321 let's create our sub menu right below our main menu. 14 00:00:45,100 --> 00:00:53,444 I can actually remove these, There we go, 15 00:00:53,444 --> 00:01:00,440 submenu, and we can actually copy this here. 16 00:01:00,440 --> 00:01:05,785 And if you want, you could actually make a single menu function and 17 00:01:05,785 --> 00:01:11,905 then pass in values for the options and the list of numbers and all that jazz. 18 00:01:15,885 --> 00:01:20,529 All right, so we only have three options and it's no longer programming books, 19 00:01:20,529 --> 00:01:22,170 we can also get rid of that. 20 00:01:22,170 --> 00:01:27,166 One will be edit, two will be delete, 21 00:01:27,166 --> 00:01:32,170 and three will be return to main menu. 22 00:01:33,960 --> 00:01:38,017 And then we can get rid of four and five, so we only have three options, and 23 00:01:38,017 --> 00:01:40,840 then we can get rid of five, and switch it to three. 24 00:01:42,150 --> 00:01:42,650 Wonderful. 25 00:01:43,680 --> 00:01:47,020 Now we can create the structure for each response in our app. 26 00:01:47,020 --> 00:01:49,493 So let's jump down to app.pi. 27 00:01:56,046 --> 00:01:57,620 Okay, so we have search for book. 28 00:01:58,690 --> 00:02:04,040 And then instead of this input, we're going to give them a submenu. 29 00:02:04,040 --> 00:02:08,660 So I'm gonna call it sub_choice = submenu. 30 00:02:10,120 --> 00:02:14,470 And then, if sub_choice 31 00:02:14,470 --> 00:02:19,609 == '1' this will be edit, 32 00:02:19,609 --> 00:02:23,790 pass for right now. 33 00:02:23,790 --> 00:02:28,980 And then, elif, sub_ choice = '2'. 34 00:02:30,200 --> 00:02:32,490 This will be delete. 35 00:02:32,490 --> 00:02:34,690 And I'll still put pass for right now. 36 00:02:34,690 --> 00:02:38,366 And then we actually don't need to do one for three because of our loop, 37 00:02:38,366 --> 00:02:40,770 it'll automatically jump it back to the top. 38 00:02:42,330 --> 00:02:44,360 Now it's time to tackle edit. 39 00:02:45,490 --> 00:02:48,852 Let's think about what we need to happen here a bit first. 40 00:02:48,852 --> 00:02:53,751 We'll need to print out the current value for each book's title, date, etc., so 41 00:02:53,751 --> 00:02:56,379 the user can see what the value currently is. 42 00:02:56,379 --> 00:02:59,630 Then we'll need to ask them to update the information. 43 00:03:00,670 --> 00:03:05,198 And we'll need to repeat this for each column, the title, author, 44 00:03:05,198 --> 00:03:07,080 price, date, all of them. 45 00:03:08,490 --> 00:03:10,921 And the keyword here is repeat. 46 00:03:10,921 --> 00:03:15,440 We're going to need to do this task for each value. 47 00:03:15,440 --> 00:03:19,096 So that means we're going to need a function to handle this task so 48 00:03:19,096 --> 00:03:21,100 that we can call it for each column. 49 00:03:22,380 --> 00:03:26,723 I'm going to place this new function after our cleaning functions and 50 00:03:26,723 --> 00:03:28,352 before the CSV function. 51 00:03:31,680 --> 00:03:32,396 Right here. 52 00:03:37,315 --> 00:03:41,897 I'm gonna call it edit_check, and inside, 53 00:03:41,897 --> 00:03:46,150 we're going to get the column_name. 54 00:03:46,150 --> 00:03:48,617 This is because for the price and for the date, 55 00:03:48,617 --> 00:03:52,800 we're gonna need to do something different compared to the author and title. 56 00:03:54,000 --> 00:03:57,620 And then we want the current_value. 57 00:03:59,930 --> 00:04:01,140 Okay. 58 00:04:01,140 --> 00:04:04,765 And inside of our edit_check function, let's print out the column names so 59 00:04:04,765 --> 00:04:07,010 they know which column they're dealing with. 60 00:04:08,280 --> 00:04:13,103 I make this an F string and I start it on a new line, 61 00:04:13,103 --> 00:04:17,811 and I give a couple stars, I'm gonna do EDIT and 62 00:04:17,811 --> 00:04:24,910 then column_name, And a few stars, close it out. 63 00:04:24,910 --> 00:04:26,340 Cool. 64 00:04:26,340 --> 00:04:29,620 Now we need to show the current value for each column. 65 00:04:29,620 --> 00:04:32,369 But remember if it's the price or the published date, 66 00:04:32,369 --> 00:04:36,710 we're going to need to show it how we want to receive the information too. 67 00:04:36,710 --> 00:04:42,240 So for instance, let's do if column_name == 'Price'. 68 00:04:43,560 --> 00:04:49,512 We're going to print, An f string of the current value. 69 00:04:52,611 --> 00:04:59,981 Current Value, and we're going to need the current_value and 70 00:04:59,981 --> 00:05:03,810 it needs to be divided by 100. 71 00:05:03,810 --> 00:05:08,906 So it's now back into that 1099 price instead of the price in cents, 72 00:05:08,906 --> 00:05:12,090 cuz that's how we want to receive the price. 73 00:05:12,090 --> 00:05:14,036 So if we model it here for the user, 74 00:05:14,036 --> 00:05:17,740 then it kinda shows what we want the information to come in as too. 75 00:05:18,810 --> 00:05:22,114 So let's do the same thing for the date. 76 00:05:28,799 --> 00:05:36,190 Okay, so if the column_name == 'Date', we still want to print an f string, 77 00:05:36,190 --> 00:05:40,990 and this is going to say same thing, Current Value. 78 00:05:42,320 --> 00:05:46,467 And to translate the date into the format we want, 79 00:05:46,467 --> 00:05:52,318 we're going to need to use current_value.strftime to convert it. 80 00:05:52,318 --> 00:05:52,948 And oops, 81 00:05:52,948 --> 00:05:57,770 we need to make sure to use double quotes because we're inside of single quotes. 82 00:05:57,770 --> 00:06:00,160 So we want to make sure we use double quotes here. 83 00:06:00,160 --> 00:06:06,046 And to get the month converted to the name of the month, we want %B, 84 00:06:06,046 --> 00:06:11,931 and then, we need the day, that's %d, and then we need the year, 85 00:06:11,931 --> 00:06:18,220 that's %Y to get the full year, Okay? 86 00:06:18,220 --> 00:06:20,590 And then, we need an else. 87 00:06:20,590 --> 00:06:24,388 So if it's any of the other columns, we're just going to print the value. 88 00:06:26,190 --> 00:06:32,334 String, do a return, Current Value. 89 00:06:32,334 --> 00:06:34,364 And that's just going to be current_value. 90 00:06:36,415 --> 00:06:37,880 Great. 91 00:06:37,880 --> 00:06:39,040 Okay. 92 00:06:39,040 --> 00:06:43,434 Now we're also going to need to do something a little special if it's 93 00:06:43,434 --> 00:06:48,661 the published date or the price because we need to use our clean date and clean price 94 00:06:48,661 --> 00:06:54,690 functions again, so that we can turn their string input into the correct format. 95 00:06:54,690 --> 00:06:58,635 So, I'm gonna just to help break it up a little bit, I'm gonna add 96 00:06:58,635 --> 00:07:02,868 a space here to help differentiate between these kind of two sections. 97 00:07:02,868 --> 00:07:06,315 So we printed out the information so they can see it and now we're gonna 98 00:07:06,315 --> 00:07:09,830 get into actually asking them for their input and interacting with it. 99 00:07:10,930 --> 00:07:14,622 So if column_name == 'Date' or 100 00:07:14,622 --> 00:07:18,561 if the column_name == 'Price', 101 00:07:18,561 --> 00:07:23,745 then we're going to want to do some stuff in here. 102 00:07:23,745 --> 00:07:27,167 And it's going to be inside of a while loop because we have to work with 103 00:07:27,167 --> 00:07:30,059 the clean date functions and we need to keep asking them for 104 00:07:30,059 --> 00:07:33,770 information until they give it to us in the correct format. 105 00:07:33,770 --> 00:07:36,270 I'm just gonna put pass inside for right now. 106 00:07:36,270 --> 00:07:41,416 Okay, and then on the outside, we're gonna need an else, and we can just return 107 00:07:41,416 --> 00:07:47,560 if it's not one of these two columns, that means it's either the author or the title. 108 00:07:47,560 --> 00:07:49,513 And so we can just ask for the input and 109 00:07:49,513 --> 00:07:52,830 we can just return their response right away. 110 00:07:52,830 --> 00:07:55,820 Because it's in a string format and that's all it needs to be. 111 00:07:58,040 --> 00:08:05,281 So 'What would you like to change the value? 112 00:08:05,281 --> 00:08:09,670 ' Okay, so now let's handle inside of our while loop. 113 00:08:11,770 --> 00:08:13,190 We need to ask the same question. 114 00:08:13,190 --> 00:08:14,660 So I'm gonna call it changes. 115 00:08:15,820 --> 00:08:19,108 And I'm actually just gonna use the exact same question. 116 00:08:22,090 --> 00:08:23,060 There we go. 117 00:08:23,060 --> 00:08:23,830 Okay. 118 00:08:23,830 --> 00:08:27,600 And then we need to call clean date for the date and clean price for the price. 119 00:08:27,600 --> 00:08:31,380 And now we want to break it up then into either the date or the price. 120 00:08:31,380 --> 00:08:35,870 So column_name == 'Date'. 121 00:08:35,870 --> 00:08:40,660 Then we're going to do something very similar to what we did before. 122 00:08:40,660 --> 00:08:45,727 And say the changes are now equal 123 00:08:45,727 --> 00:08:50,987 to the clean version of changes. 124 00:08:50,987 --> 00:08:57,278 And if the type(changes) == datetime.date, 125 00:08:57,278 --> 00:09:00,788 then we can return changes, 126 00:09:00,788 --> 00:09:07,650 cuz remember return automatically stops a loop. 127 00:09:07,650 --> 00:09:11,257 And that's why we're using while true instead of using a variable to 128 00:09:11,257 --> 00:09:12,260 control our loop. 129 00:09:13,380 --> 00:09:16,731 Okay, so that takes care of the date and 130 00:09:16,731 --> 00:09:21,922 we're gonna do essentially the exact same thing for price. 131 00:09:21,922 --> 00:09:27,378 == 'Price' to changes 132 00:09:27,378 --> 00:09:31,349 = clean_price. 133 00:09:31,349 --> 00:09:33,691 And we're gonna pass in changes. 134 00:09:33,691 --> 00:09:38,716 And then if type(changes) is 135 00:09:38,716 --> 00:09:45,680 an integer then we can return our changes. 136 00:09:47,350 --> 00:09:48,080 And let's save. 137 00:09:49,930 --> 00:09:50,430 Awesome. 138 00:09:52,660 --> 00:09:57,259 Now let's pop down into the app.pi and call our new function. 139 00:10:00,475 --> 00:10:02,560 Here we are in edit. 140 00:10:02,560 --> 00:10:05,409 Now remember when we're updating a value, 141 00:10:05,409 --> 00:10:09,190 we need to set the current value equal to something else. 142 00:10:09,190 --> 00:10:11,840 And we've already grabbed our book previously. 143 00:10:11,840 --> 00:10:15,290 We have it here in a variable called the_book. 144 00:10:15,290 --> 00:10:19,861 So we just need to set the_book.title equal to and 145 00:10:19,861 --> 00:10:27,100 then we'll call our edit_check('Title') to pass in the column name. 146 00:10:27,100 --> 00:10:31,054 And then we can actually just copy this because it's the same thing, 147 00:10:31,054 --> 00:10:34,640 the_book.title to pass in the current value. 148 00:10:34,640 --> 00:10:36,850 And we'll do that for each column. 149 00:10:36,850 --> 00:10:42,280 So the_book.author, and I'm gonna copy that. 150 00:10:42,280 --> 00:10:48,745 Goes edit_check('Author'), and then the_book.author. 151 00:10:48,745 --> 00:10:52,147 And the next one the_book, 152 00:10:52,147 --> 00:10:56,968 and then we'll do .published_date, 153 00:10:56,968 --> 00:11:02,214 copy that, = edit_check('Date'), 154 00:11:02,214 --> 00:11:08,043 and then fill in the_book.published_date. 155 00:11:08,043 --> 00:11:11,055 And lastly the_book.price, 156 00:11:13,345 --> 00:11:18,340 Copy that, and =edit_check('Price'). 157 00:11:19,810 --> 00:11:22,158 And then the_book.price. 158 00:11:22,158 --> 00:11:27,957 And then below here let's do a print(session.dirty) 159 00:11:27,957 --> 00:11:35,460 to see if the changes are being grabbed by our session, just to make sure. 160 00:11:36,500 --> 00:11:39,010 So let's make sure we saved. 161 00:11:39,010 --> 00:11:43,340 And I'm gonna pull this up so you can see more of our console. 162 00:11:43,340 --> 00:11:44,449 And let's run the file. 163 00:11:46,674 --> 00:11:50,004 Oops, I have an IndentationError, line 29. 164 00:11:50,004 --> 00:11:50,770 Let's go check it out. 165 00:11:52,960 --> 00:11:56,393 Popping up to line 29. 166 00:11:59,632 --> 00:12:00,340 Yes. 167 00:12:01,426 --> 00:12:06,560 Somehow we got menu inside of, oops, I don't know how that happened. 168 00:12:06,560 --> 00:12:07,610 We don't need that. 169 00:12:07,610 --> 00:12:08,110 There we go. 170 00:12:09,200 --> 00:12:10,527 That should be much better. 171 00:12:16,700 --> 00:12:17,470 There we go. 172 00:12:17,470 --> 00:12:18,510 Okay. 173 00:12:18,510 --> 00:12:20,300 So let's search for a book. 174 00:12:20,300 --> 00:12:23,110 Let's do book 11 cuz we know that's my silly one. 175 00:12:24,970 --> 00:12:27,041 Jethro's Favorites, let's edit it. 176 00:12:27,041 --> 00:12:28,260 So I'm gonna do one. 177 00:12:29,440 --> 00:12:31,460 Okay, so the current value is Jethro's Favorites. 178 00:12:31,460 --> 00:12:33,339 What would you like to change the value to? 179 00:12:33,339 --> 00:12:36,340 And here in my console, I can copy and paste. 180 00:12:36,340 --> 00:12:39,570 I know in some consoles it doesn't always let you. 181 00:12:39,570 --> 00:12:40,589 I'm just gonna leave that. 182 00:12:40,589 --> 00:12:45,246 I'm gonna change this value to Jethro Amendola. 183 00:12:45,246 --> 00:12:46,427 What do I want to change the date to? 184 00:12:46,427 --> 00:12:52,950 Let's change the date to, actually, let's test this, 23rd 2003. 185 00:12:52,950 --> 00:12:54,210 It should give me an error. 186 00:12:54,210 --> 00:12:55,730 Cool, and that should ask me again. 187 00:12:57,370 --> 00:12:58,989 What would you like to change the value to? 188 00:12:58,989 --> 00:13:00,000 Awesome. 189 00:13:00,000 --> 00:13:01,032 Now let's give it a real date. 190 00:13:01,032 --> 00:13:04,967 Let's do June 14, 2020. 191 00:13:06,802 --> 00:13:08,850 And now we're on Price, perfect. 192 00:13:08,850 --> 00:13:09,432 So it took it. 193 00:13:09,432 --> 00:13:11,010 Let's give it an incorrect price here. 194 00:13:11,010 --> 00:13:14,317 So let's do $4 but with the dollar symbol. 195 00:13:14,317 --> 00:13:17,160 And, yep, gave us that error. 196 00:13:17,160 --> 00:13:19,180 Let's press enter to try again. 197 00:13:19,180 --> 00:13:22,600 And let's just keep it at 5.99. 198 00:13:22,600 --> 00:13:23,930 Cool. 199 00:13:23,930 --> 00:13:26,486 And you can see we have our identity set here, 200 00:13:26,486 --> 00:13:29,610 which means that it is captured by session. 201 00:13:29,610 --> 00:13:32,656 And we have Jethro's Favorites, we can see the change to the author, 202 00:13:32,656 --> 00:13:35,930 the change to the published date, and the price is the same. 203 00:13:35,930 --> 00:13:36,590 Amazing job. 204 00:13:38,240 --> 00:13:40,720 So our changes are being added to session. 205 00:13:40,720 --> 00:13:45,620 But to make the changes reflected in the database, let's make sure to commit them. 206 00:13:50,195 --> 00:13:53,140 And then let's print out a nice message so 207 00:13:53,140 --> 00:13:56,430 that the user knows that our book was updated. 208 00:13:58,470 --> 00:14:05,719 And then let's do a time.sleep, 1.5. 209 00:14:05,719 --> 00:14:09,080 Save, and amazing, our edit functions are working. 210 00:14:10,140 --> 00:14:11,760 Now let's get to delete. 211 00:14:13,030 --> 00:14:16,690 This one is 1,000 times easier, I promise. 212 00:14:16,690 --> 00:14:17,220 Are you ready? 213 00:14:18,300 --> 00:14:21,570 We already know we still have access to the book. 214 00:14:21,570 --> 00:14:27,640 So we'll do a session.delete(the_book), 215 00:14:27,640 --> 00:14:30,601 session.commit, and 216 00:14:30,601 --> 00:14:35,485 then we can just copy our message here so 217 00:14:35,485 --> 00:14:42,749 they get the same message, except this will say deleted. 218 00:14:44,552 --> 00:14:45,910 And that's it. 219 00:14:47,860 --> 00:14:49,467 That's the code. 220 00:14:49,467 --> 00:14:53,675 Let me save this to Python app.pi. 221 00:14:53,675 --> 00:14:58,870 Let's do 3 and 11, since that's our silly book. 222 00:15:01,190 --> 00:15:07,210 And scroll up so you can see that it's Jethro's Favorites. 223 00:15:07,210 --> 00:15:11,590 And let's do 2 for delete, book deleted. 224 00:15:11,590 --> 00:15:14,913 And then we can check by running, view all books and 225 00:15:14,913 --> 00:15:18,170 we only have ten books in the database, amazing. 226 00:15:19,210 --> 00:15:24,230 Pat yourself on the back, you've tackled a ton of code so far in this course. 227 00:15:24,230 --> 00:15:27,214 Don't forget to take time to review what you've written and 228 00:15:27,214 --> 00:15:31,460 maybe go line by line and try to explain to yourself what's happening. 229 00:15:31,460 --> 00:15:34,874 This is a great way to get used to thinking about how your code 230 00:15:34,874 --> 00:15:38,320 is working and is a skill used in technical interviews. 231 00:15:38,320 --> 00:15:41,520 So use every opportunity to practice. 232 00:15:41,520 --> 00:15:45,252 Don't forget, run your Git commands, add, commit and 233 00:15:45,252 --> 00:15:47,519 push your changes up to your repo,