From f6facacbafe3a6787210e93d9c0115cb66a37331 Mon Sep 17 00:00:00 2001 From: Dimash Date: Mon, 9 Dec 2024 16:44:14 +0500 Subject: [PATCH 1/2] feat: add task crud --- .../01-tasks-controller/jest.config.js | 2 +- .../01-tasks-controller/package.json | 1 - .../tasks/tasks.controller.ts | 20 +++-- .../tasks/tasks.service.ts | 74 +++++++++++++++++-- 4 files changed, 84 insertions(+), 13 deletions(-) diff --git a/02-nestjs-basics/01-tasks-controller/jest.config.js b/02-nestjs-basics/01-tasks-controller/jest.config.js index 6a9c5cc..d212c5c 100644 --- a/02-nestjs-basics/01-tasks-controller/jest.config.js +++ b/02-nestjs-basics/01-tasks-controller/jest.config.js @@ -1,4 +1,4 @@ -export default { +module.exports = { moduleFileExtensions: ["js", "json", "ts"], rootDir: ".", testEnvironment: "node", diff --git a/02-nestjs-basics/01-tasks-controller/package.json b/02-nestjs-basics/01-tasks-controller/package.json index ba844fb..1bb8bbe 100644 --- a/02-nestjs-basics/01-tasks-controller/package.json +++ b/02-nestjs-basics/01-tasks-controller/package.json @@ -1,5 +1,4 @@ { - "type": "module", "scripts": { "start": "nest start --watch", "test": "cross-env NODE_OPTIONS=\"$NODE_OPTIONS --no-warnings --experimental-vm-modules\" NODE_ENV=test jest --config jest.config.js --runInBand" diff --git a/02-nestjs-basics/01-tasks-controller/tasks/tasks.controller.ts b/02-nestjs-basics/01-tasks-controller/tasks/tasks.controller.ts index 013e8d3..13f1196 100644 --- a/02-nestjs-basics/01-tasks-controller/tasks/tasks.controller.ts +++ b/02-nestjs-basics/01-tasks-controller/tasks/tasks.controller.ts @@ -15,17 +15,27 @@ export class TasksController { constructor(private readonly tasksService: TasksService) {} @Get() - getAllTasks() {} + getAllTasks() { + return this.tasksService.getAllTasks(); + } @Get(":id") - getTaskById(@Param("id") id: string) {} + getTaskById(@Param("id") id: string) { + return this.tasksService.getTaskById(id); + } @Post() - createTask(@Body() task: Task) {} + createTask(@Body() task: Task) { + return this.tasksService.createTask(task); + } @Patch(":id") - updateTask(@Param("id") id: string, @Body() task: Task) {} + updateTask(@Param("id") id: string, @Body() task: Task) { + return this.tasksService.updateTask(id, task); + } @Delete(":id") - deleteTask(@Param("id") id: string) {} + deleteTask(@Param("id") id: string) { + return this.tasksService.deleteTask(id); + } } diff --git a/02-nestjs-basics/01-tasks-controller/tasks/tasks.service.ts b/02-nestjs-basics/01-tasks-controller/tasks/tasks.service.ts index 8036598..3da9d5a 100644 --- a/02-nestjs-basics/01-tasks-controller/tasks/tasks.service.ts +++ b/02-nestjs-basics/01-tasks-controller/tasks/tasks.service.ts @@ -1,17 +1,79 @@ -import { Injectable, NotFoundException } from "@nestjs/common"; +import { + Injectable, + NotFoundException, + BadRequestException, +} from "@nestjs/common"; import { Task } from "./task.model"; +import { TaskStatus } from "./task.model"; @Injectable() export class TasksService { private tasks: Task[] = []; - getAllTasks(): Task[] {} + getAllTasks(): Task[] { + return this.tasks; + } - getTaskById(id: string): Task {} + getTaskById(id: string): Task { + const taskById = this.tasks.find((task) => task.id == id); - createTask(task: Task): Task {} + if (!taskById) { + throw new NotFoundException(`Resource with ID ${id} not found`); + } - updateTask(id: string, update: Task): Task {} + return taskById; + } - deleteTask(id: string): Task {} + createTask(task: Task): Task { + if (typeof task.status != "string" || typeof task.description != "string") { + throw new BadRequestException("Not string"); + } + + if (!Object.values(TaskStatus).includes(task.status)) { + throw new BadRequestException("Not right status"); + } + + const newTask = { id: Math.random().toString(), ...task }; + this.tasks.push(newTask); + return newTask; + } + + updateTask(id: string, update: Task): Task { + if ( + typeof update.status != "string" || + typeof update.description != "string" + ) { + throw new BadRequestException("Not string"); + } + + if (!Object.values(TaskStatus).includes(update.status)) { + throw new BadRequestException("Not right status"); + } + + const taskIndexById = this.tasks.findIndex((task) => task.id == id); + + if (taskIndexById === -1) { + throw new NotFoundException(`Resource with ID ${id} not found`); + } + + this.tasks[taskIndexById] = { ...this.tasks[taskIndexById], ...update }; + + return this.tasks[taskIndexById]; + } + + deleteTask(id: string): Task { + const taskIndexById = this.tasks.findIndex((task) => task.id == id); + + if (taskIndexById === -1) { + throw new NotFoundException(`Resource with ID ${id} not found`); + } + + if (taskIndexById === -1) { + throw new NotFoundException(`Task with ID ${id} not found`); + } + + const [deletedTask] = this.tasks.splice(taskIndexById, 1); + + return deletedTask; + } } From 98caa06d23bb9fbf120400036293f895545e6fd1 Mon Sep 17 00:00:00 2001 From: Dimash Date: Tue, 10 Dec 2024 17:16:03 +0500 Subject: [PATCH 2/2] feat: add filter --- .../02-filtering/tasks/tasks.controller.ts | 4 ++- .../02-filtering/tasks/tasks.service.ts | 27 ++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/02-nestjs-basics/02-filtering/tasks/tasks.controller.ts b/02-nestjs-basics/02-filtering/tasks/tasks.controller.ts index 088f979..1aadbc4 100644 --- a/02-nestjs-basics/02-filtering/tasks/tasks.controller.ts +++ b/02-nestjs-basics/02-filtering/tasks/tasks.controller.ts @@ -11,5 +11,7 @@ export class TasksController { @Query("status") status?: TaskStatus, @Query("page") page?: number, @Query("limit") limit?: number, - ) {} + ) { + return this.tasksService.getFilteredTasks(status, page, limit); + } } diff --git a/02-nestjs-basics/02-filtering/tasks/tasks.service.ts b/02-nestjs-basics/02-filtering/tasks/tasks.service.ts index a7b04c4..da3e78b 100644 --- a/02-nestjs-basics/02-filtering/tasks/tasks.service.ts +++ b/02-nestjs-basics/02-filtering/tasks/tasks.service.ts @@ -1,4 +1,4 @@ -import { Injectable } from "@nestjs/common"; +import { Injectable, BadRequestException } from "@nestjs/common"; import { Task, TaskStatus } from "./task.model"; @Injectable() @@ -36,9 +36,24 @@ export class TasksService { }, ]; - getFilteredTasks( - status?: TaskStatus, - page?: number, - limit?: number, - ): Task[] {} + getFilteredTasks(status?: TaskStatus, page = 1, limit = 10): Task[] { + if (Number(page) < 0 || Number(limit) < 0) { + throw new BadRequestException("Page or limit is not correct"); + } + + let filteredTasks = this.tasks; + + if (status) { + if (!Object.values(TaskStatus).includes(status)) { + throw new BadRequestException("Status is not correct"); + } + + filteredTasks = filteredTasks.filter((task) => task.status === status); + } + + const start = (Number(page) - 1) * Number(limit); + const end = start + Number(limit); + + return filteredTasks.slice(start, end); + } }