Welcome back.
Let's work on the image endpoint here.
And given what we've done so far, we can pretty much be sure that we won't be needing any of this.
We're going to connect it to the database.
Now the image endpoint, if you remember, updates the entries and it increases the count.
So all we have to do is update and let's look at KNEX and see if they have the update function
Yep they do.
And they give us good examples of how to do it.
So let's use this as an example.
To create our update function
we'll just copy it here.
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
this is SQL where the ID equals the ID that we received in the body that we want to update the entries
to be higher than the entries that is currently there.
But how can we do that.
How can we grab the entries from the database and increase it.
Again this is a problem that's very common and is said of selecting the ID grabbing whatever the entries
is and updating it.
Well we can simply use something called increment and increment just tells us the column and the amount
we want to incremented by. Very cool.
Let's do it.
Let's say that user ID instead of update was just to increment and we'll do entries and we just want
it by one.
And the default is one I'm pretty sure but I always like being explicit so that people know what's going
on when they read the code and let's do that.
So we increment by one and then we want to say returning the entries let's do a dot then and see what
we get
console dot log
entries and let's save. Go back to postman. Let's find our image endpoint that has a body of ID and
we'll have to give it an ID that actually exists for now.
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.
Remember we changed it to put we send again and it's hanging because we haven't given it a response
so we'll just cancel the request for now.
But if we go back to the server Well we can see that there's a bit of an error here and we definitely
made a silly error here.
It has no idea what database we're talking about.
We need to actually say users here. The SQL errors are sometimes can be really confusing.
But like I said it is pretty declarative.
So as long as you can read it and understand it with KNEX errors are to a minimum unless you're like
me and you forget to actually mention the table.
Let's try that again.
Sent.
We're going to counsel the requests because there's no response.
But now I have one return.
If I send it again cancel the request.
I have two. If I go back to the database and I select all I see that Ann now has entries of two. Amazing.
So we can go back here
now, knowing that this works. Let's say that entries, we want to - again, return the first array, because we
get one and in stead of console logging we'll say response dot json entries.
But if something fails then we will do a dot catch just like we failed over here and we can say that
error response dot status we'll give it another 400 we'll say Json unable to get count or entries
We will save that and I'm going to actually remove this to make sure that the error is working.
So it should return me a 400.
Let's try that again.
Send. Unable to get entries. 400 request.
But if I fix this to users and select Ann again, I get three.
All right we got our image endpoint working and we have our final endpoint to work on which is sign in
that's for the next video.
I'll see you on that one. Bye-Bye.