Let's Code: TO-DO App API with Nest.js and MongoDB | Stefan Omerovic | Skillshare

Playback Speed

  • 0.5x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 2x

Let's Code: TO-DO App API with Nest.js and MongoDB

teacher avatar Stefan Omerovic, Full Stack Web Developer and Instructor

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

29 Lessons (2h 17m)
    • 1. Introduction

    • 2. What is Nest?

    • 3. Install Nest and Create a First Project

    • 4. Going Through Nest Project

    • 5. Setting Todos Module with Controller and Service

    • 6. Life Cycle of Todos Module

    • 7. Creating Data for Todos

    • 8. Returning Todos Data from Service

    • 9. Using Service in Controller and Getting Todos

    • 10. Creating a Get Route to Get Specific Todo

    • 11. Finding Specific Todo in Service and Returning it Back

    • 12. Setup Postman

    • 13. Creating Get Requests

    • 14. Create Todo

    • 15. Delete Todo

    • 16. Update Todo

    • 17. Creating a Model for Todos

    • 18. Talking About MongoDB and Creating a Cluster

    • 19. Installing Mongoose

    • 20. Connecting DB to an App

    • 21. Creating Schema for Todo

    • 22. Using Schema and Mongoose Model in Service

    • 23. Creating Todo with Mongoose

    • 24. Getting all Todos from Database

    • 25. Get Specific Todo from Database

    • 26. Delete Todo from Database

    • 27. Update Todo in Database

    • 28. Test the Whole API

    • 29. Thank You and Goodbye

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.





About This Class

Through this Let's Code mini-course, we will build an entire TO-DO App API using Nest.js and MongoDB Technology. You will be able to access the entire API, save and modify your data and also pull data from your database at any point.

This course belongs to a Let's Code series and for you who don't know, the Let's code series is a series on SkillShare where we together create real-life projects using a variety of different technologies.

What will you learn:

  • Nest.js ( What it is, How it Works, Lifecycle of it, Modules, Creating Routes, Using Controllers, Using Services, Installing Third-Party Modules, and much more )
  • MongoDB ( Creating Private DB, Creating a Cluster, Connecting DB to an App, Working with Data, Handling Privacy and more )
  • Mongoose ( Installing Module, Importing it, Connecting DB to an App with It, Creating Schema, Using Mongoose Model, Saving Data, Modifying Data, Getting Data and more )
  • Postman ( Installation Process, Creating Collection, Creating Requests, Editing Requests, Sending Parameters for Requests, Sending Body for Requests, and more )
  • Promise Pattern ( Using Async / Await, Returning Promise Data, Handling Asynchronous Code )
  • Typing ( Using Proper Types for Data, Using Specific Model for Data, Returning Promise of Data )

By learning all of this and building your own TO-DO App API using Nest.js and MongoDB you will be one step closer to understanding Back-End Technologies and getting your first job as a Back-End Developer.

Meet Your Teacher

Teacher Profile Image

Stefan Omerovic

Full Stack Web Developer and Instructor


Started at age of 18, at the usual starting point for Front End. For the first 2 years mostly coded static websites with Front End Frameworks like Bootstrap. After that, explored the next point on my path to becoming a Full Stack Developer and entered into Web App Development. Started openly coding in Angular and handling everything regarding the Front End Side of multiple projects. Currently, I am a MEAN Stack Developer with 7 years of experience.

Started my way as a Self-Taught Developer, I never stopped learning new Web Development Technologies and I plan to learn and become even better with those. Through a few more years I plan to move into Mobile Development with Flutter.

Experience as Instructor

The reason why I got into teaching Web Development was... See full profile

Class Ratings

Expectations Met?
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%
Reviews Archive

In October 2018, we updated our review system to improve the way we collect feedback. Below are the reviews written before that update.

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.


1. Introduction: Hello and welcome to another course in Mallat's go and series. In this course, you will create the to-do application with Nest Jess and Mongo database. My name is Stefan, no matter which, and I'm a self-taught web developer, but also an online instructor. The entire course should be quite straightforward. From this course, you should learn what these nest, how to use nest as a technology. You will also learn how to use nested routes, how lifecycle of networks, and also handle functionality for those routes. We will also use a Mongo database, which means that you will learn how to save data into that database, gets specific data, get all of your data, and also delete, end, modify your data as well. So by the end of the entire course, you should have fewer, more skills at your disposal. So now for who this course really is. Well, this is a course for anyone that wishes to start their own backend the journey. Also, this course uses a nest jazz technology, which is quite similar to Angular technology. So all of the England developers should easily follow up with these scores. Next to that, if you are already experienced with back-end technologies like cannot JS or extra stress. Just JavaScript in overall, this is a course definitely for you. Nest JS is a technology that uses a TypeScript. And the TypeScript is becoming a more popular by the day. So learning it would be a big plus for your career. So about the requirements as usual, I will drive this course as simply as possible. So you should be able to follow up without any problems. There is no need for requirements for this course, but it is highly suggested to know at least basics of backup or to be experienced back-end developer. Also, once again, if you are Angular developer learning nest just technology could be one of the best things that you could do these days in that way, become a full stack developer. Now about the select Xcode series this course belongs to. Let's go to series. And there Let's go and series is a series on Skillshare, where we together develop a real life projects with variety of different technologies. Next to this course, on my profile, you will find angle our courses, multiple JavaScript courses in Bootstrap courses as well. So if you wish to learn those technologies as well, make sure to watch those courses and build real life projects with me. Now that you know what you can expect from this course, Let's talk about the specifics. 2. What is Nest?: So before we start working on our to-do application, let's learn more about the nest JS. Js is a progressive framework for building efficient, reliable, and scalable server-side applications. At its core, nest JS will use TypeScript, but it will also enable developers to code in pure JavaScript if developers wish to also nest itself combines elements of object-oriented programming, functional programming, and also functional reactive programming under the hood and nest JS will make you to use the robust HTTP server frameworks like experts JS, but it can also optionally be configured to use for testify. Now also to talk a bit about the philosophy of nest in recent years, not JSON JavaScript became one of the leading technologies in web development. Well, in whole web industry. With that came the rise of awesome frameworks like Angular reactant view. Those frameworks are all on front-end development, but nest as a framework was built for an auto DJ us. So it is used on server-side, well, Can side, as it was built for Node.js, it extensively by default uses experts diaspora as well. The theme behind the nest JS liked Engler as a framework, so they apply the best practices from Angular JS. This means that the whole architecture of the framework is very similar to Angular. It will also use similar naming. It will extensively use classes and injectables as well. In ludus as well, Angular supports decorators, custom types, guard interceptors and so on. And nest as well will support those in a similar way. One of the crucial features integrated in Angular is RhD, as in. Next, we'll support our XDS as well. So yes, we can say that nest JS as a framework, even though it is on a server side, it is very similar to angular framework. That is why Angular developers can more easily learn nest JS and become full stack developers. And now to the next step of this course, the next step of this course will be to install nest jazz and also to create our own first project. 3. Install Nest and Create a First Project: Toys, towel nest, jazz. We will have to do something before that. We will need to install NodeJS in our local machine to do such a thing, going to Node.js official website with Jews, another js.org. And on that you will have these recommended version to download. So just click on that. Once the version is downloaded, make sure to run that installation for Node.js should be quite straightforward as it is just next, next, next and finish. So next year I will accept the terms in the license agreement. Next again, and we will select where do we wish to save Node.js. So I will select this path as it is defaulted. I will again go next here. And here you can choose some specifics related to installation of NodeJS, one of those z's and PM or not Package Manager. I will talk a bit more about this soon. So just click Next here and you will be prompted to his towel necessary tools like Chaco latte, soy. It is up to you if you wish to establish such a thing, just tick this box, but I don't wish to install it, so I will just click next year as well and run the installation. Once distillation is finished, just click this well, simple finished Finish button. Now, if you wish to check, if not it was properly installed, go to your terminal. In your terminal, use NADH and then dash v. This dash v is a flag for a version. So by this command, we will check the version of NADH, hit enter, and we will see that the current version was indeed the style next to its own installation, not JS Would these Tao also these npm or Node Package Manager. Node package manager is also quite important for us here because we did it. We will be able to install and use different packages like exponential JS, Not mon, and so on to check if the NPN was installed, use npm dash V again. So by that we can check that we do have installed our 6.14.15 version, which is the latest one for NPM. Now you can go to nested J S official website, which is Nest js.com. There you can click on this documentation button. We are ready now to install nest JS. And down below you will see the whole installation. And how can you do that? So first of all, you can see that first command that we should use is this npm command. So already we are using these npm. We will use it to style and also globally, this g flag stands for globally. So we will install this package globally on our machine. And the package itself is called Nest JS select CLI. So let's just copy this command and go into our terminal. In terminal itself, we can just paste this command and hit Enter this. Now we'll install nest J as well, the CLI of it on our machine. Once this is finished, we can confirm that indeed we added to our local machine nest js slash CLI, where its latest version now go back to nest JS documentation. And the next command is a command to generate our project simply here you can see that now we can use nest because we installed globally and we can pass a command of new to generate the new project and the project name. So to do that now, I will lose nest knew and my project name will be Nest to-do app. Now simply we can hit the Enter once again. These will generate our project. Before it generates our project, it will also ask us couple of questions. First question is, which package manager would we like to use? We have an option to use npm Yarn and B and PM. I will go just with the NPN because we already installed it. And I guess that you are also mostly experienced with NPM. So hit enter here and the installation will be in progress. So just wait a bit. Now that our project was properly installed, we can see these two commands that we can use. First command is CD ness to-do app. With this, we will be moved from our current folder into the application folder. So let's use CD nest to-do app. And now once we are in our application folder, they do have an option to use npm run start. This command will around our local server for our application. So npm run, start and hit Enter and nest JS will now start our local server. Once you have these green messages, those are well basically logs from a nest JS for example, it is starting the next application. It is initializing get module, app controller. It is also checking for the routes which is one gets routed by default, our projects should hold one get route. And also last one is that the nest application was successfully started to check your nest application going into your browser on localhost 3 thousand. There you should have a message of hello world. If I zoom in, you would see that message a bit more clearly. So now that you saw how to install the nest and you also created your first project in place. We should go through the nest JS architecture and see what the project, while default project of Nest JS will hold. 4. Going Through Nest Project: Now once we generate that our Nestor JS project, and let's see what will hold these nest JS project by default. So for that, open your project in your preferred code editor. I opened just mine in Visual Studio Code. And the first thing that we should point here is this SRC folder or source folder. This source folder, we'll hold all of our nest JS files. So basically, all of the file is where we will code, will be at the end in this source folder. Next to this SRC folder, we can see these node modules. All of the node modules that should be installed in that are currently style to just around these nest project should be in this node modules folder. This node modules folder is rarely or subtract with the Git version control because it will include a lot of files. So we wish to nap track those files that not to commit those files into the GitHub for example, next to this node modules we can see this dist folder. This folder is a folder for distribution. So once we are ready, we can build our Nas suggests project, which will compile all of our TypeScript files into JavaScript files. And it will put all of those compiled files into this folder. And this folder at the end will be the folder that will be deployed onto our server after this dist folder at the bottom here we do have also these tests folder, test folder, as its name says, is used mostly for testing and it is mostly used just for end-to-end tests. But next, all of these folders, we do have the package.json file, which is the file that will hold our scripts and dependencies that we installed. For example, these nest JS dependencies because we are working with the nice JS project, so we need those modules to be installed. Also next to that, we do have these adage day as dependency. As you know, I already mentioned that the average day as similarly as it is in Angular, we'll be also installed by default in Nest project because those two technologies are quite similar. Now closing the spec a JSON, we can also see these nests CLI j. So this is just the configuration file for our nest CLI. So any specifics that you wish to change regarding the nest CLI would go into this JSON file. Now moving ourselves into this SRC folder, we first have these main that the file, this main TypeScript file, will be our main file for the whole application. So for example, this file, we'll import this nest factory, which will build the entire application. It we will have its own main module, which is apt module by default. And we can see the function called bootstrap, which are at the end. We'll be called on a line aid, the bootstrap function, we'll call the next factory and it will create the new application based on the app module. So every file that is used throughout our application will, well should be in our app module at the end, which will be passed to a creation of our application of nest. And at the end we can see that app dot lease and we'll listen to a server on port 3000 by default. And if you're still running your solver, you can go to your local host, 3 thousand and you will see Hello World message there. Now let's go into this app module file. On the left side here you can see app.use module.js file. And this is the main file for our application, main JS file. We'll create our application, but all of the files and configurations of our project will be in this app module file. So here we can see all of our imports, controllers, and providers that we have for this application by default. Next, we'll create two of those, one controller and one service for us. And at the end, it will just export the whole app module. So we could import it into main types, could file and their use it to build our next application. Now let's take a look into this controller file. This controller file is mostly when we write our routes. So all of those routes get post, patch, delete and so on should be in this file. So here we can see that we do have the decorator of controller, which will decorate the entire file, file down below to be used as a controller. We are also passing the app controller here has a name here because this is just the by default created by Nest JS. And at the end it is just the class. So inside we can have some kind of constructor, which we do have here. And to this constructor, we are also passing our App Service, which is injectable, which you will see soon. Down below, we are creating our first GET method, and this is again by default generated from Nest CLI. There we do here one method get hello, which should return a summary response of a string. Here we don't have any string, but we are returning whatever we have on our App Service, get hello metal. So now going into my app service here, we can see that these decorated by this injectable, which means that App Service can be ejected into different files while other files in there. So the service itself is the place when we do our functionality and share results of those. So similarly, like we do here, we have the name of our service, which is App Service, and the get hello method here, which will just return hello world string at the end. And this is all the life cycle of the nest, which I will talk soon. But now next to the service file, we also do have these app controller that spec that ts file. This file is a testing file for our app controller and by default, nest CLI, we'll generate that one as well. Now that you saw which files will be by default created from Nest CLI, let's go into next video and create our own module for todos with its own controller and the service. 5. Setting Todos Module with Controller and Service: So as you saw, which file is the nest will hold by default, let's create our own module that we need. So we do have this app module which is well mainly used for the entire app to set up entire app. But as we will work with the to-dos, well, we are creating a to-do app with the nest JS here, let's create our own module called todos. To do such a thing, go back to your terminal and use nest G for generate and then module because we are generating module here. And I will name my module to-dos. So now the next CLI will generate the module file for us. Once this is finished, you can see that it created one file at this PET and it also update that one file at the spat. So going back to my Visual Studio Code, I can see that the SRC folder, I do have the folder for to-dos. Now, inside of that folder, I do have these to-dos, that module.js file. So you can see that it is decorated with the module, which means that these two duos is a module. It just represents a new feature for my application. But as you know, the app that the module file was also updated with Nest JS. Well last CLI. And here we can see how in our imports array, we can see that to-dos module was added here, which means that our application will now also include these to-dos module. So later where we build our application for deployment, it will also include entire to-dos module and everything that is in it as well. Next to todos module, we will also need to generate the controller and the service. So here I will lose nest and the g flag, and here I will pass controller. Those hit Enter. Once this is finished, I will immediately run the next command, which is Nest G again for Generate and now service. Again to do so, we will again create to those service here and to those controller. Now going back into my Visual Studio Code, if I open up my to-dos, that module, the ts file, we can see here, let me add a comma here to remove this error. We can see that to-dos controller end to those service was indeed added into my to-dos module, which means that both files will be included in these two, those module. So if I open up my to-dos controller file, I can see that it was generated. Our next to that, it was also gender. These todos dot controller, that spec file, which is test file for my to-dos. And on top of that we do have a service file and the test for a service file as well. So now we prepared our whole to-dos module that should be used. And if you still don't understand how the whole module works, Let's go into the next video where I will explain to you how the life cycle of modules in a nest works. 6. Life Cycle of Todos Module: This video here will be specific just to explain to you the life cycle of all of these files. And I will use the example of our to-dos module that we've just generated. So let's go into the to-dos module itself. To do is modular represents the main file of our to-dos feature, which means that we expect some kind of to-dos future to be integrated into our application. Those module must be decorated with this module to represent the module file, and it is decorated with the module decorator. It can have different options, for example, different controllers, providers, imports and so on. So for today's module, we could create as many controllers services in other child modules, which could be important here as we wish. The main idea is as long as we have all of those imported here, we can at the end, export the entire to those module as a file which will obviously export all of those important stuff. For example, in our example, our to-dos controller, to do service as we are exporting that the dues module in our app module file at the end. That to-dos module must be important. And as it is important here, it will also include that controller of to-dos in the service of those as well. Now this app module file is the main file which should hold all of our main controllers, providers, and import, for example, our to-dos module here. And once again, once we group all of those here, we will again export our app module. But this time this app module won't be imported into some different module low, it will go into main ts file. In this file, we will read the entire app module and we will use the nest factory, which is the factory from a nest as a framework which is able to create the application of Nest based on some specifics that you pass to it. In our example, we will pass the entire app module. So now as we have everything imported, that app module will hold all of those and it will pass all of those, which will be at the end use to create our whole application. That is why in this disk file, we can see that we do have that main JS instead of the ts file, we can have our app module js file, our app controller.js file, our App Service JS file, and so on. Because all of these ones, it is finished, it will compile TypeScript code, it back into JavaScript code. So now that you know how modules work and how can we pass different files and we'll export and import those so we could have the entire architecture and hierarchy of our project. You should also know that this is not just specific to todos. If you wish, you could have as many features as you would like to have in your application. For example, if you wish to handle your users, you could create this specific module controller in service for your users. If you have a feature of some kinds of tasks, you could do that as well for tasks and so on. On top of that, all of these main modules, like our to-dos module, could also have their own child modules. And that way the hierarchy will go from child modules to the main modals, to the main app, that module file that we have. At the end, everything will be finished in our main TypeScript file that we have. Now let's talk a bit about this controller and also a service. So this controller, as I already mentioned, should hold all of our routes. As we just generated this controller, we don't have any route here. But if I open up the app controller file, we can see this one route of get here. Basically the controller itself should be our main connection to the front-end or UI side that we could have our client-side. The easiest way to connect our server and our client would be through our routes. So the controller should be used for that. It can create a route and once the client side dispatch some kind of GET request at this point, it will go into specific function as we have here. Again, our controller should wait for some kind of response and return that response well, that data back to a client side, which we are doing here. We are waiting here for this get hello from our App Service, and we will return that back to our client side, our front-end. But now opening up our app service file here, the syllabus file, whole file is an injectable, which means that it can be imported into different files like different controllers, interceptors, guards, and so on. So mainly we would hold all of our main functionality for specific features into our service files. So once we pass call from our route well to our controller into our service at the end, we will do our functionality here. We don't have any, we just have the string hello world, and we should return some kind of response back. Well, the result at this point, this is our result, this Hello World. So this hello world is being returned to this controller, which is being at the end return to our client side. So now if you would go again to your terminal and run npm run, start. And once your project runs, you will see the hello world on your screen or localhost 3 thousand. This means that that controller and the service work correctly combined. So here we call this get route. As we visited our localhost 3000, that get route contacted our controller, which at the end contacted our service. Service returns some kind of result for, for us, which was this Hello World. It returned back into controller. And that controller return it back to our client side. So that is why we do see these Hello World as a message on our screen. Next to that, it is worth to mention that in our app controller file here you can see that this controller has just empty brackets. But if he would go into to-dos controller, we can see that this controller has these to-dos inside. Which means that these to-dos, well, it is based on a feature of our application. So if you, I would write a get route for specifically for my to-dos here, I won't be able just to visit my localhost 3000 to get some result. But I will need to visit a local host 3000 slash these to-dos, because this to-dos we'll represent our route in a nasty JS. While in our app controller we don't have any. So by default, it will go into this controller. When we visit our localhost 3000, it will go by default in this controller, and then it will go by default in the first GET route that we have. As this one here is. Now hopefully you understand this life cycle of nest application a bit better. Now, let's now start to working with our to-dos. So in the next lecture, we will create the data for our to-dos that they will use. 7. Creating Data for Todos: So as I mentioned in this lecture, we should create our data for our to-dos. We still want to have our database. So we should create our to-dos inside of our project. To do such a thing, I will go into source to do's end my to-dos service as the service is injectable class, then the service is able to share my data through different controllers, services and so on. So as it will be used to share my data, I will put my data inside of the service. So how should we do this? So inside of my to-do service, I will create my own to-dos property. This property should hold some kind of a type because we are working with the types with here. And four now the type will be any, and it should hold the array of any properties inside. So here I will set this to do's to be an empty array. So now we do have this property and this property will hold our data. So let's inside put some sample data that we will be able to use. For example, I will put the object here, and first of all, I will pass property of ID because my to-dos should have their own ideas to be unique. So here I will put the string and 0 inside like an ID will be 0 for this particular todo. Next thing, I will put the title as a property, which would be, for example, create a course. And I will put completed property which will be set to, let's say true. Then we can have different properties here. We can add few more to those here, I will create two more to do so 12, Let's change now this data, so ID here will be one, and for my third one will be two. Here I will change titles as well. So this title, we'll say go to a store. In this title, we'll say wash a car. And this completed to do now will be set to false. And the third one settled true as the first one is here. So here I do have an error. This is just a prettier error. So if I would add a comma at the end, it will resolve this error. This is just my Visual Studio code. Maybe for you it didn't even show up the error. But now we do have our to-dos in our service. Well, we hard-coded those and let's move into the next video where we will return these to-dos from the service into our controller. And that way you can see how can we share the content from our services as those are injectables. 8. Returning Todos Data from Service: So as you can see, I'm still in my service. The idea here is to share these to-dos array of data to my controller where I will be able to create my get route and get this to those from my API. So I will show you now the example how this is usually done. First of all, in this to do service, we can create the method called get to those. And this method here, I will put the comment at the top, getting to-dos. And this method here will, well should just return this dot to those here. By this, we will return entire content of our to-dos data, and that is okay, but how can we improve on this here? First of all, we should set specific type, well, the type of the data that will be returned at the end. So for now, as our to-dos holds this any array type, I will use the same type here. So as you can see, there is no error. Also here we can see that we are returning the studios. This is definitely okay. But if at any point we wish to modify this to those, Let's say that we wish to modify the title of our first to do from a create the course to, let's say, buy a milk. Then at that point we will also modify our initial data that we have, which maybe you don't want to do that. So in any case I will remove, move this back. We can hear, return a copy of this data. And there are different ways to do that. We can use cell lyse here on our to-dos, which will return the slice of our data, well, our to-dos kinda the copy of it. So let's take a look as these two. Those could be our pie and we are taking the slice of that pie. But in this example, the slice of the pie would be the entire pie, which will create a copy of these tools. But now there is even a better way to do that. We can wrap these to-dos into an array here. And by wrapping this to-dos into an array, we are currently having an array inside of our array, which is obviously not good, but we can use spread operator here, which is only three dots. This spread operator will make sure to return the copy of a disarray to us. So basically, at this point we will hold only one array here. So we won't rip our array with diff, with another array. And on top of that, we will return only copy of our data. So if you wish to modify only the copy of the data and we wish this initial data to stay as it is. This will make sure to do that. So this get to-dos method will return some data to us. Well, any data, and that data will be our to-dos, the copy of our to-dos. So now let's move into the next lecture where you will see how can we import our service into our controller and the really return that data to our controller. 9. Using Service in Controller and Getting Todos: So as you know, we should import first of all, our service into our controller to do that inside of my class of to-dos controller, I will first of all use something called constructor. This constructor will be able to well import our service. It can do much more. It is the constructor for our class, but mostly when we import our services, we do that inside of our constructor. So to import our service, I will go into these brackets of my constructor and I will use private property. I will name this private property to those service. And I will set this type well, I will set the type for this to do service, to be my own to-dos service. And you can see that automatically these two to service will be important here at the top. By this here we were able to import our service does to those service into our controller. And we can do that because because our to-do service is injectable. So going back to my to-dos controller, what I wish to do is to create my own getter out here. So let's create our own first route. So here I will first of all put the comment, getting all to-dos. So to create a route we must use at, and they'll get this get is a decorator. So by using these here, we will create our own route. And the gap should also be important from Nest js slash CLI. After these get, usually create a function which should be called when these get route happens. So this function will be get to those because we are getting all to-dos here next to this function. This GET should hold some perimeter, for example, some specific ID for the to-do's or something like that. But because we are trying to get all of our to-dos, we want to pass any parameter here. That way we will return all of the data from our to-dos. So how can we return the data here? First of all, we can use return keyword here. And to return the data from our to-do service, we will use this data and we have an option to use this data to, to service. And if I again use that here, I will have an option to use this. Get to those because as you know, in our to-do service, we do have this get to-dos method which will at the end return the copy of our to-dos. So at this point, we are returning whatever we are getting from this method here. And in our service as we have this type of any, let's make sure that here we are also setting the specific type which is any currently. Later in this course, we will create a specific custom model for our to-dos. And we will reuse that model instead of any type. But now that we have this from our to-dos, from here, we are passing out to-dos through this method into our controller where we are importing our service and also creating our first get route, which is the route to get to those. By this comment, you can see that this route, we'll return to some kind of data to us and that data will be returned from our service. Now, if you wish to test this, let me zoom this a bit so you can see it. I'm currently on my Hello World route. Well, that's just localhost 3000. If I would use localhost 3 thousand slash todos, hit Enter, then on my screen you won't see Hello World, but you will see this data. I'm also using specific extension which will format my JSON data. Probably for you. You will see the data like this, but with this extension, you can see the data as I see. So what do we have here? We have our array of our to-dos, and inside we do have three objects, which are our own three to-dos. For example, we have ID of 0 for the first one with the title of create course and completed value of true. So by this here, you can see that our data is successfully returned from our service through our controller at the end to our browser here. So good. Now we have an option to access our data through our API. Also, if you had any errors like four or four, for example, slash to-dos route is not found, then just make sure to restart your local server. Once you do that, everything should be okay. But with this data here, what can we do now? For example, the next route that we can create is to get specific. To do so by passing a perimeter of one, we can try to find the ID that will be set to a value of one. And we can return only that to do instead of returning all of our to-dos. So let's go into next lecture where we will return only specific to do that we wish. 10. Creating a Get Route to Get Specific Todo: So in the last video you saw how can you get all to those? Well, all the data from our service. And in this video, we will create a route to get a specific Todo. So first of all, let's put a comment here. Get specific to-do. And now let's create our route. Well, this is still a get route. So as we already used get route, we will hear again use decorator for get. Now, I will pass my function here, which will be get to-do. And this function here should return something to me. So I will use return keyword here as it should return some kind of data. I should also put the type of the data that should be returned here. For now, it'll be any well, array of any data and what will be returned here? Well, I will again contact my service, so this dot to those service and on it I will use get to-do method, which we still don't have nothing to lose but to do, we will create these methods. So let's go first in our service. So how can we get a specific to-dos from our array here? Well, first of all, we do have this ID property on each of our to-dos, and it is specific to each of the to-dos. So in our route, we should expect some kind of ID to be passed to our route. And by that ID, we will find and filter out that specific to do so now if you wish to pass a parameter in a nest throughout, you will go into your method here. So not in I get decorator, but in a get to DOE method. So here and I get to do method I will use at param. And that should be imported here on the top. And I will call this bottom here. This param will expect something to be passed on for. Here. You can see also examples of this. And I will pass my ID as the example says here, but I will name it todo id. So now I'm expecting this todo id here, and I also should save it somewhere. So I will create my own so-called variable, which will be todo id. And I should also set specific type here. If I would go to my service, I can confirm that my ideas here will always be strings. So in my controller here, I will get todo id. Well the type of it will be a string here. So now when we are searching for a specific to-do from our service, we can pass this todo id to this method. And on a service side, we will do our functionality Related to that functionality. Let's jump into next lecture and return proper to do based on todo id. 11. Finding Specific Todo in Service and Returning it Back: So now let's continue creation of this gets specific todo route. For that, I will go into my service now and in my service I will create new metal. First of all, let's put a comment of get specific to-do. And here I will create my route, get to do here. And as we know, if we go into our controller, we are expecting this todo id to be passed into our routes. So here we should expect to do ID, which will be again type of string. Now, as this method should return one of our to-dos here, I will put the type of it to be just any, not any array, but any because it wants to return the array itself but on object. Now, in this method, we can create const of Todo, which we need to find right now. And to find that, I will use this dot to those. So I am trying to get all of my to-dos and on it, I will be able to use this, find the method. This find the method should while loop through all of my to-dos and return specific to do if it finds one. So as it is loping, we will, for each that iteration have our own to do. So here we can pass it to do, and we will have an arrow function here. So what can we do here? We can check if and if that to-do, that id is equal, well triple equal to our todo id that we have. If that is the case, then we will return our todo here. So now already you can see that there are some problems. First of all, we don't have specific type for this to do. Also here, this is too much of code, so let's clean this up a bit. First of all, this to-do, I will cut it out here and I will use here a brackets and inside my to-do and pass the type of this to do, the type will be just any four. Now, here also, we don't really need to use this curly brace. So let's remove all of these. And in our arrow function directly here, we will use if this to do that. Id is triple equal to our todo id that we are passing into here just by using this one line of code, we will directly return that. To do that should be fine. Also here we have our cones to do. So now we can just return that to-do as well here. But you should know that this here is also not so much important because we can also clean this code even further. For example, instead of returning my to-do here, I can directly return from this found find method. So here, return this way. We will return whatever we find in our to-dos array and we will find our specific to do that. We need that to do. We'll go into our controller here and from there it will be returned. So this way we will keep our clone, our code quite clean in just few lines of code. So the last thing that we should do here is going to our controller. And here we do have same to get decorators. If we visit our localhost 3000 slash todos, it will read first getMethod, which is this, get to those, but how it will know to read this second decorator. And here we are passing this paradigm of to-do ID. So is it possible to pass the spectrum into here? And yes it is. First of all, we should use single quotes here and todo id by this here, we will specifically and dynamically tau this route that we are searching for some kind of specific ID. So here we will pass that ID instead of hard coded value. So now we should be able to go and check our localhost slash todos to get our first route, get route, which will be this one here. And also localhost 3000 slash todos select specific to-do ID to get into this method. To test this, go into your local host 3 thousand. And let's go first of all to our slash todos page. Here. We are still getting all of our to-dos. So let's find some specific to do by the ID. For example, let's find this, go to a store. So here, localhost 3000 slash the do's slash one, because our idea here is one, hit enter. And you will see that now we are getting only one to do. We were able to filter and find proper to do that, we need it. So now I could change this to 0, and here it is, I could change this to two, and here it is, I could change it to 22. And now here we will have an empty page because none of our to-dos holds the ID of 22. So for this example, we could do some error handling as well because data was not found, but we will do that later in this course. One thing before I close this video, if you are still getting errors on your page like a route was not found, and so on. Make sure to run your project with npm, run start and then column DEF. This way, you will start watch mode in a nest and which ever change you make in your nest code, your local host will be always refreshed, so you won't need to manually close and start your server once again. But now you saw how can you create your get routes. Now we do have one problem here. For example, if I visit my to-dos, Yeah, I will get all of my to-dos, but how can I create a new to-do now, obviously, we cannot send the post request from our browser. So for that we will use a tool called Postman. Let's go into next video to setup our postman. 12. Setup Postman: Postman itself is a very helpful tool for us. Developers wear it. We can organize our API, create specific requests, organize those by folders and collections, and also send those requests and definitely test dose as well. So if you don't have an account on postman, make sure first of all, to create one. I won't lead you through the creation of the account, but I will lead you through the installation. For that. I'm on my Windows. I will click on this Windows button on my screen, which will download the app for me. So here I am first of all read directly to download Postman. Just click on this, download the app and you have, you must make sure that we download the correct version. Once the download is finished, make sure to click on it. So now I'm able to run this installation for Postman. Postman itself should install itself on its own. Once it is installed, it should also automatically open. So let's go through a postman a bit. First of all, we do have our collections here. For example, our to-dos would be one of our collections. We can have different APIs as well environments. So our mock servers, we can also monitor our data and take a look at the history, what we worked on. For example, here through my history, you can see that quite a lot. I tested my to-dos here, which we will definitely do through this video. So let's go into collections here. On top here you can have a home of your postman, different workspaces. So depending if you are working with the team or your personal workspace, you can have your reports and just explore the postman as a tool. On the right side of the Postman, you can invite people are on. You can also sync the postman online. Well, from Cloud to your local machine postmen, you can do the settings, set up your environments, and also upgrade to the higher deal of Postman. So now that you have your postman account, make sure to login into your postman properly with your account. And here we are good to go. So in the next lecture, we will create our first collection and our to get methods that we have. Well that will be get routed to get all to-dos and also get routed to get a specific to-do. Let's first start with those simple things. 13. Creating Get Requests: So now that you've set up your postmen, we are ready to create our collection and also our requests that we already created in our API. So first of all, on the left side you can click this Create Collection button. This Create Collection button. We'll create specific collection for us. So for example, I will create my own name to those. Now this collection can hold its own authorization tests, variables, and so on. But that is not important for our example here, we won't go so deep into the LPI, but on the left side we can see this ADA request, so-called link. So we can click on that and we can add our own request here. First of all, let's create the request to pull all of our to-dos from our service. So here I will name this get to-dos. And if you don't remember, go back to your code so you can check your entire request. You can see that it is a getMethod. So in a postmen here we can select the type of the method. For us. It is a getMethod. And if you remember to get our to-dos, we had to visit localhost 3 thousand slash todos. So you can copy this URL from your browser and here in enter request URL next, next to our method, we can paste that URL here, and that is it. Basically we set up our methods and our URL to get our tools. Now to test this on the right side, we can click this Send button. And just by clicking that send button, we will get our to-dos here, well to those from our service, but moving back to our requests next door, get to those requests. We have our get to do request. So visiting our code once again, we can see what we need to send for our get to do request. Also one problem to Norris here. Here I set the request. Well, the type of the data that we will get back is any array. Let's move this array from here because we will just return any data so it won't be the array. So now that you saw how can you create your own request in a postman, try and create this, get to do request on yourself. So just pause this video and do it on your own. And you can continue this video to do that with me, to create a specific route to get to the specific to-do, I will go into to-dos collection, click these three dots here and again add requests. So here, now I will name this request, get to do. The method will be get to do, and I will paste here my http localhost 3000 slash todos. And now if I wish to get to do by the specific ID, I will, for example, use again slash one here to get the specific todo. If I send this request now, I will get specific to do here, which is to do with the idea of 1, go to a store is a title and completed is false. If I change these two to send. Here it is now we have an idea of to wash a car and completed as true. So now let's jump into the next video where we will create our to-do. 14. Create Todo: So now we will work with our code to create our Todo, where we need to create our posts throughout. And also functionality in our service to create a new to-do. Next to that, we will also create a new request in Postman and test that through that request. So first of all, let's create our postal route here in our controller. Here, I will set create to-do Well my comment. And now we can use post route similarly as we used get route here. So this post route must be called. And below we will set our method. So create To-do will be our method. This method here, we'll return some data to us. For example, we can return any array here because we will return the new array with the new two dorsal side. So to create the new Todo, we must get data. Which data to do will hold. So here I will use decorator of body. And this body must be again imported from Nest slash common. And we will call this decorator here, decorator itself. Well, decorator body will hold the body of our data. So here we can get that entire body which will be the type of any, well, our database. So now in our method here, we can again returns something from our this.tab, those service. And this time we will call this method create Todo. We still don't have this method in our service. We will soon created and to that we will pass our body that we are getting from our body decorator. So opening up our to-dos service now and next week we'll get to those and get to do methods. We will create a to-do method. So create Todo here, this method expect some kind of body reaches our todo, which will be the type of any for now. And this will be a method. So here we should also pass the data that we will get back. So it will be type of any array. And let's now create our to-do, our todo here. Bill, hold a title and completed value, but it won't hold the ID itself. So this ID will, we will need to set that. Well, we will generate our ID here. So for that, to generate our to-do ID, we will use to do ID here. Well, let's create the coast of todo id and we will set this to something. What we can do here is, well, january DID by the date. So for example, we can call our new date here and dot get Time, which will return the Unix time to us, which will always be the unique ID for our Todo. So down there we can again create coast of New to do. And we will set this to be our new Todo that should be added into our data. So this new todo should hold the ID as our to-dos are holding here as well. So the ID will be our todo id. Then next thing will be our title, which will be our to-do that title. We will assume that our to-do data here. We'll hold the title on it and we will have our completed value. And this would be to do that, completed. This to do that. Well, this completed entitled values will be sent in our body well, in our body that we are getting in our post method here. So that body will hold the todo, which will hold the title and completed. And we are able to pass those values into our new to do. Now, let's use this new todo. So to push this to do basically to our to-dos method here, well, to those data that we have, we can just use this dot to-dos, that push method. And this push method will push our new todo into our to-dos data. And back we will return, return back a copy of our to-dos. So to splice our todos, todos here, and that's it. So basically we have our post method here, which we'll call these create pseudo method, which will get the entire body, that body. Once we get that, we will pass that into our to-dos service create to-do method into those service. In that method, we will generate unique ID by the date, well, get time method. And also we will generate our new to do that we should create. We will generate it to do by the hour unique ID, by the title from our body and by the completed value from our body as well. And also at the end, we will push that new todo into our current data. Well, our to-dos array that we have at the end, we will return back our copy of our to-dos array, which is correct. We should return array of any data here and here as well. And the last thing left is to task this. To test this, we will need to go back into our postman. In our postman, let's first of all create new requests. So add request, request will be called create to-do. And now we won't create a get route, but our Paas trout. And now if I save this control S, and we will see that here we have a post keyword instead of Get Keyword. I will also move my create Todo here to the top. So we call the well organized this a bit better, and let's now use proper URL here. So in my controller, our URL will be localhost 3000 slash todos. And we can set specific URL for outpost, but we don't need to do that. One thing that I will do is remove this post from here and move those to a tap so we could organize by crud functionality. Crud functionality if you don't know, is basically create, read, update, delete. So first of all create methods, then read methods. After that we'll come update methods and delete methods as well. So here we have our post method without specific URL. I will do the same thing for our service. I will remove this great to do well, cut it out and move it to a top side. Well from the top to bottom so we could read this as a crowd as well. So create, read, update, delete. That means that we can just copy over our URL from our get to-dos method, and by that we will send our post method. One thing that we know is that our post method should hold some body. So in Postman, we could set our body just by clicking on this body. Here, we can set specific data. I will set the role data, and I will select the JSON data. So here we can send the JSON data. One thing that we know is that our to-do should hold the title. That title could be, for example, watching a movie. And completed value as well. This will be a Boolean value, so true or false, I will set this as false because mostly when we create the new todo, it is not finished, so the completed value will be false. Now, if I send this request, what we see back is the newly-created array, well, the same array, but with the new data, we can see that this ID now, we'll hold our ID here. Well, the unique ID, which is a number that is not good When must pass that as a string because other ideas are strings as well. Title here is watching a movie which is good and completed value is false, which is again good. So let's go back into our code. And here we can move the seasonally tostring just by using two string at the end of our get method. So again, in our postman, if I dispatch the same request, we will now see the same result, but this ID will be a string. One thing to notice whenever you refresh, well, whenever you change something in your code and local host is refreshed, then your data will go back to just three to those that you had initially. And that is why later in this course, we will use a database so we can save our data somewhere and not initially create our data when we run our local server. But this is it related to the posture method. Let's now move to delete method. 15. Delete Todo: So now we created our GET request and post requests. Let's now create also delete requests. So at the bottom here, I will first of all put the comment of the lift to do. And let's create our delete request here. So for that we will use decorator of delete. And that decorator must be important again from ness js slash CLI. And once we have this deleted decorator, it will call a function called delete todo. And this function can just return to us, well, back at us array of left of those. So for example, if we delete one of the to-dos, we should get the rest of to-dos back as an example here. So first of all, how we will know which to do to delete. Well, similarly, as we got specific to do here, we passed our to-do ID. Well param here, well, we will do the same thing for our delete. So here I expect to get some kind of todo id. And in my Delete to DOE method, I will use my parallel decorator in it. I'm expecting my to-do ID, which I will name to-do ID, which will be the type of string. And here we can do our functionality. For example, we can return from our this to do service and on it we will create a delete todo method. So we still didn't upgrade this one, will still need to create this one. So into our delete to do a method, I will pass our todo id from our para. So now let's go into our service to create such a method. So in my service at the bottom, I will put the comment of delete to do. And here I will create a method called delete to do. So, we already know that this will return an array of any and also it should get to-do ID in it. So it will, this todo id should be the type of string. How can we remove specific to do from our to-dos data that we have? Well, for example, by this ID we could find specific index of data. So for example, if we wish to remove the to-do by the ID of one, then we will get the specific index, which will be 0, 1. And we will splice this by the index. So let me show you how can we do that. So first of all, I will create a const of to-do index. And I will set this equal to my this.tab to-dos. And on it we will have a method of finite index, This find index. We loop through all of our to-dos and for each of those iterations, we will have our specific to Duke, which is the type of any. And here we can set our condition to find specific, to do well, to find the index of specific to-do. That condition is if the to-do ID is triple equal to our todo id that we are getting from our para at that point we found that specific to-do and we are getting the to-do index by this find index method. Now that we have our index, we are able to use this data to those and on it to use splice method. The splice method expects two values. First value, where the splice should start well, which items should delete. So here I will pass that to do index. So it will splice out the todo with that specific index. And the next value is the how many items after this to-do, it should splice as well. Well, we just wish to splice one item so I will pass number one here. And at this point we are removing one of the to-dos. So basically we should have modified to those data. So here we will return our copy of the left over. So basically splice our to-dos once again. So this dot those similarly as we did for our to-dos and also create to do so now this should be it. To test this once again, we will go into our postman. In our postman onto those collection, I will create, well, add a new request. This request will be called delete to do and the type of the request should be deleted. So we already know that this here will be similar URL as for our get specific to do because we wish to visit localhost 3 thousand slash todos and slash specific ID. So for example, if I first of all go to get to those method here and send that, I will get all three to those. Now, if I wish to delete specific to do for example, due to do with the idea of one. Then in my Delete to do in my URL, I will pass number 1 here as the last value. So here sending that data and we are getting back only to todos. The first to do, we'll call the idea of 0. And the second to do we'll hold the idea of two. As you can see. The to do with the idea of one was absolutely deleted. To double-check this, we can go into back into our get to those and dispatch the same request. We will see only two to-dos here. So one of those was successfully removed. That would be how the delete functionality works using this nest JS technology. The only method that is left is our update method. So let's go into next video and update one of our to-dos. 16. Update Todo: So we have only one more method to create, which is our patch method. So just above my Delete to do now, I will put first the comment of update to do. And here we will use patch request, which must be imported here at the top, right after my patch request. First of all, I need to call it here. I will also create a method called update to do. And this method will return the whole array of our to-dos. So here I will set the type of any array. So at the end we should get the whole array back with that updated to do as well. First of all, let's see how we will know which specific to do we should update and which which data. You already know that we have an option to use upon hearing the nest and to pose a problem in our example, our to-do ID. Also next to that, we can use a body to get the specific data that could be passed to us as a body. So here, update to do will be the combination of both. First of all, let's set our param for our todo id. So our PECC should expect some kind of dynamic todo id here. And in our update to do, we will set it up, which will be called todo id. We will name it as well to do ID. And the type of it will be just the string. Next to this. We will get that updated the data for our todo, which will be our bodies. So here we will use our body decorator and we will name it to do as our updated to do and the type of it will be just any for now. So in our update to do method in our controller here, we should return something from our disk to do service that update. To do method, which we still don't have. We must create 12 this update to do method. We will pass our to-do ID we are getting from our parents and also our to-do that we are getting as a body here. And that should be it related to controller. Let's go into our service. Just above our delete method, I will put the comment of update to do. And here I will set the method called update to do, which will be our method that will return at the end any array. Well, the same as our controller returns here. Later, we will change this any types into proper models that we will have. Don't worry about that. And in this update to do, we are expecting some kind of to-do ID, which is a string. And our todo, well, to do that should be our updated the data, which will be the type of venue for now. First of all, what do you think? Should we automatically just update our to do here? Well, no, we can do some validation as well. So here inside of my update to do, I will check if my to-do dot title. If the title of it is equal to an empty string, if that is the case, then we don't wish to update our to-do because always we wish to have a title on our to-do. We should not be able to update our todo with an empty title. This goes same for our creation of R22 here. For now we're just expecting our to-do to hold some title, but maybe we could add the validation here as well. So in our update to do what could be our validation? Well, now if we don't have a title, we can just draw a new error here. So here I would use straw, and now I will use the new and the nest JS has their own exceptions. So here I will use bad request exception. This exception must be imported here at the top from Athens to js slash common. This exception will return bad request status code back to us, which means that our request, well, the data for the request, for example, our body, in our example, our todo with our title is not properly formatted or does not hold a properly formatted data. So in our example, our title should never be an empty. Same validation could be added for our Create to do here. So just at the top of this method, I will add that as well, going back to my update to do now. Now that we have this validation, first of all, let's get the correct index of our to-dos. Well, of our specific to do that we wish to update. So the same function that we used here for the five index. So here I will use two cons of to-do index to be equal to this dot to-dos that find index. This will iterate through our, through our to-dos. And each of those iterations, iterations will give us 1, 2 du, which is the type of any. And once we have that to do, we will check if that todo id is equal to our todo id that we are getting in this function as an argument. Once we have that index of our to-do, to update that to-do is quite easy. We can use these to-dos and we can find the specific to-do index. So by this, V will find specific to-do by its own index. And we will set it equal to an object here. Now this object will be structured. Two sides. First of all, we will grab all of our welder copy of all of our current data. So this not to do's, again to do index inside and use comma. And we will replace that data with a copy of our task. Well, not task, sorry, but our todo, which means that our current to do whatever the data it hold it. It will hold the newer data that we pass. So if you change our completed status owner to do, it will change only that status. If you change only the title, it will change only the title. And we are also able to change both one update. Now here, once we have this, we can just return something back. As you know, we should return the array of any. So here I will use, I will use splice that, this dot to those and that's it. Oh, okay. I just also realized that here I didn't use to do bus today. So to do here should go. And we're ready to test this in our postman. First of all, as you remember, we added that the validation for our create to-do, let's try and add now to do without any title. So just an empty string here. Send. What we see is our status code of 400 with the message of bad request. If I reverted the change for I'm watching a movie for my title and sent now, now we do indeed have a new to-do added at the bottom. Let's now create our update to do request here. So here on the left side, I will go to add request. I will name it update. To do like this, I will move it above my delete todo method itself should be PECC method, not get. And I will just copy my URL from my to-do method here because it is quite similar. And next to that we should be able to pass some kind of a body. We will use a row data for a body and JSON data here. So here what they wish to do is to change my completed property to be true, for example. So let's now find one of our to-dos. First of all, I will get my to-dos here. So get all of those. We have four of those now. And the second one here with the ID of one has a completed value of false. So here in my update to do, I will try to update that one. So my URL should say localhost 3000 slash todos, select one. And the completed value here that should be sent, should be our Boolean value of true. If we send this currently, what we will get back is the updated method for our completed right. We don't have a false here, but are true. So that is good to this same method. If I wish to now update my title as well next to these completed value, what I will do is set this title to a string, which will save by our milk. And let's now dispatch the same request sent. And here it is. Now once again, if I tried to update these title, we just an empty string and send instead of our data, we will have our bad request exception here, which is good. Now, with all of these requests, you are finished with your crud functionality for your to-dos. Well, basically we have the whole API application, but obviously we can quite improve this application. We can add specific model for our Any types. We could also integrate async await well promise pattern here. And on top of that, we can create our own Mongo database and save our data into that database and not in our service. First thing of those that we will do will be our own custom model. So let's go into next video and create that one. 17. Creating a Model for Todos: So as you already know, we have our post-mitotic hour to get methods, patch and delete methods. And all of those are using currently quite extensively this any type. What, what is the point in TypeScript if we are using this any type everywhere, because we are setting this any type for our to-dos here. We will now create our specific type, well, our interface or a model specifically for our to-dos, and we will share and use that model instead of any type. So in my source, I will create a new folder called models. And here we will store all of all of our models. Inside of these models, I will create my model core called to do that model.py TypeScript file. And here now we can export interface of two doped. So what should these two do hold? Obviously, first of all, it should hold our ID. This ID will be the type of string. So here I will just pass a string keyword next to that, it should hold the property of title, which will once again be a string. And the last property will be our completed, which will be a Boolean value. And that should be it. There are some errors here which maybe you don't see. These are just the arrows for my extension. So I could go quick fix here and disable this for the entire file because it is not so much important for our use case here. So now with this custom to-do model, we are able to go into our controller back and use that instead of our Any types. First of all, here we are getting the body to create the to-do. And this body should be the type of to do. This to do must be imported here at the top. And you can see that it is important from a to-do that model itself. So now these two do is quite correct. On top of this, I will go back to my model. And here we should change this ID to be an optional property because we want work always be tau or ID. For example, if we try to create a to-do, then our to-do won't have the ID. It must first of all generate one. So by this, by adding this property as an optional property, we won't have those interface errors. So going back to controller, Let's continue now here, instead of any array, we are expecting a return of to-do array. Here, again, to do array. Down below, we are passing a paradigm of to-do ID, which is a string that is good. But we are returning here just the to-do, not the type of any. Down below, again, we have a paragraph todo id, that is good, but we also have a body of Todo which is currently any. I will change this to say todo. And here the array will be todo array. And the last one is our delete method, which will return to do array as well. With this, we are fully finished with our controller. So let's go in our service now. In our service starting from the top here, first of all, we have our to-dos, which are type of any array. This will be a todo array. You must import this modal here at the top. Now for the Create to do, we are expecting some kind of to do here. Well, our body, this will be the type of to-do. Then the next thing that we have is our new todo. If you wish, you can set up type here of to-do as well. It won't harm your code. We are pushing out to do here and returning our array of to do so as we are returning the array of to-dos here, we should expect to return array of tools down getting the to-dos, again, return that array of to-dos for a specific to do, we will just return to do here. And as we are finding are specific to do here. You can see that with every iteration hour to do is set to any, now, it is set to proper property. Well, the model of to-do continue down below for our update to do. Again, used to do instead of our to-do body and used to do instead of our any array down below, again, we are using this find index method, which will each iteration use any as a type we will use now to do as a type. And the last thing is our delete todo, which should return to do array and here to do index, find index, to do as any, change this to be, to do as well. And that's pretty much it. So now with those changes, we can go into our postman and test everything once again. So in my Postman, I have a get to those method. If I send that, yes, I'm still getting my data, so my request is working. Then I will create one of my to-dos. Send that. Here it is, It is still working. Then let's get a Specific to do with the ID of two. Send that. It is working. So that's good. Let's updated the specific ID. Well, to do nothing the id with the title of buy milk. Here it is, buy a milk, so that is also working. And finally, let's delete that bio milk. So cent. And now we don't see that buy a milk to do here. And that's it related to our model. You can see how easy is to create our model and use it instead of our Any types. So now we can switch a bit from our API and start talking about Mongo database. 18. Talking About MongoDB and Creating a Cluster: So the plan for this video is to introduce to you or MongoDB and also to create our own first project on MongoDB. That project will offer to us to build a database which we can use to store our data, for example. So currently all of our to-dos that we have on our side locally, well, to be more precise in our service, should be stored in our database so we could access those data and modify those as you wish. If you already have an account on a MongoDB, good. If you don't, please make sure to create one. Mongodb itself is a database for modern applications and it will store our data in a JSON format which you already worked with. On top of that, it is quite flexible and scalable database as well. So you can use it for your smaller projects, like your portfolio projects, but also in a scalable applications. So now what you can see is that I am currently in my dashboard well, on my account. So going to the left side here, clicking this project down below, we will be able to create, well, to generate our new products, or just click this button to generate yours. First of all, we should name our project and I will name mine nest to-do app. Now we can just click this next button. The next option for us is to well, at some members and also set permissions for those members. So if you have a team of people working on this database, well, on this application, you can invite those people here and also set specific permissions for those people. For us, we can just skip the step of because we are working individually. So just create project here. Once the project is created, you are redirected into dashboard of your project. In here on the left side you can see your databases, your triggers, and also some analytics specifically to your database. Down below in a security section, you can have a database, a database access, which you can grant to specific people, maybe or a network, local network access as well, and some advanced security information if you wish to change those. The main part, well, the only part that is important here for us is to create our own database. So here in the center we have this big green button saying build a database. So just click on it. Once you clicked on that button, you are first introduced with the mispricing table. You can see how the serverless database will work and also the price for that. You can see how normal scalable database will work as well. But what we can see here is that on the right side we do have this free tier for us. This free tier will give us, well, just the basic configuration options of Mongo database. And for us, for our example here, for this to-do application, this is more than good enough. So here we can just click on Create button. Here once again, we have these tears at the top. So we will stay on this totally right Theatre, which is a free tier. And we should select some region here, as I'm in a Europe and the Frankfurt is, well the most closed location for me, I will select that region. Down below, you can see what your cluster till we'll hold. For example, you can see that this cluster will be included just for low traffic applications because it is a free one. Can easily change that if you be skier. But for our example, once again, that is not important. They have additional settings here, which you can see. And also we can change our cluster name here. Usually this is option 2, well-named your database name, your cluster on a MongoDB. So I will just stick to this default name. Close this accordion and just click on this Create Cluster, big green button. One thing that you should know here that this Create cluster, well, creation of this cluster could take some time, so just be patient. So once your cluster is created, you should see something like this. We will use this cluster to connect it with our application. We can also see and monitor our analytics and the work of this cluster. And also we will able to browse our collections, which means that we will be able to see and modify our data. Well, I will do is that we will store in this cluster. So for the next video, we will install the tool that will help us to work more easily with Mongo database. And for the video after, we will connect to our database with our application. 19. Installing Mongoose: So as you just saw in your last video, we created our database. And in this video we will install one tool which will offer us well easier workflow with MongoDB database and also our application, because currently we don't have any type of connection with our applications and our MongoDB, our database. One such tool could be mongodb module. It is specified for a not just technology, but we are here using nest JS, even though it is for another JS, it can still be used in a nasty JS technology. And also you can confirm here that it has more than 3 million downloads. Weakly similar tool to mongodb module, also mongoose module. This module is well object modelling tool and also it will give us options to connect to our database with our application. Also get data from our database, modify those data and much, much more. But once again, this tool is specified for Node.js developers, but Mongoose as a tool, we went a step forward and also created the School of wrapper for a nasty JS. So we could install this module and use Mongoose as a tool to connect our database with our application. So to do such a thing, make sure to copy this installation and in your terminal you can paste it by this command. You will install Mongoose as a package and also Mongoose specified package for nasa GIS technology. So just hit enter here. Once this is installed, you can also confirm that to confirm that Mongoose is correctly installed going to your package.json file. And there you will find the Mongoose as a package and also nest js slash Mongoose here. And now that we have such total installed, let's go into next video and connect our database with our application. 20. Connecting DB to an App: As I already mentioned in this video, we will connect our database with our application. And for that thing we will use mongoose module, specify the wrapper for our nest jazz technology. If you wish to learn more about the Mongoose, you can visit mongoose js.com website, which is official website of this package. In their own website, you will find these cool example where you will see how can you connect your database with your Node.JS application and also how could you save some data in that application? Next to that, there is whole documentation of Mongoose as a package, but about our connection here, it is quite easy to connect your cluster, well, your Mongo database, which are application through Mongoose package. First of all, as you already installed mucus into your project, you can go to this source folder and open app.py module.js file. As this is a main file of our application, we will import the mongoose module here and also connect our database right here in this file. First of all, to import mongoose module here at the top, I will use Import and then mongoose module. Make sure to use mongoose module because you need to import more goods from at Nest js slash Mongo's. Now that you import that this mongoose module in your imports array down below, you are able to use it. So just put comma after your to-dos module and use mongoose module here. On these mongoose module, you are able to use something called four root. This for wrote method will give us the option to pass some kind of well string to that. This requires string is the URI string to connection with our database, and we will get that string directly from our cluster next to that, you can also pass object of options here for this mongoose module, if you need to set up this and customize this a bit differently to get that URIs shrink from your, well cluster here on a cluster page, your own database, click Connect button. And this will show you this pop-up. First of all, in this pop-up, there are few steps that we need to finish before we get that URI string. First thing that you should do here is add your IP address. So here you will put the IP address of your server, your local IP address, or you can also allow access well from anywhere that is obviously not so secure. And I will hear at just my own IP address. So just click on this green button to do that. Once your IP address is loaded here, make sure to add it by clicking on this green button once again. And that way, you are prepared to well connect your IP local address, which your cluster. By doing that, you are the only one that is able to read and write to this application. So if you wish to deploy this application to production, well, you will need to find that different IP address. Well, the IP address of your server, because your users will have to have an option to access your data well, database next to that IP address, you also need to create your own user for your database. So here I will pass DB for my database and on Mexico as my nickname or as my YouTube channel. So make sure to visit my YouTube channel. You can also subscribe there as there is a weekly content from me that you can expect that there, which is obviously the content based on web development next to that on the right side, you should also create some kind of password. I will click on this auto-generate button in one crucial thing here is that you will also have to save your password. So make sure to copy our password as you will need it later for your URI string. Now that you have your user, click this create database user. In the next step we will choose a connection, well, the method to connect our application with our database. So click here to go to a next step. And here we will connect our application. So to do that, we will click on this connect your application. And here you have that URI string. You can see that URI string holds this DB America, which is my database user. And here there is a password keyword. Instead of these password keyword, you need to paste your own password here. So make sure to copy this URI string and back in your code, in your string connection here, use that URI as a string here. Now last thing here is to change the password to our own passwords here. So with this here now, our database should be fully connected with our application. So you can just click Close button here. Obviously you won't see any data here on the screen because we didn't save any data yet. But as soon as we save our data, you will be able to visit your cluster and you will be able to see and browse your data and collections here to test if these connections worked, you can, well, I will go into my terminal and clear the terminal, and I will run here, npm, run, start with these commands. We will start our application. And one of those logs here in a terminal should be related to our database. For me, obviously, there is an error which says that there was a wrong authentication. So I will go into my database and quickly fix that. To fix that, I will go into network access page, and here I will first of all delete my IP address. And yes, I'm sure that I wish to delete this. Now, I will add IP address. Once again, click on this green button to add the IP address. And here, instead of adding your, your own IP address, now, I will allow access from anywhere. This way it will be easier for me to create my application and to deploy all of my changes into my database. But obviously once I'm finished, I will make sure to remove this from my database and use correct IP address. So hit Confirm here, and once everything is deployed, we are ready to test again. So going back into my terminal, I will close my local host. I will also clear my terminal and run the same command, which is npm run start once again and let's follow our logs right now. And now, I don't have such an error here. That error could occur if you have well, badly IP address or you didn't authenticate yourself properly, for example, your database user is wrong or your password is wrong. So make sure to have all of that properly passed into mongoose module here. But you can see is that we do have initialized our mongoose module, which means that our Mongoose successfully connected well our database without application in ones that started successfully. Down below, you can see that all of our routes are there and mapped properly. So let's go into next video where we will create the schema for our to-dos. That schema will help us to easier work with the mongoose module and properly save our data into our database. 21. Creating Schema for Todo: So now that we have our database and we successfully connected our database through Mongoose package with our application, we will use one of many Mongoose features. First of all, we will use something called schema. If you would open up your to-do that model.py file there you will find your own model for your to-do data. For now this is just the model, but soon it will become a whole schema. Schema in a nutshell represents the structure of our data and the Mongoose is able to create such schema in our example here we should import Mongoose package right here into our model and also import the schema from it and create our own custom schema for our own to-do data. By using that schema, we will be able to save data based on that schema into our database to pull that data from our database and modify it as you wish on top of that Mongoose also offer us some additional features for that schema. For example, we could validate our data there as well. So for an example here, at the top, first of all, I will import everything. So use star sign here, The mongoose from and we will pass in single quotes. Mongoose package here. As you remember, the rate we installed development goes through four nested JS and also Mongoose as an individual package. So we could use it here in our schema, well, for our schema. So now let's create such schema related to this interface to do. We will still leave these here because we still need the specific well custom model customer interface for our data, as we will refer our data to this model as well. So we could make a TypeScript work its way, right? But now related to our schema, just after my import, I will use export and I will export const of to-do schema. First of all, this to do schema will be used through our application. For example, in a place where we wish to save our to-dos. So that is why we must export it from this file. So to create our schema, we will call a new and then we will pass our Mongoose package which we just imported. And on it we will have an option for a schema. This schema must be called here and inside we can pass the object with different web properties. Those properties should refer to our properties from our model, well, for our schema of our data. So here, first of all, I will create the title property for my schema. And here now we can pass some additional Well attributes or an option specifically to this title field. First of all, we could refer to a type of our title. As you can see, the type here is a string, but now here we will again use string. But make sure that this string is capitalized because string as a type for a TypeScript and string for a type for our schema is different. For a schema is capitalized N for us to do well. For our TypeScript code, it is just lowercase. We can also pass additional options here, and these are the least, this is the list of all of those options, but I will use one more option called required. And this here, I will set it to true. This is the type of validation for our schema. So for example, if you wish to try to save a new to-do without this property, without the title, then our well, the save of that data will fail at that point because the title as a property is required for our schema. So I will repeat the same for my completed property, so completed here, and I will set the type of it to be boolean, make sure that it is capitalized. Required will be set once again to true. We wish to have completed property when we send our data. Now we do have our schema here, but still our code, our application doesn't know much about the schema. So if you wish to use the schema as a proper schema for our to-dos, we will also need to register it at some place. For that, we will go into todos, todos module because these to-dos module is referring only to our, one of our features for our application here, which is to-dos. Here, we will specify the schema for our to-dos. So here in this module, I will add the inputs array here. And here. In our imports array, we will import our mongoose module. This must be imported from ethnicity JS selection rules for that. As you remember in our app module, we use for root method, but here we will use for future method because now we are working with specific feature here, which is our to-dos. We are not referring to our whole application. What we are able to pass through these feature is different models. So here we can pass an array because our to-dos could hold different models, well, different schemas. So if you wish to pass one of such schemas, like our to-do schema. Here we must use an object and first of all, we must base, well pass a name for our schema. We'll refer to that as a to-do. You can use lowercase to uppercase. It doesn't matter after that, we should also pass our schema here. So use comma here and then a schema property. And here we will pass to do schema. And now we must also import this to do schema from our to-do model. And as we are exporting that to do schema here, we are able to import it in our to-dos module, and that would be it. This is the most easiest way to create a specific schema for your data and also to register your schema for your own mongoose module. So now let's go into the next video where we will use the schema in this mongoose module for the first time. 22. Using Schema and Mongoose Model in Service: So now we should also make some changes to our code as we now have our schema and we also registered our schema in our to-dos module. We are able to use that schema to save our data properly in our database. But that will also require some changes in our code. For example, in our to-dos service, here, in our to-dos service, we are currently hard coding our data here and saving, getting those data, updating and deleting those data. Well, just hard coded like this. So if we wish to work with the correct database like our MongoDB that we have, then we will also need to make some changes to this file. This file will handle everything about our to-dos. We will import our Mongoose here and specify our Todo model here. Well, our to-do schema here. So that way when we save our data, we will be able to well pass our data properly into our MongoDB through our mongoose module. So to do that, right below my data here, I will still keep my hard-coded the data as a reference, but I must use something called constructor here to construct something. Well, basically this constructor will give us permission to import something specifically into this to-do serves that something in our use case here could be inject model. This injector model is a decorator from Nest JS, Well, from a mongoose package of messages, and it must be important here at the top. This will allow us to inject correct schema and to use that schema in our service. For example, if I would go to, to todos module here, I can see that the name of my schema is to do so here in this inject model. Now I can call it and pass the name of my schema of my model. This here could be our to-do. And now as we are passing our schema here, we should also make a reference so we could use it in our to-dos service. So here, for example, now we could create a private property, which will also be a read only property. So we won't be able to modify this property because essentially the whole property will come from this inject model. And we should also pass a name for this property like to do model for example, because we are referring to our to-do schema, which is at the end our model of our data. So here we can reference our model. I mistyped that. So model here, which again must be imported from goose. This is another feature of Mongoose to specify the specific model. And here we can pass our own model. So if you would go to, to do a model that TS, we do still have this interface. So here we could pass our proper type of model, like to do. So this means that we are using here our to-do schema by to-do name. And also we will refer to that schema with our Todo model property here. And we must expect in this to-do model to get data based on our model. To do so now you can see how everything here is quite connected. So what does this means? Well, obviously as we are working here with hard coded the data, we will have to change this file quite a bit. For example, we want to push this data anymore to this array, but instead of that, we will save our new todo into our MongoDB. We will be able to do that through our to-do schema. Same goes for our other outs. For example, here we are, we are getting our to-dos. We will through Mongoose package contact our database to get all of our to-dos and return those right here. Also, same goes for a specific to-do Mongoose package has a method which will give us options to find specific to do and return only that from a database. We will use that method here and same goes for updates to do and also delete to do so we will obviously use quite extensively here, our Mongoose package to save, update, delete, and also get our to-dos from our database. So as this first method here is the crate to do, let's move into the next video to create our todo using Mongoose package and save it in our database. 23. Creating Todo with Mongoose: So let's refactor our create to-do method here in our service. First of all, I will leave my validation right here, even though I still have my validation from Mongoose package to double-check that I can go to my to-do model that ts file in here. I do have my required properties, which is my completed and my title, so those are properly validated as well with Mongoose. Now for the to-do ID, I don't need this todo id. Why? Because the mongoose, when it saves the data in our database, it will also generate specific and unique ID from that MongoDB saw at the end each of our to-dos we'll hold their own specific IDs. Also, I don't need this new todo right here because we want to generate this to do right now. Well, hard-coded like this. So because we will use our model that will inject it here to generate them, to create our to-do. And soon you will see how and as I mentioned earlier, we won't hear also push our new to-do in our heart code that the data that we have. But we will make sure to save our new to-do in our database. So for example, after my validation here, I could create the const of new todo and set it equal to something. As I wish to use my to-do model here, I will be able to call it with my new this, that Todo model here. When I call this, it expects also some value here. The value that I could pass could be my to-do. And these 22 refers to my to-do that I will pass well through my Postman. So from my Postman postman collection, I will pass them to do with proper title and proper completed value and hearing my to-do model, I will be able to generate the new to-do based on that here. Also when we generate that new todo, it will also at the end, generate our ID for our to-do. So with this, we already have our new todo, create it. Now how could we save our to-do in our database? Well, that's easy. We can just use new todo here and on it we will now have metal save because we used our Todo model here from mongoose on it. Well, the data that it will hold, there should also be the method called save. This save method will automatically save our data in our database. And as we connected our Mongoose package from our application to our database, it will immediately know where to save it. And don't worry, if you don't have a collection of to-dos in your database, Mongoose will create such collection automatically. It will just name it to those as well. How does Mongoose knows that? Well, first of all, we name the named our model to do here, Mongoose, we'll grab our model and turn all of the letters to be lowercased. So at the end it'll be to do lowercase. And at the end it will add S for plural. So which means that it will generate the to-dos collection for us. So let's return this back because we are referring to our model here. Now that we are saving our data. When we save our data, we can also get some kind of result back. That result in our use case here could be our to-do. Here I will create the coast of result and just set it equal to whatever we are getting from our new to do that save here. Now what can we do here? We can well, first of all, remove this return value. And here we can console.log our result I will use to do a result. And after that, I will use my own result here. Let's first of all check what this result will hold. Now also, I do have an error here, which is referring to, to, to do array here. Let's just remove this returned value from here. And also, I will go into my controller just to double-check if there is an error, there is an error. I will also remove my to do a ray from here as well. And now let's quickly test this. What will happen when we create our to-do right now? So now in our Postman, we can open up our create to-do method here. Well, our route, and we can see we do have our proper title and completed value, which is watching a movie and false. So now let's try and create our todo here. Send this here. There is no response here because we are not returning anything from our API. But if you would go to your local host where your terminal, you would see todo result lock the right here. And next to that there will be a promise value of pending, which means that at this point our promise is pending. Well, obviously we are not using promises here as well, and we will integrate promised pattern zone, but Mongoose itself, the save method of Mongoose, we'll use promise here. And obviously we can see that these pending currently. That also means if you visit your own cluster on your MongoDB, the dashboard, you will be able to browse collections right now. So just click on this browse collection. And here you will be able to see to-dos collection, as we mentioned earlier, and your first item in your to-dos collection. So you've successfully saved your first to do in your database. You can double-check the site and we can see that we have title of watching a movie, which is the correct title that we sent. And also completed value is false, which is the correct value that we send. First value here could be, well, it is underscore ID, which is object of ID for a mongoose, well, MongoDB, and here inside we can see a string of different characters. Well, this here is that specific ID that will be generated for our own to those. But as this save method, we'll use promise. Let's integrate our own promise pattern. Let's use async await feature here. And that way we will be able to get proper results from the save method and also logged that result and return it to our Postman. So here I will now use our sink. And here I will now await for this save method to finish, which means that my result will hold the proper value. So now here, when I returned my data, the type of data that I will return here will be now a Pyramus and I should pass some generic type here. I will just pass a string. Why a string? Well here now instead of returning, well, instead of console logging here, I will use return and I will use the result, that ID here, because now our result will be the object of our two data, well-to-do data, and it will hold our ID, title and completed value as we are having our Promise type of a string right here. Let's go into our controller and change here as well. Let's add the data that we'll come back to be our Pyramus. And it will be, which will hold the generic type of a string because the ID at the end is just a string. If after this call you wish to do something else in this method, makes sure also to integrate async await pattern here as well. So you could await, first of all, for your response from the save method and then continue to code down below. Now just save this Controller and let's test this again in my Postman. Now, instead of watching a movie, I will set watching a movie tool, for example, and send it here. Now back, what I'm getting is a string value of my ID, as that is correct, I can go back into my MongoDB and refresh my whole collection. We here have this at the top right corner, a button for refresh. Just click on that. And our collection of to-dos now should hold to those. First two do, holds the title of watching a movie, and the second todo holds the title of watching a movie too. So now with this, we successfully used our Mongoose for the first time to save our data. And we also integrated promise pattern for our own data. That way we are making sure to have a proper result from Mongoose once everything is finished and saved in our database. And we can have that result back and also have that as a response to send it back to our post-lab. If you would have your front end application. This is a good example to send this response to your front-end application. But with this, you saw the whole workflow with Mongoose and we will now continue going down through this service. We will now refactor each of our methods to use our Mongoose and MongoDB and not use our to-dos hardcoded data at all. 24. Getting all Todos from Database: So now that you saw how could you create your own data in your database? In our example, our to-dos, it is the time now to retrieve that data from database back into our application. So in this video we will work without getting to those method, which is a method to retrieve all of our to-dos, as you know currently to just return our heart coded the data. But now we will refactor that function and make sure to retrieve, well to return our watching the movie and watching a movie do to do is that we have in our database. Now in our to-dos service file, we will go down there where we have our get to those method. As you can see currently, we are returning the copy of our to-dos data, which is just this hard-coded the data. And these ones were currently the reason why it won't work well, we have our database now and we should return the data from our database. So to do that, instead of returning here these to-dos, we are able to access now our database and we are able to access it to the tower to do model as we did in our create Todo where we saved our data. So here I am now able to use this dot to do model and on it, I'm able to use all of these different methods. One such method is called Find, which is the method that you'll find specific to-do in our example. It won't find a specific to-do, but it will find all of the to-dos as we are not passing any parameters, decide if you would pass a specific perimeter. What the return just that specific to-do based on that perimeter. Now we obviously do have an error because here we are returning to do array. So we are expected to do well to return the array of to-dos. And that is not correct because from here we will return our promise. So here I will cut this to do array and use promise and set the generic type of these promised to be to do array. And next to that here to return a promise, I will need to use a sinc function. So now our function is a sink also here where you are returning. You cannot wait here, but this here just so you know, is a redundant bit of code because using the async and await for your return value, it is the return that bit of code because return will automatically expect to return a promise from your method if you are using us incremental, so you don't need to use a weight there. So now is a real returning also a promise of 22 array here. Let's copy this and go into our to-dos controller, because in our to-dos controller, we will have now error. We have the error because we are returning to do array here, which is not correct. So let's make sure that this is a promise off to do well off array of those. And that should be it. We are returning data here. We are returning data in our service with this find method from our database, we are able just to test that. The easiest way to test that is to go into our postmen and going to get to those better if I would run this method right now, we will see two to-dos here. We can see our watching a movie and watching a movie TO, which are both to-dos from our database. Next to that you can see this completed values which are both for both false and specific unique IDs for both of those, because those are, these are generated well from MongoDB and that is essentially it for returning our to-dos. Let's go into next video and find a way to return our specific to do. 25. Get Specific Todo from Database: Now we should find a way to return our specific to draw from our database. For example, currently we will loop through our to-dos, which is still hard coded the data in this file. And we will find the specific to-do based on its own ID. We can do the same, but with these fine the method from our Mongoose. So now for example here instead of returning to do from these to-dos array, we will remove this from here and we will return from our this dot Todo model. And only three, we'll have this find the method. And as I mentioned, we will use this Find method but in a different format. You can just below CDs find by ID method, which refers to a finding the to-do by its own ID. Well, unique ID that Mongoose, well, mongodb will set. So what can we do here is just make sure to call this function. And to this function we can pass our own to-do ID that we are passing here into the ghetto, don't matter. So this way we will pass our to-do ID here. And by this todo id, find by ID method will be able to find specific to do in our database and returning back to us once again, here we are having the same issue as we had for our get to those, because now we are trying to return here just to do data. And it won't be just to do, but it will be the promise with generic type of to-do. And also here we will have to set our arcing function quickly going now into our controller here where we have our get to do. Let's make sure that now here we are not returning to do but Pyramus off to do, okay, I mistyped here, so Pyramus and to do inside of this generic type, and this should be essentially, let's jump into our postman to test this. Now, in my postman right now, I will grab one of my to-do as well, one of the IDs. So let's grab this first one, which is watching a movie. And in my get to do where I'm having my URL here, localhost 3000 slash todos slash two. Instead of these two, I will pass my ID, specific ID to retrieve these specific Todo. So now I will send this. And what I'm getting back is only one to do, which is watching a movie. So we were able to find our specific to do by its own ID and retrieve it back from our database. So for now we successfully connected our post and get routes with our database. Let's move into the next video and see how could we delete a specific to-do from our database. 26. Delete Todo from Database: So as I mentioned in this video, we will delete our to-do from our database. So going to your service and find the method delete todo. As you know, currently, we are finding the specific index of our to-do and we will splice them to do by that index from our hardcoded data. This won't work this way anymore for this year specifically, we will remove all of these three lines of code and we will again use our Todo model for such a thing. We know that our delete todo should be a sink await function here, and it should return a promise. So here I could our weight. And what I wish to await is my this.tab Todo model. And on it I will have a method called delete one. This delete one method is able to delete one of my to-dos based on its own IDs. So as we are already passing here todo id, we are able to pass that ID to delete a specific to do from our database. One thing to notice here is that you don't really need to pass only your to-do ID here. You can have whatever field or set of fields that you wish to pass to delete your to-do and same goes to find your to-do as well. So here in our delete method, I will now pass an object because I'm able to set, well send those set of fields. And now I will pass the ID. I wish to set my to-do ID here, so I will send my unique to do ID for my to-do. And by that, I should be able to remove that specific to do for my database. Next, for this delete one, I will pass another method well, bind another method, chain another method to this delete one, which is called exec for execute this executable, just make sure that it will execute this specific query, which is our ID. And by that, we will delete our to-do also here now you can see that we are not returning anything. So I will hear returned hard-coded string which is V to do, which will be to do it deleted. The reason why I will return a hard-coded string like this is, well, mostly when we delete some data from our applications, we don't really wish to give the feedback back to a user which specific data it was deleted. But mostly we are sending back some success message like to do successfully deleted in our case to do deleted, which means that here now we want to return to do array. But when we return the promise of string and as we are using async await functionality here, the will indeed be able to return promise back. One last thing just to make sure is to go into our to-dos controller and we will see our arrow down below in our delete method. The reason why we have these salaries, because we are here also expecting to return our array of to-dos and we want to return that right now, but our promise of type of storing here. So now what can we simply do is just test our delete method. So for that, I will grab my unique ID from here, and I will grab the second one, which is watching a movie. To do so, as I grab that idea will go into my delete todo method. And here where I'm setting my URL of localhost 3000 slash slash one. Now instead of that one, I will paste that ID that I just copied over and I will dispatch these requests are sent. And we can see that we are having to do delete that message back, which means that we successfully deleted our to-do from our database. If we wish to double-check that, I will go into my get to those in MATLAB and try to get all of my to-dos back. If I tried to do that, we can see that we are getting back only the one to do here. So that means that we successfully deleted the other to do if you wish, you can also go to your database and refresher database just to be sure that you only have one to do in your database. So with this, you saw how easy is to delete well-to-do from your database. And now we are left off only with one or more route, which is to update our to-do. And let's do that in the next video. 27. Update Todo in Database: So guys, as you already know, we are almost finished with our to-do application. The only method that we need to do now is to update our to-do in our database. And for that, let's go into our code. So as you already know, we already have our two update to-do method in our to-do service. But this update to do method is not valid enough now for us because we are currently updating our hard-coded to those data instead of our data in a database. So let's change this. First of all, I will remove this return and also these two lines of code, I will leave these validation for now because validation here is good enough. What we can do here is get some kind of result from our update based on our Mongoose method. For that result, we will again use async await here. So our update to do should be a sync function. And here we will await our discs that to-do model and on it. As you already know, we had this method to delete one and find by ID. Mongoose also have a method called find by ID and update, which well basically it's own name says a lot. So this method will accept two parameters. First one will be our ID. Well, by that ID mongoose when low, which specific to two it should update. Here, I will pass my to-do ID and then we will also need to pass our body well for the update has to do so here, I will also pass the to-do from our, my update to the method that I have here. And with this, that would be all. Now that we have our result down below, we could return our result here, but instead of returning just results that will also return a message. So you can see that there is a difference here that we can return different things here in our, in our example, this customized object with a message or for example, todo, update it. And also next to that, I will pass my result here, which will be my to-do. Now that I have this here, let's also properly specify what type of Todo we wish to return here. Well, what type of response we will return from this update to do matter? So now here we will return the promise with generic type of sampling. That generic type will hold an object here, which will hold our message. And this message is obviously just a string, is these two to update, update is. And also the next thing is our result, which is the type of our Todo. So here you can now see that we do have proper promised selected, this error on my screen that you can see it does the prettier error. Well, my code could be formatted differently, so I will go to quick fix this code and fix this with prettier extension. And here it is. Now my code looks quite better. Now that we have this here, we are ready to go into our controller and just make sure that we are returning the same response in our controller. So here in our controller itself we can see the error here. Let's quickly fix this up. Instead of returning to do array here on our update to do method, we will return a promise with genetic type of object. And here we will pass two properties. First property, property will be our message, which is the type of string, and our result reaches the type of our todo. Again, same error here because we can format this code a bit differently, and here it is. Now everything is okay. And with this here, we are ready to test our update to do method. So first of all, I will go into my Bozeman and here I will send this get to those method. Once I have all of my to-dos, in my case, only one, I will grab the idea of these do and go into my update to do method that we have here. First of all, let's change our local host. Well, our URL here, we have our local host of 3000 sludge to do's slash one. Let's change that one to our proper specific unique ID in here, what we wish to change, let's say for example, the title could be wash your car, completed, can be set to true. So send these now and back. You will see this response. Now, the response will hold the message of todo update it, and it will hold the result with our updated to do. But obviously there is a problem because our title here is still watching a movie too and completed the set to false. Let's jump into database to confirm this in my database, I can quickly refresh my screen. And here what I can see is that my title here is a washer car and completed value is true, which means that my to-do was properly updated. Going back to my Postman, I will go back to get to those method, send that and we can confirm that our to-do was indeed updated. So there is a problem in our code probably. So let's grab the same ID and let's try to update, wash your car now once again, so update to do pass that ID which is already there. And now I will move this completed value to false. And let's send this request right now. What we can confirm here is that our completed is still true even though we are sending the false value. So let's debug this. Okay, so the issue here is following. First of all, we use these find by ID and update and mongoose just recently had some changes to these methods. So for example, instead of using find by ID and update, we will use find one and update. This method is quite similar to our find by ID. Why? Because the first parameter will be our object to specifically find our to-do. And for that we will send the ID of our todo, which will be our to-do ID that we are getting in this method. And next to that, the next value will be our to-do as up body. And also these find one n update method will just return the latest values for our todo, which in our example won't be the new value for our to-do. So here we can fix that as well because the third option that we can pass to this find one and update is options. So to this options, we can pass a new and set this to true. This way we will always get the new updated value for our to-do. And also let's move this down a bit. Here it is, so we could see it properly. And let's test this right now. So back into Postman. Let's change this to do that we wish to update entirely. So here we will say buy a milk. And instead of completed, we will set this to true. Well, instead of false, Let's set this to true. Send this now and we will see our proper change here. Title is indeed bio milk and completed value is true. So sorry about this misunderstanding as well as you can see, the Mongoose had some changes with their own documentation. So now everything should be okay. Let's just jump into the next video and test the entire application. 28. Test the Whole API: So as you know, we created our entire API for our to-dos application, and we did that with the nest JS technology with this. Now you can do a lot of things. If you are also front-end developer, you can connect your API that you just created with your frontend application and make sure to create, well, some nice design for this. That way you will have a full-stack application, fully developed, which is just the absolutely great portfolio project for you. Definitely next to that in my, let's call it Series, I also often do the deployment of the applications, but for this tutorial, I won't do such because we already deployed our data into our database. If you truly wish to deploy your API to some service like Heroku is, make sure to watch my weather application with NodeJS and x plus js. And there you will have the entire deployment process to Iraq. But now there is only one thing left and that is just to test our entire API. So going into my Postman, I will start from top to a borrower with my routes. So first of all, we have this create to-do, and here we do have our title. Let's set that title to watching a movie. Completed value for that will be false. So now let's dispatch this request. And we can see back our ID of that created to do to confirm that we created indeed these to do with this title, we can go into the next request, which is our get to do's, let's dispatch this request. And in our database we will now hold two different to those. First one is via milk and the second one is watching a movie. If I wish to get the second one watching a movie by specific ID, I can grab that ID and go into my get to do request here. And just at the top, I will replace the IDB team I just copied ID. So now we can dispatch this request and we will get back our watching a movie to do which we just created. Now going back to my get to those requests, I can grab this idea from my bio milk and let's go into update to do to update this by a milk. So here I will paste this proper ID, which is already set correctly here. And I will change the title for this to say wash your car. And also completed value will be false. Now, now when I have this bar is set for my update of to-do, let's dispatch this request and back. We will get our message with to-do updated and the whole result of our updated to do to double-check this, we can go again back into our get to those route and dispatch this request as well. In our request, we now don't see a buyer milk to do anymore but wash a car because V update at the studio. And the last thing that is left, I will grab this ID from wash a car, going to my delete todo request, paste that idea right here and send it. And to-do is indeed deleted. If I would go to get to those, once again, send this. And now instead of two to-dos, we have just one to do, which is watching a movie. So entire API is now properly tested and you can see that it works correctly. Once again, if you wish to deploy this API to Heroku as a service, for example, make sure to watch deployment process in my course where we deploy also weather application with knowledge AS and the expression S. But this will be all for this course, guys. Let's just jump into the next video where I will say you a proper goodbye. 29. Thank You and Goodbye: And this will be it, the guys. Congratulations. You created your own to-do application using a nested just technology in the Mongo database. I truly wish to thank you that you watch this course. And I also wish to thank you that you finished the entire course. As you know, in this course, we created the to-do application using a nest Jess and Mongo database. So to the integrity of the course, you saw what is nest, how to use nest, how lifecycle of Nest works, and also how to create the routes and handle the functionality for your own to-do application with the Mongo database, you created your own cluster. You were able to save, modified and get data with the mongoose module and also be together integrated the promise pattern. So it could be easier to modify and work with our data. So as you already know, this course belongs, but let's go to Series in there. Let's go and series is a series on Skillshare, where we together create our own real-life projects using a variety of different technologies. So next to this course, you can also find many other courses like English courses, multiple JavaScript courses, and Bootstrap courses as well. Next to Skillshare courses, you can also find on my YouTube channel called Amerigo. On that channel you can find the many other web development tutorials and entire courses. So if you would like more content from me, make sure to follow my Skillshare profile and also to subscribe to my YouTube channel. But this will be all for this course. Thank you once again, and I will see you in my other courses as well. Bye-bye.