-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
446 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); |
Oops, something went wrong.