Skip to content

Commit

Permalink
user must be authenticated before updating a todo item or task item
Browse files Browse the repository at this point in the history
  • Loading branch information
bhaidar committed Apr 13, 2019
1 parent 715e63a commit 04d1d2e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions server/src/todo/task/task.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class TaskController {
}

@Delete(':id')
@UseGuards(AuthGuard())
async destory(@Param('id') id: string): Promise<TaskDto> {
return await this.taskService.destoryTask(id);
}
Expand Down
4 changes: 3 additions & 1 deletion server/src/todo/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ export class TodoController {

@Put(':id')
@UsePipes(new ValidationPipe())
@UseGuards(AuthGuard())
async update(
@Param('id') id: string,
@Body() todoDto: TodoDto,
): Promise<TodoDto> {
return await this.todoService.updateTodo(todoDto);
return await this.todoService.updateTodo(id, todoDto);
}

@Delete(':id')
@UseGuards(AuthGuard())
async destory(@Param('id') id: string): Promise<TodoDto> {
return await this.todoService.destoryTodo(id);
}
Expand Down
13 changes: 8 additions & 5 deletions server/src/todo/todo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ export class TodoService {
return toTodoDto(todo);
}

async updateTodo(todoDto: TodoDto): Promise<TodoDto> {
const { id, name, description } = todoDto;
async updateTodo(id: string, todoDto: TodoDto): Promise<TodoDto> {
const { name, description } = todoDto;

let todo: TodoEntity = await this.todoRepo.findOne({ where: { id } });

Expand All @@ -72,22 +72,25 @@ export class TodoService {
}

todo = {
id: todo.id,
id,
name,
description,
};

await this.todoRepo.update({ id }, todo); // update

todo = await this.todoRepo.findOne({ where: { id }, relations: ['tasks'] }); // re-query
todo = await this.todoRepo.findOne({
where: { id },
relations: ['tasks', 'owner'],
}); // re-query

return toTodoDto(todo);
}

async destoryTodo(id: string): Promise<TodoDto> {
const todo: TodoEntity = await this.todoRepo.findOne({
where: { id },
relations: ['tasks'],
relations: ['tasks', 'owner'],
});

if (!todo) {
Expand Down

0 comments on commit 04d1d2e

Please sign in to comment.