From 9ab2b81eda945a5f26b19d8241061254a0136b46 Mon Sep 17 00:00:00 2001 From: Shaalan Marwan Date: Fri, 29 Apr 2022 02:19:22 +0300 Subject: [PATCH] add Patch taks --- src/tasks/dto/update-task.dto.ts | 5 +++++ src/tasks/tasks.controller.ts | 17 +++++++++++------ src/tasks/tasks.service.ts | 21 ++++++++++++++++----- 3 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 src/tasks/dto/update-task.dto.ts diff --git a/src/tasks/dto/update-task.dto.ts b/src/tasks/dto/update-task.dto.ts new file mode 100644 index 0000000..7ce0bdb --- /dev/null +++ b/src/tasks/dto/update-task.dto.ts @@ -0,0 +1,5 @@ +import { TasksEnum } from 'src/tasks/task.model'; + +export class UpdateTaskDto { + status: TasksEnum; +} diff --git a/src/tasks/tasks.controller.ts b/src/tasks/tasks.controller.ts index ebc1e5a..7f47829 100644 --- a/src/tasks/tasks.controller.ts +++ b/src/tasks/tasks.controller.ts @@ -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(); @@ -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); + } } diff --git a/src/tasks/tasks.service.ts b/src/tasks/tasks.service.ts index 584dda8..59c40fe 100644 --- a/src/tasks/tasks.service.ts +++ b/src/tasks/tasks.service.ts @@ -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() @@ -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 = { @@ -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); } }