Creating a simple To-Do list application involves setting up a basic CRUD (Create, Read, Update, Delete) functionality. Below is a simplified example of how you might structure such an application using NestJS, a progressive Node.js framework.\n\n### Step 1: Setting Up Your NestJS Project\n\nFirst, you need to install NestJS CLI if you haven't already:\n\nbash\nnpm i -g @nestjs/cli\n
\n\nThen, create a new project:\n\nbash\nnest new todo-list\n
\n\nNavigate to your project directory:\n\nbash\ncd todo-list\n
\n\n### Step 2: Creating a Todo Module\n\nGenerate a module for your todo items:\n\nbash\nnest g module todos\n
\n\n### Step 3: Creating a Todo Service\n\nGenerate a service for handling business logic:\n\nbash\nnest g service todos\n
\n\n### Step 4: Creating a Todo Controller\n\nGenerate a controller for handling HTTP requests:\n\nbash\nnest g controller todos\n
\n\n### Step 5: Defining the Todo Model\n\nIn your todos
module, you might define a simple interface for a Todo item. Create a todo.model.ts
file:\n\ntypescript\n// src/todos/todo.model.ts\nexport interface Todo {\n id: string;\n title: string;\n isCompleted: boolean;\n}\n\n
\n\n### Step 6: Implementing the Service\n\nEdit your todos.service.ts
to manage a list of todos in memory:\n\ntypescript\n// src/todos/todos.service.ts\nimport { Injectable } from '@nestjs/common';\nimport { Todo } from './todo.model';\nimport { v4 as uuid } from 'uuid';\n\n@Injectable()\nexport class TodosService {\n private todos: Todo[] = [];\n\n getAllTodos(): Todo[] {\n return this.todos;\n }\n\n createTodo(title: string): Todo {\n const newTodo: Todo = {\n id: uuid(),\n title,\n isCompleted: false,\n };\n\n this.todos.push(newTodo);\n return newTodo;\n }\n\n // Add more methods for update and delete as needed\n}\n
\n\n### Step 7: Implementing the Controller\n\nEdit your todos.controller.ts
to handle HTTP requests using the service:\n\ntypescript\n// src/todos/todos.controller.ts\nimport { Controller, Get, Post, Body } from '@nestjs/common';\nimport { TodosService } from './todos.service';\nimport { Todo } from './todo.model';\n\n@Controller('todos')\nexport class TodosController {\n constructor(private todosService: TodosService) {}\n\n @Get()\n getAllTodos(): Todo[] {\n return this.todosService.getAllTodos();\n }\n\n @Post()\n createTodo(@Body('title') title: string): Todo {\n return this.todosService.createTodo(title);\n }\n\n // Add more routes for update and delete as needed\n}\n
\n\n### Step 8: Testing Your Application\n\nYou can run your NestJS application by running:\n\nbash\nnpm run start\n
\n\nNow, you can test your To-Do API using Postman or any other API testing tool by sending requests to http://localhost:3000/todos
.\n\nThis example provides a basic foundation. Depending on your requirements, you might want to add additional functionality, such as persistence with a database, validation, authentication, etc.
henihaddad / workshop-enit Goto Github PK
View Code? Open in Web Editor NEWthis repo is for a git workshop