Skip to content

Instantly share code, notes, and snippets.

@dustinmyers
Created April 26, 2022 14:58
Show Gist options
  • Save dustinmyers/f227b4669f37e3b28e0f8489bca3a06b to your computer and use it in GitHub Desktop.
Save dustinmyers/f227b4669f37e3b28e0f8489bca3a06b to your computer and use it in GitHub Desktop.
Todo App example in NestJS - a Node framework inspired heavily by Angular
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Put,
} from '@nestjs/common';
import { toPromise } from 'src/shared/utils';
import { CreateTodoDto } from './dto/create-todo.dto';
import { TodoListDto } from './dto/rodoList.dto';
import { TodoDto } from './dto/todo.dto';
import { TodoService } from './todo.service';
@Controller('api/todos')
export class TodoController {
constructor(private readonly todoService: TodoService) {}
@Get()
async findAll(): Promise<TodoListDto> {
const todos = await this.todoService.getAllTodos();
return toPromise({ todos });
}
@Get()
async findOne(@Param('id') id: string): Promise<TodoDto> {
return await this.todoService.getOneTodo(id);
}
@Put(':id')
async update(
@Param('id') id: string,
@Body() todoDto: TodoDto,
): Promise<TodoDto> {
return await this.todoService.updateTodo(todoDto);
}
}
import { TaskEntity } from '@task/entities/task.entity';
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
ManyToOne,
OneToMany,
} from 'typeorm';
@Entity('todo')
export class TodoEntity {
@PrimaryGeneratedColumn('uuid') id: string;
@Column({ type: 'varchar', nullable: false }) name: string;
@Column({ type: 'text', nullable: false }) description?: string;
@CreateDateColumn() createdOn?: Date;
@CreateDateColumn() updatedOn?: Date;
@OneToMany(() => TaskEntity, (task) => task.todo) tasks?: TaskEntity[];
}
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TodoController } from './todo.controller';
import { TodoService } from './todo.service';
import { TodoEntity } from '@todo/entity/todo.entity';
import { TaskEntity } from '@task/entities/task.entity';
@Module({
imports: [TypeOrmModule.forFeature([TodoEntity, TaskEntity])],
controllers: [TodoController],
providers: [TodoService],
})
export class TodoModule {}
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { v4 as uuid } from 'uuid';
import { todos } from 'src/mock.todos.mock';
import { TodoEntity } from '@todo/entity/todo.entity';
import { TodoDto } from './dto/todo.dto';
import { toPromise } from 'src/shared/utils';
import { toTodoDto } from 'src/shared/mapper';
import { TodoListDto } from './dto/rodoList.dto';
import { CreateTodoDto } from './dto/create-todo.dto';
@Injectable()
export class TodoService {
todos: TodoEntity[] = todos;
async getAllTodos(): Promise<TodoDto[]> {
return todos.map((todo) => toTodoDto(todo));
}
async getOneTodo(id: string): Promise<TodoDto> {
const todo = this.todos.find((todo) => todo.id === id);
if (!todo) {
throw new HttpException(
`Todo item doesn't exist`,
HttpStatus.BAD_REQUEST,
);
}
return toPromise(toTodoDto(todo));
}
async createTodo(createTodoDto: CreateTodoDto): Promise<TodoDto> {
const { name, description } = createTodoDto;
const todo: TodoEntity = {
id: uuid(),
name,
description,
};
this.todos.push(todo);
return toPromise(toTodoDto(todo));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment