Skip to content

Commit

Permalink
add constrain on task with owner
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaalanMarwan committed May 1, 2022
1 parent 927a557 commit f08f13d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/tasks/task.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ export class TaskRepository extends Repository<Task> {
query.andWhere('task.status = :status', { status });
}
if (q) {
// 'LOWER(task.title) LIKE LOWER(:search) OR LOWER(task.description) LIKE LOWER(:search)', //case insensitive
// '(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
'(task.title LIKE :search OR task.description LIKE :search)', //case sensitive
{
search: `%${q}%`,
},
Expand Down
11 changes: 6 additions & 5 deletions src/tasks/tasks.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,19 @@ export class TasksController {
return this.tasksService.getTasks(filterDto, user);
}
@Get(':id')
getTaskById(@Param('id') id: string): Promise<Task> {
return this.tasksService.getTaskById(id);
getTaskById(@Param('id') id: string, @GetUser() user: User): Promise<Task> {
return this.tasksService.getTaskById(id, user);
}
@Delete(':id')
deleteTask(@Param('id') id: string): Promise<void> {
return this.tasksService.deleteTask(id);
deleteTask(@Param('id') id: string, @GetUser() user: User): Promise<void> {
return this.tasksService.deleteTask(id, user);
}
@Patch(':id')
updateTask(
@Param('id') id: string,
@Body() updateTaskDto: UpdateTaskDto,
@GetUser() user: User,
): Promise<Task> {
return this.tasksService.updateTask(id, updateTaskDto);
return this.tasksService.updateTask(id, updateTaskDto, user);
}
}
16 changes: 10 additions & 6 deletions src/tasks/tasks.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { of } from 'rxjs';
import { GetUser } from 'src/auth/get-user-decorator';
import { User } from 'src/auth/user.entity';
import { CreateTaskDto } from 'src/tasks/dto/create-task.dto';
import { GetTasksDto } from 'src/tasks/dto/get-tasks.dto';
Expand All @@ -19,26 +20,29 @@ export class TasksService {
return this.taskRepository.getTasks(filterDto, user);
}

async getTaskById(id: string): Promise<Task> {
const found = await this.taskRepository.findOne(id);
async getTaskById(id: string, @GetUser() user: User): Promise<Task> {
// const found = await this.taskRepository.findOne(id);
const found = await this.taskRepository.findOne({
where: { id, user },
});
if (!found) {
throw new NotFoundException(`Task with ID ${id} not found`);
}

return found;
}

async updateTask(id: string, updateTaskDto: UpdateTaskDto) {
const task = await this.getTaskById(id);
async updateTask(id: string, updateTaskDto: UpdateTaskDto, user: User) {
const task = await this.getTaskById(id, user);
task.status = updateTaskDto?.status ?? task.status;
await this.taskRepository.save(task);
return task;
}
createTask(createTaskDto: CreateTaskDto, user: User): Promise<Task> {
return this.taskRepository.createTask(createTaskDto, user);
}
async deleteTask(id: string): Promise<void> {
const deletedTask = await this.taskRepository.delete(id);
async deleteTask(id: string, user: User): Promise<void> {
const deletedTask = await this.taskRepository.delete({ id, user });

if (deletedTask.affected !== 1) {
throw new NotFoundException();
Expand Down

0 comments on commit f08f13d

Please sign in to comment.