So we added our first post request were capable of creating a to do.
Now we've got more routes.
We also got a route to serve all the Jews we created.
Now please be aware that you're to do this will reset every time you change and save your code because
that restarts your server and that loses everything that was stored in memory.
And we're only managing our to do this in memory.
That's simply something we have here and the dummy app in a real app you would of course stored is in
a real database feature from there and so on and therefore it would survive.
Nonetheless here we can create a new function get to dos and this will be a request handler here.
So if we don't have to set the types on all the parameters individually and then in there I simply want
to return a response where I returns up Jason data where I have my tattoos and there I just want to
reach out to my to dos array and send that back to the client that already can be all of course you
could tweak this maybe you want to have a different logic maybe you want to sort the to dos first or
anything like that but this in its simplest form already is all we need here.
It returns our tattoos.
So a very simple function in tattoos roots.
We now just need to connect this.
So let's import get to dos from our controller as well and connect it here with this route.
And with that if we now safeties we can see it in action already.
If you go back and we create a new request a GET request to H to the IP slash local host three thousand
two dos slash nothing and we centers but we get back an empty array.
Now why is that.
Well I said that the to dos would be cleared whenever we change the code and the server therefore restarts.
So we should create a new to do now without restarting the server in between now and if we created that
to do and we now get our to dos we see that created to do with the idea 0 9 8 1 1 and so on here as
well.
So that's already working now since we're on it let's also work on Patch and delete and of course feel
free to come up with some logic there on your own in the end.
Now you have all the building blocks you need for that of course will now all do it together although
So here now on the patch request let's add a new controller function export const update to do which
is of type request handler and should hold a function in this format.
We already know and in there the goal is to extract data about the new to do from the incoming request
and then update the to do with that new data.
Now please note that as part of our route here we have a dynamic segment.
We have this I.D. segment.
So that's something we will be able to extract from the U.R.L. a concrete value for this dynamic segment
can be extracted.
So you're on our incoming request we can get it to do I.D. from our request perms dot I.D. dot I.D.
because that's the name I chose here.
If you chose a different name here you have to use a different name there.
Now we don't get type support here.
Right.
Parents is of type any now because typescript can't parse this string and understand what's in there.
So to get better types of support we can actually utilize that the request handler here is a generic
type and we can add angle brackets and basically tell type to which parameters will have we simply set
this to an object.
And there we now have a key value map of all our parents we have in the U R L.
So for example that we have an I.D. parameter which in the end holds a string.
That's something we can add here and now you'll see we get auto completion and Ty Cobb knows that this
will be a string.
So that's better using this generic type.
Now we have the I.D. we also need something from the request body to be precise.
We need the new text from the request body.
So the updated text can be retrieved from request body.
And now again let's use typecasting here to make it clear that there will be text property in the body
and that this will be of type String and let's retrieve the text thereafter and now we can update the
to do in our to dos array up there.
For that let's find the index of the to do we want to update to index by reaching out to deduce and
with find index we can run some logic on every item in the DOS array and there we're looking at average
to do and if to do I.D. is equal to the to ideas we have here.
We know that's easy to do we want to update now if that for some reason should be minus one it means
we didn't find a to do so if that is smaller than zero essentially we know something went wrong.
And in this case I'll throw a new error whereas say could not find to do and that shouldn't trigger
our default error handling middleware here.
Now if we do make it past the safe check we did find that to do and then we can simply update the to
do at this index in our to dos array to be a new to do where we keep the old I.D..
So where we access to dos to do index dot I.D. and we access the old I.D. and stored it but the text
here which I set whips and of course we don't assign it like this but the text here which we set is
now the updated text.
This replaces to to do here and thereafter we can sent back a response with a message of updated for
example and where we then also append the updated to do to the outgoing response and set as equal to
well to dos to do index which now holds the new value.
This is something we could do here in our updating logic of course some dummy code because we're still
working with this in memory to dos array but it shows how we use typescript here and that's the main
thing.
So let's try does update functional for that we can go back to postman and in the end.
Now I want to send patch request because in the roots were waiting for this patch request to this route
and speaking off that we should maybe also connect this route to this controller function first.
So let's go to the roots file an import update to do and establish this connection here and thereafter
we can proceed and send this request.
So now back to postman patches what we want to send and then it's HP local host three thousand slash
to dos and now the idea of the to do which we want to update as part of the you are.
Now since we saved something since we saved some changes our server restarted.
So all to dos will be lost anyways.
So let's first of all recent that post request to create a new to do.
And then here we got the idea of that to do.
Now we can add this idea here to this U.R.L. and then also add a body which should be of type Jason
Ruby's set does new data we want to send to the server.
Now keep in mind in our update controller I am looking for a field named text in the incoming request
body.
So we should again provide this here in the Jason data we're sending finished a course in one month.
This may be our new text and we now centers we see it was updated.
And if I now get all to dos we also see there's still only is one to do but there we simply changed
the text.
So now this is working we're always able to update it to do.
Let's all check what happens if I have an I.D. which does not exist for example by deleting the last
digit here that I get could not find to do in my error response of type 500.
So this all is looking good as a last step.
Let's add a function to delete it to do so in the controllers folder I'll export a constant named delete
to do which is of type request handler.
And again we get our request our response and then the next function here and end here.
Now I went to find the to do and simply remove it.
For that we can actually copy this code where we get the I.D. of the to do and then the index just get
rid of that line where we extract the updated text because for a delete request there will be no request
body but we need the idea of the to do which is part of our route of the U R L and then we find the
index and once we got a index we can go to introduce called a splice method and removed it to do at
the to do index and only that to do.
And thereafter we can send back a response where we say to do delete it just like that.
This could be our deletion logic.
Now it's time to connect this.
So in the routes folder let's import delete to do from controllers to dos and connect it down their
end with this connected if we now save this.
Again we need to add it to do because all tattoos are cleared with every server restart.
So here it is.
Copy that I.D..
And let's now send that delete request.
So a new delete request here.
Aged dippy low closed three thousand slash to do slashed his I.D..
No body needs to be added.
Click Send and it works.
If I send it again we get D could not find error because we just deleted it and if we get all to do
it that should be an empty array and it is.
So this is now working and this is basically a corrupt functionality added with typescript here in this
simple node an express app.
No of course that's a fairly trivial application here but it already shows us some usage of typescript.
It shows us that we write code that should look familiar to you if you work your way through the rest
of this course and that we're using all the features you learn there and justice and you react section
that's all important you can use anything you learn about typescript here as well because it is getting
compiled to regular javascript code in the end which would then serve.