1 00:00:01,300 --> 00:00:02,240 Welcome back. 2 00:00:02,469 --> 00:00:04,980 Let's work on the image endpoint here. 3 00:00:05,230 --> 00:00:11,840 And given what we've done so far, we can pretty much be sure that we won't be needing any of this. 4 00:00:11,860 --> 00:00:14,840 We're going to connect it to the database. 5 00:00:14,950 --> 00:00:20,710 Now the image endpoint, if you remember, updates the entries and it increases the count. 6 00:00:20,710 --> 00:00:28,560 So all we have to do is update and let's look at KNEX and see if they have the update function 7 00:00:32,790 --> 00:00:33,970 Yep they do. 8 00:00:33,990 --> 00:00:37,540 And they give us good examples of how to do it. 9 00:00:39,760 --> 00:00:42,170 So let's use this as an example. 10 00:00:43,030 --> 00:00:45,220 To create our update function 11 00:00:48,310 --> 00:00:49,690 we'll just copy it here. 12 00:00:52,530 --> 00:01:04,739 And we know that with our DB dot where we want to say that where the ID equals and it's not 3 equals because 13 00:01:04,739 --> 00:01:14,740 this is SQL where the ID equals the ID that we received in the body that we want to update the entries 14 00:01:16,090 --> 00:01:21,100 to be higher than the entries that is currently there. 15 00:01:21,140 --> 00:01:22,990 But how can we do that. 16 00:01:23,160 --> 00:01:26,660 How can we grab the entries from the database and increase it. 17 00:01:26,660 --> 00:01:34,230 Again this is a problem that's very common and is said of selecting the ID grabbing whatever the entries 18 00:01:34,230 --> 00:01:35,890 is and updating it. 19 00:01:36,300 --> 00:01:46,080 Well we can simply use something called increment and increment just tells us the column and the amount 20 00:01:46,080 --> 00:01:49,110 we want to incremented by. Very cool. 21 00:01:49,230 --> 00:01:49,870 Let's do it. 22 00:01:50,160 --> 00:02:00,200 Let's say that user ID instead of update was just to increment and we'll do entries and we just want 23 00:02:00,200 --> 00:02:01,140 it by one. 24 00:02:01,450 --> 00:02:06,280 And the default is one I'm pretty sure but I always like being explicit so that people know what's going 25 00:02:06,280 --> 00:02:09,020 on when they read the code and let's do that. 26 00:02:09,430 --> 00:02:22,110 So we increment by one and then we want to say returning the entries let's do a dot then and see what 27 00:02:22,110 --> 00:02:22,470 we get 28 00:02:29,450 --> 00:02:30,530 console dot log 29 00:02:33,340 --> 00:02:44,790 entries and let's save. Go back to postman. Let's find our image endpoint that has a body of ID and 30 00:02:44,790 --> 00:02:47,940 we'll have to give it an ID that actually exists for now. 31 00:02:47,950 --> 00:02:59,840 So let's just do one send. And going back here we don't get anything because well we need to do a put request. 32 00:02:59,850 --> 00:03:07,380 Remember we changed it to put we send again and it's hanging because we haven't given it a response 33 00:03:07,380 --> 00:03:09,270 so we'll just cancel the request for now. 34 00:03:09,410 --> 00:03:16,610 But if we go back to the server Well we can see that there's a bit of an error here and we definitely 35 00:03:16,610 --> 00:03:18,600 made a silly error here. 36 00:03:18,620 --> 00:03:22,370 It has no idea what database we're talking about. 37 00:03:22,370 --> 00:03:29,310 We need to actually say users here. The SQL errors are sometimes can be really confusing. 38 00:03:29,380 --> 00:03:33,720 But like I said it is pretty declarative. 39 00:03:33,720 --> 00:03:40,340 So as long as you can read it and understand it with KNEX errors are to a minimum unless you're like 40 00:03:40,340 --> 00:03:43,070 me and you forget to actually mention the table. 41 00:03:43,070 --> 00:03:43,970 Let's try that again. 42 00:03:44,930 --> 00:03:45,600 Sent. 43 00:03:45,720 --> 00:03:48,730 We're going to counsel the requests because there's no response. 44 00:03:49,070 --> 00:03:52,440 But now I have one return. 45 00:03:52,580 --> 00:03:56,300 If I send it again cancel the request. 46 00:03:56,370 --> 00:04:05,220 I have two. If I go back to the database and I select all I see that Ann now has entries of two. Amazing. 47 00:04:07,740 --> 00:04:08,630 So we can go back here 48 00:04:08,640 --> 00:04:15,570 now, knowing that this works. Let's say that entries, we want to - again, return the first array, because we 49 00:04:15,570 --> 00:04:22,670 get one and in stead of console logging we'll say response dot json entries. 50 00:04:22,990 --> 00:04:32,170 But if something fails then we will do a dot catch just like we failed over here and we can say that 51 00:04:32,350 --> 00:04:45,820 error response dot status we'll give it another 400 we'll say Json unable to get count or entries 52 00:04:48,180 --> 00:04:55,260 We will save that and I'm going to actually remove this to make sure that the error is working. 53 00:04:55,260 --> 00:04:58,430 So it should return me a 400. 54 00:04:58,560 --> 00:05:00,030 Let's try that again. 55 00:05:00,200 --> 00:05:04,590 Send. Unable to get entries. 400 request. 56 00:05:04,840 --> 00:05:16,310 But if I fix this to users and select Ann again, I get three. 57 00:05:16,390 --> 00:05:22,660 All right we got our image endpoint working and we have our final endpoint to work on which is sign in 58 00:05:24,340 --> 00:05:25,690 that's for the next video. 59 00:05:25,960 --> 00:05:26,800 I'll see you on that one. Bye-Bye.