Skip to content

Commit

Permalink
feat: 🎸 add todo api
Browse files Browse the repository at this point in the history
  • Loading branch information
yeukfei02 committed Apr 21, 2022
1 parent 3727f98 commit 0ee48e3
Show file tree
Hide file tree
Showing 14 changed files with 446 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { LocationModule } from './location/location.module';
import { PostModule } from './post/post.module';
import { TagModule } from './tag/tag.module';
import { CommentModule } from './comment/comment.module';
import { TodoModule } from './todo/todo.module';

@Module({
imports: [
Expand All @@ -16,6 +17,7 @@ import { CommentModule } from './comment/comment.module';
PostModule,
TagModule,
CommentModule,
TodoModule,
],
controllers: [AppController],
providers: [AppService],
Expand Down
9 changes: 9 additions & 0 deletions src/todo/dto/createTodo.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';

export class CreateTodoDto {
@ApiProperty()
todo: string;

@ApiProperty()
users_id: string;
}
9 changes: 9 additions & 0 deletions src/todo/dto/updateTodo.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ApiProperty } from '@nestjs/swagger';

export class UpdateTodoDto {
@ApiProperty()
todo: string;

@ApiProperty()
users_id: string;
}
29 changes: 29 additions & 0 deletions src/todo/response/createTodo.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApiProperty } from '@nestjs/swagger';

export class Todo {
@ApiProperty()
id: string;

@ApiProperty()
todo: string;

@ApiProperty()
created_at: string;

@ApiProperty()
updated_at: string;

@ApiProperty()
users_id: string;

@ApiProperty()
users: { [key: string]: string };
}

export class CreateTodoResponse {
@ApiProperty()
message: string;

@ApiProperty()
todo: Todo;
}
29 changes: 29 additions & 0 deletions src/todo/response/deleteTodoById.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApiProperty } from '@nestjs/swagger';

export class Todo {
@ApiProperty()
id: string;

@ApiProperty()
todo: string;

@ApiProperty()
created_at: string;

@ApiProperty()
updated_at: string;

@ApiProperty()
users_id: string;

@ApiProperty()
users: { [key: string]: string };
}

export class DeleteTodoByIdResponse {
@ApiProperty()
message: string;

@ApiProperty()
todo: Todo;
}
29 changes: 29 additions & 0 deletions src/todo/response/getTodoById.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApiProperty } from '@nestjs/swagger';

export class Todo {
@ApiProperty()
id: string;

@ApiProperty()
todo: string;

@ApiProperty()
created_at: string;

@ApiProperty()
updated_at: string;

@ApiProperty()
users_id: string;

@ApiProperty()
users: { [key: string]: string };
}

export class GetTodoByIdResponse {
@ApiProperty()
message: string;

@ApiProperty()
todo: Todo;
}
35 changes: 35 additions & 0 deletions src/todo/response/getTodos.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ApiProperty } from '@nestjs/swagger';

export class Data {
@ApiProperty()
id: string;

@ApiProperty()
todo: string;

@ApiProperty()
created_at: string;

@ApiProperty()
updated_at: string;

@ApiProperty()
users_id: string;
}

export class GetTodosResponse {
@ApiProperty()
message: string;

@ApiProperty({ default: [], isArray: true })
data: Data[];

@ApiProperty()
total: number;

@ApiProperty()
page: number;

@ApiProperty()
limit: number;
}
29 changes: 29 additions & 0 deletions src/todo/response/updateTodoById.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { ApiProperty } from '@nestjs/swagger';

export class Todo {
@ApiProperty()
id: string;

@ApiProperty()
todo: string;

@ApiProperty()
created_at: string;

@ApiProperty()
updated_at: string;

@ApiProperty()
users_id: string;

@ApiProperty()
users: { [key: string]: string };
}

export class UpdateTodoByIdResponse {
@ApiProperty()
message: string;

@ApiProperty()
todo: Todo;
}
18 changes: 18 additions & 0 deletions src/todo/todo.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TodoController } from './todo.controller';

describe('TodoController', () => {
let controller: TodoController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [TodoController],
}).compile();

controller = module.get<TodoController>(TodoController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});
});
135 changes: 135 additions & 0 deletions src/todo/todo.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
Controller,
Post,
Get,
Put,
Delete,
Body,
Param,
Query,
} from '@nestjs/common';
import { TodoService } from './todo.service';
import { CreateTodoDto } from './dto/createTodo.dto';
import { UpdateTodoDto } from './dto/updateTodo.dto';

import {
ApiBearerAuth,
ApiHeader,
ApiQuery,
ApiResponse,
} from '@nestjs/swagger';
import { CreateTodoResponse } from './response/createTodo.response';
import { GetTodosResponse } from './response/getTodos.response';
import { GetTodoByIdResponse } from './response/getTodoById.response';
import { UpdateTodoByIdResponse } from './response/updateTodoById.response';
import { DeleteTodoByIdResponse } from './response/deleteTodoById.response';

@ApiBearerAuth()
@ApiHeader({
name: 'Authorization',
description: 'Jwt Token',
})
@Controller('todo')
export class TodoController {
constructor(private readonly todoService: TodoService) {}

@Post()
@ApiResponse({
status: 201,
description: 'Successful response',
type: CreateTodoResponse,
})
async createTodo(@Body() createTodoDto: CreateTodoDto): Promise<any> {
const todo = await this.todoService.createTodo(createTodoDto);

const response = { message: 'createTodo', todo: todo };
return response;
}

@Get()
@ApiQuery({
name: 'users_id',
description: 'users_id',
required: false,
type: String,
})
@ApiQuery({
name: 'page',
description: 'page',
required: false,
type: String,
})
@ApiQuery({
name: 'per_page',
description: 'per_page',
required: false,
type: String,
})
@ApiResponse({
status: 200,
description: 'Successful response',
type: GetTodosResponse,
})
async getTodos(
@Query('users_id') users_id: string,
@Query('page') page: string,
@Query('per_page') perPage: string,
): Promise<any> {
const usersId = users_id;
const pageInt = page ? parseInt(page, 10) : 1;
const perPageInt = page ? parseInt(perPage, 10) : 20;

const todos = await this.todoService.getTodos(usersId, pageInt, perPageInt);

const response = {
message: 'getTodos',
data: todos,
total: todos.length,
page: pageInt,
limit: perPageInt,
};
return response;
}

@Get('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: GetTodoByIdResponse,
})
async getTodoById(@Param('id') id: string): Promise<any> {
const todo = await this.todoService.getTodoById(id);

const response = { message: 'getTodoById', todo: todo };
return response;
}

@Put('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: UpdateTodoByIdResponse,
})
async updateTodoById(
@Param('id') id: string,
@Body() updateTodoDto: UpdateTodoDto,
): Promise<any> {
const todo = await this.todoService.updateTodoById(id, updateTodoDto);

const response = { message: 'updateTodoById', todo: todo };
return response;
}

@Delete('/:id')
@ApiResponse({
status: 200,
description: 'Successful response',
type: DeleteTodoByIdResponse,
})
async deleteTodoById(@Param('id') id: string): Promise<any> {
const todo = await this.todoService.deleteTodoById(id);

const response = { message: 'deleteTodoById', todo: todo };
return response;
}
}
16 changes: 16 additions & 0 deletions src/todo/todo.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Module, MiddlewareConsumer } from '@nestjs/common';
import { TodoController } from './todo.controller';
import { TodoService } from './todo.service';
import { PrismaService } from '../prisma.service';
import { AuthMiddleware } from '../auth.middleware';

@Module({
imports: [],
controllers: [TodoController],
providers: [TodoService, PrismaService],
})
export class TodoModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(AuthMiddleware).forRoutes('todo');
}
}
18 changes: 18 additions & 0 deletions src/todo/todo.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { TodoService } from './todo.service';

describe('TodoService', () => {
let service: TodoService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [TodoService],
}).compile();

service = module.get<TodoService>(TodoService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
Loading

0 comments on commit 0ee48e3

Please sign in to comment.