back in our services file let's go ahead
and build out the rest of our methods
here so we're gonna need a function for
updating so I have an update function
like this and then we're gonna need a
function for deleting which I'm going to
call destroy because delete is a
reserved keyword in JavaScript so not to
get confused we'll call ours destroy
sounds a little overdramatic but we are
destroying a record so we're good to go
okay and then just like the others we're
gonna pass in a req here and a res same
thing down here right for the delete
req and res and then in our update
function what we're gonna do here is
we're gonna destructure like we did up
here so I'm actually just gonna pull
this whole thing down so we can get all
of these different variables here and
that we want to do is say hero.findOne so we're gonna find one okay and
then what normally what we do here is
pass in the name of the field and then
the value that we want to find by but in
our case we want to find the ID field
and we want to find it by the ID
parameter which we have right here but
again with destructuring we have
some shorthand this right here is the
same as typing this and this which is
really nice we can just type this but
they're the same thing so we'll do find
one and then that will return a promise
so say then and hopefully we'll have a
hero in that promise and then we're just
gonna have to hit the heroes we're gonna
say the name is equal to the name right
from our d structuring and the saying is
equal to saying from the destructuring
and then finally we'll just call save it
soul save and that gives us a promise
and in that promise
we're just gonna return our hero that we
just updated right just like create
that's what we returned you sent me a
hero I created it I'm sending you back
the same hero that's how you know that
it's been updated and then we'll catch
an error here if we have one and as
usual resident status server error 500
and then sending the air okay excellent
and then we'll reformat looking good all
right let's implement the destroy do a
little bit more do structuring here so
we're just this time we just need ID and
it's actually gonna come off req params
so it's not in the request body it's
actually gonna be part of the URL that's
how we're
implement the destroy function here well
we'll see how that actually works here
in a second and we're gonna call hero.findOne and remove isn't Mongoose
nice some nice and find it by the ID
that's gonna give us a promise which
will containing the hero that we deleted
and we're just gonna send that back as
per usual and as per usual right catch
it
alright so we've implemented our update
and we've implemented our destroy we
just need to add them to the routes
let's come back over to the index file
add in our routes so anytime we do an
update we're gonna use a put verb so the
put http put verb you can see we're using
different verbs here we're using a get a
post now a put alright so we're gonna
put and this will be our update our hero
and then function req and res and next
and all that good stuff and then down
below that hero service and then dot
update and in true burke fashion I
forgot to export from the hero service
yet again for the millionth time but at
least I caught it this time before we
actually ran it to the error so we'll
put both of those there and then lastly
router dot delete which is also an HTTP
verb and hero and remember I said we're
gonna pass on the parameter a little bit
differently it's actually any part of
the of the route here and this is just a
restful api that we're creating here
this is a common pattern for deletes on
restful api so
there we go and hero service dot delete
and destroy rather and now it's there we
can pass in req and res and we're all
done ok let's fire up the server
and we're up and running and let's go
over postman and see if our our new
routes and our new API methods are
working all right here in postman what
we want to do is want to change this to
a put and over here in our params here
let's see here
scroll up we're going to just change
this right so let's update the tick to
be Batman so we know that we've
definitely made a change and I'm Batman
or even better I could say this all the
time they say where's the bomb okay now
we can go ahead and send this and see
whether or not we've actually created a
successful put method here in update so
this is our update sending and the
response we got back right the ID the
name and the saying again we can go back
to our application here and we can just
refresh to make sure that we're actually
changing the data and we did right so
this actually pulls from the database so
now we're pulling in a new hero here and
let's make sure that delete works so
let's do it like this and delete and you
can see that that's all we need to
provide because the ID of the here that
we're gonna delete is up here except for
our ID is zero it's not one so let's
change this to zero raise started to it
right and then we're gonna change this
to a delete over here on this side
send look at the response we got back is
the hero that we wanted to delete so we
know that it's been deleted go back to
the application refresh alright perfect
so now we have create read update and
delete full crud all ready to tie this
through to the front end of our
application we're going to do that next
