Skip to content

Commit

Permalink
refactor services to repository
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed Apr 30, 2022
1 parent abe1d04 commit 7d16a14
Show file tree
Hide file tree
Showing 9 changed files with 75 additions and 90 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
"rimraf": "^3.0.2",
"rxjs": "^7.5.5",
"tslib": "^2.4.0",
"typeorm": "^0.2.34",
"uuid": "^8.3.2"
"typeorm": "^0.2.34"
},
"devDependencies": {
"@nestjs/cli": "^7.6.0",
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/dto/get-tasks.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { TasksStatusEnum } from 'src/tasks/task.model';
import { TasksStatusEnum } from 'src/tasks/task-status.enum';

export class GetTasksDto {
@IsOptional()
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/dto/update-task.dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { IsEnum } from 'class-validator';
import { TasksStatusEnum } from 'src/tasks/task.model';
import { TasksStatusEnum } from 'src/tasks/task-status.enum';

export class UpdateTaskDto {
@IsEnum(TasksStatusEnum)
Expand Down
5 changes: 5 additions & 0 deletions src/tasks/task-status.enum.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export enum TasksStatusEnum {
OPEN = 'open',
IN_PROGRESS = 'in_progress',
DONE = 'done',
}
2 changes: 1 addition & 1 deletion src/tasks/task.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TasksStatusEnum } from 'src/tasks/task.model';
import { TasksStatusEnum } from 'src/tasks/task-status.enum';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
Expand Down
12 changes: 0 additions & 12 deletions src/tasks/task.model.ts

This file was deleted.

33 changes: 32 additions & 1 deletion src/tasks/task.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,37 @@
import { CreateTaskDto } from 'src/tasks/dto/create-task.dto';
import { GetTasksDto } from 'src/tasks/dto/get-tasks.dto';
import { TasksStatusEnum } from 'src/tasks/task-status.enum';
import { EntityRepository, Repository } from 'typeorm';
import { Task } from './task.entity';

@EntityRepository(Task)
export class TaskRepository extends Repository<Task> {}
export class TaskRepository extends Repository<Task> {
async getTasks(filterDto: GetTasksDto): Promise<Task[]> {
const { q, status } = filterDto;
const query = this.createQueryBuilder('task');
if (status) {
query.andWhere('task.status = :status', { status });
}
if (q) {
// 'LOWER(task.title) LIKE LOWER(:search) OR LOWER(task.description) LIKE LOWER(:search)', //case insensitive
query.andWhere(
'task.title LIKE :search OR task.description LIKE :search', //case sensitive
{
search: `%${q}%`,
},
);
}
const tasks = await query.getMany();
return tasks;
}
async createTask(createTaskDto: CreateTaskDto): Promise<Task> {
const { title, description } = createTaskDto;
const task = this.create({
title,
description,
status: TasksStatusEnum.OPEN,
});
await this.save(task);
return task;
}
}
16 changes: 8 additions & 8 deletions src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
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 { Task } from 'src/tasks/task.entity';
import { TasksService } from 'src/tasks/tasks.service';

@Controller('tasks')
Expand All @@ -23,22 +23,22 @@ export class TasksController {
return this.tasksService.createTask(createTaskDto);
}
@Get()
getAllTasks(@Query() filterDto: GetTasksDto): Task[] {
getTasks(@Query() filterDto: GetTasksDto): Promise<Task[]> {
return this.tasksService.getTasks(filterDto);
}

@Get(':id')
getTaskById(@Param('id') id: string): Task {
getTaskById(@Param('id') id: string): Promise<Task> {
return this.tasksService.getTaskById(id);
}

@Delete(':id')
deleteTask(@Param('id') id: string): void {
deleteTask(@Param('id') id: string): Promise<void> {
return this.tasksService.deleteTask(id);
}

@Patch(':id')
updateTask(@Param('id') id: string, @Body() updateTaskDto: UpdateTaskDto) {
updateTask(
@Param('id') id: string,
@Body() updateTaskDto: UpdateTaskDto,
): Promise<Task> {
return this.tasksService.updateTask(id, updateTaskDto);
}
}
90 changes: 26 additions & 64 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,47 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { of } from 'rxjs';
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, TasksStatusEnum } from 'src/tasks/task.model';
import { v4 as uuid } from 'uuid';
import { TasksStatusEnum } from 'src/tasks/task-status.enum';
import { Task } from 'src/tasks/task.entity';
import { TaskRepository } from 'src/tasks/task.repository';
@Injectable()
export class TasksService {
private tasks: Task[] = [];
constructor(
@InjectRepository(TaskRepository)
private taskRepository: TaskRepository,
) {}

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;
async getTasks(filterDto: GetTasksDto): Promise<Task[]> {
return this.taskRepository.getTasks(filterDto);
}

// 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 {
const task = this.tasks.find((task) => task.id === id);
if (!task) {
async getTaskById(id: string): Promise<Task> {
const found = await this.taskRepository.findOne(id);
if (!found) {
throw new NotFoundException(`Task with ID ${id} not found`);
}
return task;
}

updateTask(id: string, updateTaskDto: UpdateTaskDto) {
const { status } = updateTaskDto;
const tasks = [...this.tasks];
const updatedIndex = this.tasks.findIndex((task) => task.id === id);
if (!updatedIndex) {
throw new NotFoundException();
}
tasks[updatedIndex].status = status;
this.tasks = tasks;
return tasks[updatedIndex];
return found;
}

createTask(createTaskDto: CreateTaskDto): Task {
const { title, description } = createTaskDto;
const task: Task = {
id: uuid(),
title,
description,
status: TasksStatusEnum.OPEN,
};
this.tasks.push(task);
async updateTask(id: string, updateTaskDto: UpdateTaskDto) {
const task = await this.getTaskById(id);
task.status = updateTaskDto?.status ?? task.status;
await this.taskRepository.save(task);
return task;
}
deleteTask(id: string): void {
const found = this.getTaskById(id);
if (!found) {
createTask(createTaskDto: CreateTaskDto): Promise<Task> {
return this.taskRepository.createTask(createTaskDto);
}
async deleteTask(id: string): Promise<void> {
const deletedTask = await this.taskRepository.delete(id);

if (deletedTask.affected !== 1) {
throw new NotFoundException();
}
this.tasks = this.tasks.filter((task) => task.id !== found.id);
// const deletedIndex = this.tasks.findIndex((task) => task.id === id);
// // if (deletedIndex === -1) {
// // throw new Error('not found');
// // }
// this.tasks.splice(deletedIndex, 1);
return;
}
}

0 comments on commit 7d16a14

Please sign in to comment.