I'm gonna create a new file let's go ahead and save it we'll call it hero
hero-service.js we'll stick it in the server folder good and then let's go
ahead and bring in our hero model which was
there we go and then I'm gonna bring in read preference as well which comes off
of the MongoDB package so we can say MongoDB and then read preference now the
reason why we're bringing in read preferences is we're gonna actually use
that here in a minute to optimize our query and we'll talk about that when we
get there and then we want to require in our Mongo file right which contains
contains our connection function and then just call connect and that connects
us to the cosmos DB instance now let's create a function which will get all of
our heroes out of the database we'll call it get and we'll just pass in the
request and the response from the route so instead of handling the request and
response on the route we're just gonna send them straight to the service which
is going to handle them and the first thing we're gonna do is we're gonna
define a new query so we'll call it doc query and we'll just say hero dot find
and then find as part of that nice Mongoose API we're not gonna pass in any
parameters to reach find everything and read preference nearest and what this
does is it set it just says simply tells MongoDB read from the nearest database
because cosmos DB is a globally scaled and replicatable database this one just
finds the nearest instance and reads from it makes it fast that's what we
want and then the next thing that we want to do is actually execute this
query so we say query.exec and that's going to return a promise which
will have our heroes in it which we then return as JSON and say that we're gonna
return our heroes and if we have any errors we'll catch those and let's
actually tell you what let's bring this catch down to the next line so it's
easier to read and we'll send an error and if we get one of those then we'll
just first thing we'll do set our service status to 500 which is a server
error and then we will send back the error and then I'm gonna format all this so it
looks nice and pretty there we go that's a lot more readable okay now that we've
got this in here we need to actually be able to call this from our route so
let's go back to our routes file which is index.js so let's take all of this out here we
don't need this anymore and then let's bring in our hero service
And that was from we're gonna have to go up one level and then hero service that should
be where it's at and then we should be able to say hero service dot and there
should be a get and then we're just gonna pass and the request in the
response because we're gonna let the service handle that if you remember
excellent okay so let's start the server if we don't have any errors it should
fire up just excellent and let's go over to our browser and if you recall the
server is running on port 3001 so if we come over here and say API/heroes
we should get back an empty array but we don't we get an error and that's because
we did not export so let's go back to our hero service this is another really
simple mistake that I make all the time I just don't export anything from my
modules so module dot exports equals and then we're just gonna export get for now
and then we'll restart the server head back over to the browser give it a
refresh and now we're getting an empty array so that's coming back I'm gonna
this as big as you can get in an array in chrome but there's no data in there
so that's what we get for now cool so let's define a way for us to put some
data in so that we can get it back out so we'll go back to our hero service and
go to hero service and now we just need to add in a create function so we'll say
create and again we'll pass on the request in the response and then we want
to get the ID the name and the saying from the body of the post request that's
going to come in and we can do that just by saying req body now this is called
destructuring it's new in JavaScript and it basically it's where JavaScript just
looks at req body and if it has an ID a name and a saying in it then it assigns
those values and do variables called ID name and saying so then we can use
them right so I can just use the variable name because it now exists
because it's been destructured from req body that's called these structuring
it's shorthand it's easier than us and manually mapping things
okay so let's create a here a new hero and we'll give it the ID the name and
the saying that came in from the mapping we'll say hero dot save that's gonna
return a promise and then what we'll do is we'll just send back we'll say res
dot JSON the new hero so we're just gonna send back the same thing that was sent
to us kind of like a act did you get it yes I got it here it is back and then
we'll do a catch as usual as per usual with an error which we will do res dot
status is 500 and then we'll send back the air as well there we go
okay we're ready to add this to our route so we can have a route for
creating a new hero so let's go back to our index file and we'll do router dot
and this is gonna be a post okay so it's gonna be a post and then we're not going
to create multiple heroes just one so we'll call the path hero and then that's
gonna give us a req and a res and a next the same as the get and what we'll
do is just a hero service alright dot create because that was the method that
we created and then we're just gonna pass in rec and res there we go and I
did make one mistake here we're not using fat arrows just functions okay
excellent there we are so we've now got a method for creating a hero let's go
ahead and restart the server and let's see if we can get a hero into our
database and now to do that we're gonna use postman postman is a free tool for
testing api's and it's pretty fabulous because when you're building api's you
don't really have a way to test them until you build the user interface
portion of your application which we've done but we haven't wired that up yet so
we can test our API with postman so let's do a new post request and then
we're gonna do 300/API/ hero and then we need to in the body send in
the new hero and I'm gonna send in some raw text here and we'll do an ID is 0
will do the name which was the tick and the saying which was spoon somebody
needs to tell me why that is the tick saying maybe I can just use Google ok
let's change this to JSON because that's what Express is gonna be
expecting us to send in so that's what we'll send it and then let's just click
send and scroll down and look at our body and as usual I did not explore the
create function I told you I do that a lot so let's go back here to our hero
service get and create save that restart the server to try this again in postman
send okay and we got back the hero and noticed that it has the ID that we sent
in and it's got a new ID as well which came from cosmos DB so we know that it
worked and just in case we're in question about that let's go back to our
application refresh of here and now the API is sending back the
tick and if we go back to our react app you can see we're still getting the tick
awesome ok so we're connected to cosmos DB we can read data and we can create
data and in the next video what we're gonna do is we're gonna update and
delete data and test them both of those methods and then we're going to tie all
of that back to the UI I'll see you then