Skip to content

Commit

Permalink
add Patch taks
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed Apr 28, 2022
1 parent c98102f commit 9ab2b81
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 11 deletions.
5 changes: 5 additions & 0 deletions src/tasks/dto/update-task.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TasksEnum } from 'src/tasks/task.model';

export class UpdateTaskDto {
status: TasksEnum;
}
17 changes: 11 additions & 6 deletions src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,21 @@ import {
Get,
Param,
ParseIntPipe,
Patch,
Post,
} from '@nestjs/common';
import { CreateTaskDto } from 'src/tasks/dto/create-task.dto';
import { UpdateTaskDto } from 'src/tasks/dto/update-task.dto';
import { Task } from 'src/tasks/task.model';
import { TasksService } from 'src/tasks/tasks.service';

@Controller('tasks')
export class TasksController {
constructor(private tasksService: TasksService) {}

@Post()
createTask(@Body() createTaskDto: CreateTaskDto) {
return this.tasksService.createTask(createTaskDto);
}
@Get()
getTasks(): Task[] {
return this.tasksService.getTasks();
Expand All @@ -25,13 +30,13 @@ export class TasksController {
return this.tasksService.getTaskById(id);
}

@Post()
createTask(@Body() createTaskDto: CreateTaskDto) {
return this.tasksService.createTask(createTaskDto);
}

@Delete(':id')
deleteTask(@Param('id') id: string): void {
return this.tasksService.deleteTask(id);
}

@Patch(':id')
updateTask(@Param('id') id: string, @Body() updateTaskDto: UpdateTaskDto) {
return this.tasksService.updateTask(id, updateTaskDto);
}
}
21 changes: 16 additions & 5 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Injectable } from '@nestjs/common';
import { CreateTaskDto } from 'src/tasks/dto/create-task.dto';
import { UpdateTaskDto } from 'src/tasks/dto/update-task.dto';
import { Task, TasksEnum } from 'src/tasks/task.model';
import { v4 as uuid } from 'uuid';
@Injectable()
Expand All @@ -14,6 +15,15 @@ export class TasksService {
return this.tasks.find((task) => task.id === id);
}

updateTask(id: string, updateTaskDto: UpdateTaskDto) {
const { status } = updateTaskDto;
const tasks = [...this.tasks];
const updatedIndex = this.tasks.findIndex((task) => task.id === id);
tasks[updatedIndex].status = status;
this.tasks = tasks;
return tasks[updatedIndex];
}

createTask(createTaskDto: CreateTaskDto): Task {
const { title, description } = createTaskDto;
const task: Task = {
Expand All @@ -26,10 +36,11 @@ export class TasksService {
return task;
}
deleteTask(id: string): void {
const deletedIndex = this.tasks.findIndex((task) => task.id === id);
// if (deletedIndex === -1) {
// throw new Error('not found');
// }
this.tasks.splice(deletedIndex, 1);
this.tasks = this.tasks.filter((task) => task.id !== id);
// const deletedIndex = this.tasks.findIndex((task) => task.id === id);
// // if (deletedIndex === -1) {
// // throw new Error('not found');
// // }
// this.tasks.splice(deletedIndex, 1);
}
}

0 comments on commit 9ab2b81

Please sign in to comment.