1 00:00:00,430 --> 00:00:05,000 Next on our list is to tackle finding a specific book by ID. 2 00:00:06,380 --> 00:00:11,095 First, let's help our user out by letting them know the range for 3 00:00:11,095 --> 00:00:12,646 IDs in our database. 4 00:00:12,646 --> 00:00:17,343 We can do this by creating 5 00:00:17,343 --> 00:00:22,488 an id_options as a list and 6 00:00:22,488 --> 00:00:31,438 then do for book in session.query(Book) and 7 00:00:31,438 --> 00:00:37,704 then do id_options.append and 8 00:00:37,704 --> 00:00:43,763 let's do our book.id, okay? 9 00:00:43,763 --> 00:00:48,599 Awesome, you may be asking why not just grab the length of the database and 10 00:00:48,599 --> 00:00:50,730 use that as a range? 11 00:00:50,730 --> 00:00:55,907 Well, when an entry is deleted that ID is no longer in our database. 12 00:00:55,907 --> 00:01:01,151 For instance, if we had a range of books that was one through five. 13 00:01:01,151 --> 00:01:04,407 So 1, 2, 3, 4, 5. 14 00:01:04,407 --> 00:01:07,440 And we deleted the book with the ID of 2. 15 00:01:07,440 --> 00:01:12,530 Well, now our range is 1 and then 3 through 5. 16 00:01:12,530 --> 00:01:16,269 And so just to be more clear about what the options are, 17 00:01:16,269 --> 00:01:19,700 I think it's best if we just show them to our user. 18 00:01:20,980 --> 00:01:25,960 So let's do print, and actually let's 19 00:01:25,960 --> 00:01:30,940 do this as an input and 1, 2, 3, 4, 20 00:01:30,940 --> 00:01:35,480 5, 6, just so we can split it out on 21 00:01:35,480 --> 00:01:40,615 a couple lines and let's say Id Options. 22 00:01:40,615 --> 00:01:44,773 Let's put an f here, so we make this a, 23 00:01:44,773 --> 00:01:52,210 we can use string formatting and let's put in our id_options. 24 00:01:52,210 --> 00:01:58,120 And then let's also ask the user for the ID. 25 00:02:01,532 --> 00:02:04,500 Okay, let's save and let's run this in the console. 26 00:02:04,500 --> 00:02:11,305 All right, so we want 3 cuz now we're on searching for a book. 27 00:02:11,305 --> 00:02:15,755 We see all of our options and let's see what happens if I put in 12. 28 00:02:15,755 --> 00:02:17,190 It just keeps going. 29 00:02:17,190 --> 00:02:20,760 So we're going to need to check for errors with our ID. 30 00:02:20,760 --> 00:02:25,690 And we've done a similar thing with our clean price and clean date. 31 00:02:25,690 --> 00:02:30,594 So we're gonna do a very similar thing here to clean the ID. 32 00:02:30,594 --> 00:02:34,234 Let's save this as their id_choice. 33 00:02:36,347 --> 00:02:39,219 And then above it, 34 00:02:39,219 --> 00:02:44,611 I need to do id_error equals true. 35 00:02:44,611 --> 00:02:49,661 And then while id_error and let's tab 36 00:02:49,661 --> 00:02:55,798 all of this over, so we're gonna ask for that. 37 00:02:55,798 --> 00:03:00,808 And then do id_choice equals clean_id, 38 00:03:00,808 --> 00:03:04,426 which doesn't exist yet, but 39 00:03:04,426 --> 00:03:08,752 we are gonna make it in just a second. 40 00:03:08,752 --> 00:03:12,559 And then we're going to check if 41 00:03:12,559 --> 00:03:17,636 the type(id_choice) equals an integer 42 00:03:17,636 --> 00:03:23,276 because our IDs are an integer in the database, 43 00:03:23,276 --> 00:03:29,870 and then we can set our id_error equal to False. 44 00:03:29,870 --> 00:03:32,229 Cool, all right, now let's create our function. 45 00:03:32,229 --> 00:03:34,170 So let's scroll up. 46 00:03:34,170 --> 00:03:36,415 Lots of scrolling. 47 00:03:36,415 --> 00:03:38,585 And let's do it after our clean price, so 48 00:03:38,585 --> 00:03:40,950 that all of our cleans are next to each other. 49 00:03:43,140 --> 00:03:49,385 Clean_id and we know we're gonna take an ID string and 50 00:03:49,385 --> 00:03:54,691 inside of our function we need to do a try lock. 51 00:03:54,691 --> 00:03:59,867 And we need to take that, ID string and 52 00:03:59,867 --> 00:04:03,678 we need to turn it into an integer. 53 00:04:03,678 --> 00:04:10,123 So I'm gonna say, book_id equals Int, 54 00:04:10,123 --> 00:04:15,463 oops lowercase i of id_string, and 55 00:04:15,463 --> 00:04:21,739 then we're gonna except a ValueError. 56 00:04:21,739 --> 00:04:24,760 And we're gonna give them an error and just like we did before, 57 00:04:24,760 --> 00:04:26,426 I'm just gonna copy one of these. 58 00:04:29,890 --> 00:04:31,784 Paste here. 59 00:04:31,784 --> 00:04:40,606 This ID ERROR and the, ID should be a number. 60 00:04:44,190 --> 00:04:48,633 And then I'm just gonna say, press Enter to try again. 61 00:04:48,633 --> 00:04:54,317 And then we need to put that return here, so that it returns None. 62 00:04:54,317 --> 00:04:56,820 So our while loop will kick in. 63 00:04:56,820 --> 00:05:02,520 And then inside of here we're going 64 00:05:02,520 --> 00:05:07,460 to then return our book_id and 65 00:05:07,460 --> 00:05:13,925 let's, it already has been called. 66 00:05:13,925 --> 00:05:15,609 Let me just check to make sure. 67 00:05:15,609 --> 00:05:19,676 Let's go back down into three and 68 00:05:19,676 --> 00:05:23,751 we are calling it, yeah, okay? 69 00:05:23,751 --> 00:05:24,540 Just wanted to make sure. 70 00:05:24,540 --> 00:05:28,705 All right, so we wanna do three and okay, 71 00:05:28,705 --> 00:05:34,901 if I type in tree I get the error, it needs to be number, okay? 72 00:05:34,901 --> 00:05:41,090 I hit Enter and let me do 12, and well it took 12. 73 00:05:41,090 --> 00:05:46,057 And we know 12 is not an ID in our database cuz it's not in our ID options. 74 00:05:46,057 --> 00:05:51,396 So we also need to check and make sure that our options are being checked. 75 00:05:51,396 --> 00:05:53,069 So let's pass those in. 76 00:05:53,069 --> 00:05:58,278 Id_options and hit Save and 77 00:05:58,278 --> 00:06:06,751 then we need to accept them up in our function. 78 00:06:10,039 --> 00:06:13,818 I'm just gonna call them options up here, awesome. 79 00:06:13,818 --> 00:06:20,734 Then if the ID can be turned into an integer inside here, 80 00:06:20,734 --> 00:06:26,192 we can then check if book_id is in options. 81 00:06:26,192 --> 00:06:32,134 Then we can return it and if it's not, 82 00:06:32,134 --> 00:06:36,793 then we need to do the return. 83 00:06:36,793 --> 00:06:41,882 That we make sure we get a book with an ID that is in our options list, okay? 84 00:06:41,882 --> 00:06:45,055 Let's test this out in the console. 85 00:06:45,055 --> 00:06:52,600 Cancel and scroll this up, app.py. 86 00:06:52,600 --> 00:06:56,960 Wanna do 3, here are our options. 87 00:06:56,960 --> 00:07:04,390 If I put in 12 now, I get our Id Options again. 88 00:07:04,390 --> 00:07:09,288 So I no longer get an error, but I do get the option to try again. 89 00:07:09,288 --> 00:07:12,088 So let's give them an error, that way they know what they did wrong. 90 00:07:13,527 --> 00:07:16,889 So inside of here, I'm actually, 91 00:07:16,889 --> 00:07:21,159 I'm gonna copy this one and paste it in here. 92 00:07:26,038 --> 00:07:30,750 And then I'm just gonna say, Options and I'm gonna turn this into an f string. 93 00:07:32,876 --> 00:07:36,493 And let's just show them their options. 94 00:07:36,493 --> 00:07:37,976 Save and let's try this again. 95 00:07:37,976 --> 00:07:44,121 Clear python3 app.py. 96 00:07:44,121 --> 00:07:47,100 Let me scroll this up. 97 00:07:47,100 --> 00:07:49,762 Let's do 3. 98 00:07:49,762 --> 00:07:52,885 Let me try 12. 99 00:07:52,885 --> 00:07:55,618 And I get ID ERROR, and it shows me the options and 100 00:07:55,618 --> 00:07:57,495 it says press Enter to try again. 101 00:07:57,495 --> 00:07:58,910 And I get to try again. 102 00:07:58,910 --> 00:08:03,969 Cool, I feel like that works a lot better for the user. 103 00:08:03,969 --> 00:08:07,793 With all that complete, we can now find the correct book in our database and 104 00:08:07,793 --> 00:08:09,830 share it with the user. 105 00:08:09,830 --> 00:08:14,676 So let me pop back down into our app. 106 00:08:14,676 --> 00:08:20,796 And now that we actually have a working ID, my call is the_book. 107 00:08:20,796 --> 00:08:21,800 It's the_book. 108 00:08:21,800 --> 00:08:22,758 We have found it. 109 00:08:22,758 --> 00:08:27,322 We have found the_book and 110 00:08:27,322 --> 00:08:35,263 it will be session.query(Book).filter, 111 00:08:35,263 --> 00:08:43,610 filter, where Book.id equals the id_choice. 112 00:08:43,610 --> 00:08:47,900 And we wanna add dot first, just in case. 113 00:08:47,900 --> 00:08:51,097 It should just return one, but sometimes SQLAlchemy is a little weird. 114 00:08:51,097 --> 00:08:55,073 So we'll add dot first there, just so we don't get an error. 115 00:08:55,073 --> 00:08:59,613 And let's see, print our book to the console. 116 00:09:07,635 --> 00:09:11,015 And let's do the_book.title 117 00:09:13,507 --> 00:09:17,486 By the book.author. 118 00:09:20,825 --> 00:09:25,031 And let's do the Published and 119 00:09:25,031 --> 00:09:32,807 let's choose the book.publish_date and then Price. 120 00:09:35,970 --> 00:09:40,846 And I'm going to add an a currency symbol here. 121 00:09:40,846 --> 00:09:48,199 And let's do the book.price and I'm actually going to divide this by 100, 122 00:09:48,199 --> 00:09:54,138 so that we get back to an actual price and not the price and cents. 123 00:09:54,138 --> 00:10:00,166 That way it'll show us 10.99 instead of 1,099. 124 00:10:00,166 --> 00:10:05,620 So let's save and run the file. 125 00:10:08,170 --> 00:10:11,851 Give us some space here. 126 00:10:11,851 --> 00:10:18,164 And three and let's say a book number two. 127 00:10:18,164 --> 00:10:25,151 And there it is, Mindset by Dr. Carol Dweck, perfect. 128 00:10:25,151 --> 00:10:32,837 Now this is another great opportunity to add a input or a time sleep. 129 00:10:32,837 --> 00:10:37,039 Whatever you wanna kinda pause in the console, so that the user can 130 00:10:37,039 --> 00:10:41,932 actually look at the book before the menu gets automatically printed again. 131 00:10:41,932 --> 00:10:44,838 I'm going to choose doing an input, just so 132 00:10:44,838 --> 00:10:49,795 that the user has the control over the amount of time they wanna spend here. 133 00:10:49,795 --> 00:10:57,189 So I'm just gonna do Press enter to return to the main menu. 134 00:10:57,189 --> 00:11:04,505 Save and then just to show you all that it's working, let's do that again. 135 00:11:04,505 --> 00:11:07,875 Three, book two, and there. 136 00:11:07,875 --> 00:11:09,672 Now, it kinda pauses us here, so 137 00:11:09,672 --> 00:11:13,281 we can actually take in the book before returning to the main menu. 138 00:11:18,990 --> 00:11:23,029 All right, you all look at all of this code. 139 00:11:26,370 --> 00:11:27,641 It's still going. 140 00:11:27,641 --> 00:11:32,190 [LAUGH] Wow, that is amazing. 141 00:11:32,190 --> 00:11:35,437 And we finished another task, so you know what to do. 142 00:11:35,437 --> 00:11:40,318 It's time to add, commit and push these changes up to your repo. 143 00:12:01,737 --> 00:12:04,439 Nice work, you're amazing. 144 00:12:04,439 --> 00:12:05,007 Keep it up.