Hey there, everyone.
They sure and welcome to the video.
And we are trying to write swagger documentation.
Now, so far in the course we have made only the gate request.
This is also going to be a get request, but this is going to be a bit of a different type.
Now in this one, we want to pass on something to the database or you can say backend in this case.
And based on that particular value, we will be getting some result.
Now there are a variety of ways of how you can pass on some information to the backend.
It can go through the URL or also known as parameters or also known as path, all these names for the
single thing.
You can also pass on some information via a proper parameters like forms and requests, body and all
of that.
So we're going to start with the very basic one, which is passing information in the route itself,
also known as PATH.
So let's first go ahead and open up index for us and go ahead and write something for it.
So we're going to simply go ahead.
We can actually copy this one.
So I'm going to go ahead and bring all of this.
There we go.
Okay.
So how this is going to work, so this is going to be called as course or they would rather like to
call it as my course, feel free to name it anything, whatever you like.
And then we expect that some information will come up into the URL.
The way how we do it is put up a colon and then we can simply mention this as course ID now you can
name it course id id however you like.
This is how simply we do it.
Now let's go ahead and remove this one.
Now what we're going to do is we're going to look into our array will match if there is any course ID
related to this.
If it matches, we're going to return it back.
Really simple.
So all we got to do is simply take my course out of it.
Cost.
Not course.
There we go.
Come on.
There is a lot of mistake here.
There we go.
Okay, so how are we going to find this one out?
First, we need to look out for our courses array and in that we can simply use a find method.
Definitely you can go ahead and use your maps and filter and anything else that you are comfortable
with in this case, I think find makes a lot of sense.
It's really easy one.
So find again loops through with all the values.
So let's call this each value as course.
And once you go ahead and loop through it, then all you got to do is this course, individual course,
which is an object, has a property of ID if it matches with the ID that is being provided by the URL.
And you cannot just go ahead and write course ID like this because this is not how you access the URL
in the express.
In this case, what we got to do is we have to say request dot params dot cause id.
There we go.
Now in this case, remember, the course ID is exactly coming up from the URL and in order to access
it from the URL, you have to say request params.
Now this will obviously fill up the my course available and all we got to do after that is rez dot send
and we are going to send this my course.
There we go.
So this is pretty easy in case you want to test out, you can use Thunder client or any other similar
situation like this.
So let's go ahead and try to use this and let's go ahead and pass on this to two.
So let's open up the tender client.
I want to make a new request, which is going to be.
HTTP, colon, slash, slash.
And by the way, this is a simple add on.
You can just go ahead and install this extension look or you can use postman also.
So local host Colon 4000.
I know this is like really small.
Yeah.
This is now much more visible.
4000 slash and there we go.
I accidentally added two slashes, so let's go ahead and remove that.
There we go.
Make sure you are also cautious about this.
And once you do this, after that, we're going to go ahead and simply use two two for the course ID.
Let's go ahead and send this.
And this gives me the course which is coming back.
As with the ID, two, two, I can go ahead and write three, three and let's send this up and it gives
me the next course.
So we get the idea of our API is working fine and this is two too much zoomed in.
There we go.
Okay.
Now, once we are done with this, we obviously need to write documentation for this particular route.
So let's go ahead and address this one.
This is slash my core slash course ID going up into the swagger.
We are going to copy this route.
So let's go ahead and select all of this, make a copy, and now let's change this one.
So this one is actually my.
Of course.
And once you have this course, then we are taking something in the parameter.
So this is how we represent it.
So course ID goes like this.
Now this is a get tag.
We can combine it with array.
Let's just say it's not really ideal, but let's go ahead and do this.
It is not going to return all the courses.
It is going to return course based on request ID.
That's what it does.
Now.
Also, so far we have only talked about responses, but this time we need to talk about how we can send
some information.
Usually most of the information is sent through parameters.
If I can write that para meters.
There we go.
Okay, so how do we do this?
Parameters can be done and being passed can be passed on through variety of things.
First, let's name it.
Name is not at all compulsory, but I still like to give it.
I'll show you where this name comes up in a minute.
So once we have this name, the most important part is in now where your information is going to be
passing through.
Is it going to be in the query?
Is it going to be in the path?
Yes, we call you out as spot and there are a couple of other places that we eventually are going to
explore.
And is it required?
Yes, this is very much required.
So we're going to mark the required flag as true.
And we will also say that there are some default values.
If the user doesn't pass on anything or write anything, 22 is going to be default one.
And then we are also going to say, what schema is it going to return?
The schema is definitely an object that we are coming up for that.
So let's go ahead and say that the schema that is coming up is going to be an object.
You can write string as well, but this is what we are writing and once we write the object, you already
know that we need to type all of this.
So let's go ahead and grab all of this, copy that.
And we are going to go ahead and paste this one.
And again, make sure the type and property are on the same indentation, just like we have done it
here.
We are going to receive an ID name and price and this is too much indented.
So let's go ahead and shift them back.
There we go.
Looking nice.
Now coming up onto the responses.
My bad.
This is not like this.
Sorry, this is my bad.
We are not having property.
This is the scheme of what you are sending and this is not an object.
This is actually an a string to just God that could have been done a little bit wrong there.
But hey, thankfully notice on time response is going to be all good success.
The schema that we are turning is not an array, this is an object and we don't have items this time.
So let's go ahead and remove this.
We also don't need this one.
So we're going to go ahead and remove this one.
This is going to have the properties just like we saw about these properties, but it needs to be indented
properly.
So let's go ahead and move this up here.
This can also be indented a little bit better.
Again, these documentation may have some errors, but you've got to fix them up.
Let's go ahead and hit a reload.
And we see that area is now having two of these ones.
So let's go ahead and mix it up with the string so that it's much more easier to actually see.
So instead of array, we're going to go ahead and say, hey, you go ahead and get mixed with strings.
Let's go ahead, go back, hit to reload.
And there we go.
Now it's mixed up with the string, so easy with the tags.
Now let's analyze this a little bit.
So first, the default values in all are all good.
Remember we gave it a name?
Of course ID this is exactly coming up from here.
So the parameter when you say course ID, this is exactly coming up here in the name section.
It is a string, it is a default, value is 22.
And notice here it says marked as required.
So yes, this is required.
The default value is 22.
Right now it's giving me this icon that you are not allowed.
It's because I didn't hit the try it out.
As soon as you hit the try it out then you are allowed to enter any values and all good success.
We have already discussed quite in detail on that.
So let's go ahead and hit execute and there we go.
As soon as you execute.
Now notice it says my course 22.
So we are returning all the bad stuff up here.
So this is something wrong.
Let's go ahead and see that how this is going on.
Yep, I missed a slash.
So see, errors are a part of the development.
Let's go ahead and hit a reload.
Let's see what is going on wrong and let's try it out one more time.
Execute this and this time we are getting a proper course ID back.
Let's go ahead and try it one more time with one one execute that and we are getting react awesome looks
great.
Looks fine.
So now that you know that how you can handle the get request but you can also you have also the information
about how we can send parameters or we can send some information to the back end and can receive some
data back.
So parameters and responses are the new thing that we have learned in this video.
Let's go ahead and catch up in the next video.