From 0648236b7c35790cb5ebd8bc853aea856c753eaa Mon Sep 17 00:00:00 2001 From: Shaalan Marwan Date: Fri, 29 Apr 2022 04:16:11 +0300 Subject: [PATCH] apply filters on get tasks --- src/tasks/dto/get-tasks.dto.ts | 6 ++++++ src/tasks/dto/update-task.dto.ts | 4 ++-- src/tasks/task.model.ts | 4 ++-- src/tasks/tasks.controller.ts | 6 ++++-- src/tasks/tasks.service.ts | 35 ++++++++++++++++++++++++++++---- 5 files changed, 45 insertions(+), 10 deletions(-) create mode 100644 src/tasks/dto/get-tasks.dto.ts diff --git a/src/tasks/dto/get-tasks.dto.ts b/src/tasks/dto/get-tasks.dto.ts new file mode 100644 index 0000000..ef58d69 --- /dev/null +++ b/src/tasks/dto/get-tasks.dto.ts @@ -0,0 +1,6 @@ +import { TasksStatusEnum } from 'src/tasks/task.model'; + +export class GetTasksDto { + status?: TasksStatusEnum; + q?: string; +} diff --git a/src/tasks/dto/update-task.dto.ts b/src/tasks/dto/update-task.dto.ts index 7ce0bdb..9264e4a 100644 --- a/src/tasks/dto/update-task.dto.ts +++ b/src/tasks/dto/update-task.dto.ts @@ -1,5 +1,5 @@ -import { TasksEnum } from 'src/tasks/task.model'; +import { TasksStatusEnum } from 'src/tasks/task.model'; export class UpdateTaskDto { - status: TasksEnum; + status: TasksStatusEnum; } diff --git a/src/tasks/task.model.ts b/src/tasks/task.model.ts index 9f48de1..0b057db 100644 --- a/src/tasks/task.model.ts +++ b/src/tasks/task.model.ts @@ -2,10 +2,10 @@ export interface Task { id: string; title: string; description: string; - status: TasksEnum; + status: TasksStatusEnum; } -export enum TasksEnum { +export enum TasksStatusEnum { OPEN = 'open', IN_PROGRESS = 'in_progress', DONE = 'done', diff --git a/src/tasks/tasks.controller.ts b/src/tasks/tasks.controller.ts index 7f47829..f173bf8 100644 --- a/src/tasks/tasks.controller.ts +++ b/src/tasks/tasks.controller.ts @@ -7,8 +7,10 @@ import { ParseIntPipe, Patch, Post, + Query, } from '@nestjs/common'; import { CreateTaskDto } from 'src/tasks/dto/create-task.dto'; +import { GetTasksDto } from 'src/tasks/dto/get-tasks.dto'; import { UpdateTaskDto } from 'src/tasks/dto/update-task.dto'; import { Task } from 'src/tasks/task.model'; import { TasksService } from 'src/tasks/tasks.service'; @@ -21,8 +23,8 @@ export class TasksController { return this.tasksService.createTask(createTaskDto); } @Get() - getTasks(): Task[] { - return this.tasksService.getTasks(); + getAllTasks(@Query() filterDto: GetTasksDto): Task[] { + return this.tasksService.getTasks(filterDto); } @Get(':id') diff --git a/src/tasks/tasks.service.ts b/src/tasks/tasks.service.ts index 59c40fe..977ee1f 100644 --- a/src/tasks/tasks.service.ts +++ b/src/tasks/tasks.service.ts @@ -1,16 +1,43 @@ import { Injectable } from '@nestjs/common'; import { CreateTaskDto } from 'src/tasks/dto/create-task.dto'; +import { GetTasksDto } from 'src/tasks/dto/get-tasks.dto'; import { UpdateTaskDto } from 'src/tasks/dto/update-task.dto'; -import { Task, TasksEnum } from 'src/tasks/task.model'; +import { Task, TasksStatusEnum } from 'src/tasks/task.model'; import { v4 as uuid } from 'uuid'; @Injectable() export class TasksService { private tasks: Task[] = []; - getTasks(): Task[] { - return this.tasks; + getTasks(filterDto: GetTasksDto): Task[] { + const { status, q } = filterDto; + let tasks = this.tasks; + + if (status) { + tasks = tasks.filter((task) => task.status === status); + } + + if (q) { + tasks = tasks.filter( + (task) => task.title.includes(q) || task.description.includes(q), + ); + } + + return tasks; } + // getTasks(getTasksDto: GetTasksDto): Task[] { + // const { q, status } = getTasksDto; + + // return this.tasks.filter( + // (task) => + // (status !== undefined && + // task.status === status && + // q !== undefined && + // task.description.includes(q)) || + // task.title.includes(q), + // ); + // } + getTaskById(id: string): Task { return this.tasks.find((task) => task.id === id); } @@ -30,7 +57,7 @@ export class TasksService { id: uuid(), title, description, - status: TasksEnum.OPEN, + status: TasksStatusEnum.OPEN, }; this.tasks.push(task); return task;