Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add task crud #2

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion 02-nestjs-basics/01-tasks-controller/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default {
module.exports = {
moduleFileExtensions: ["js", "json", "ts"],
rootDir: ".",
testEnvironment: "node",
Expand Down
1 change: 0 additions & 1 deletion 02-nestjs-basics/01-tasks-controller/package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
20 changes: 15 additions & 5 deletions 02-nestjs-basics/01-tasks-controller/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
74 changes: 68 additions & 6 deletions 02-nestjs-basics/01-tasks-controller/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -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;
}
}
4 changes: 3 additions & 1 deletion 02-nestjs-basics/02-filtering/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
27 changes: 21 additions & 6 deletions 02-nestjs-basics/02-filtering/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Injectable } from "@nestjs/common";
import { Injectable, BadRequestException } from "@nestjs/common";
import { Task, TaskStatus } from "./task.model";

@Injectable()
Expand Down Expand Up @@ -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);
}
}
Loading