Skip to content

Commit

Permalink
apply filters on get tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed Apr 29, 2022
1 parent 9ab2b81 commit 0648236
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/tasks/dto/get-tasks.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TasksStatusEnum } from 'src/tasks/task.model';

export class GetTasksDto {
status?: TasksStatusEnum;
q?: string;
}
4 changes: 2 additions & 2 deletions src/tasks/dto/update-task.dto.ts
Original file line number Diff line number Diff line change
@@ -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;
}
4 changes: 2 additions & 2 deletions src/tasks/task.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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')
Expand Down
35 changes: 31 additions & 4 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -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);
}
Expand All @@ -30,7 +57,7 @@ export class TasksService {
id: uuid(),
title,
description,
status: TasksEnum.OPEN,
status: TasksStatusEnum.OPEN,
};
this.tasks.push(task);
return task;
Expand Down

0 comments on commit 0648236

Please sign in to comment.