| Total Complexity | 4 |
| Complexity/F | 1 |
| Lines of Code | 43 |
| Function Count | 4 |
| Duplicated Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
| 1 | import { Injectable } from '@nestjs/common'; |
||
| 2 | import { InjectRepository } from '@nestjs/typeorm'; |
||
| 3 | import { Repository } from 'typeorm'; |
||
| 4 | import { ITaskRepository } from 'src/Domain/Task/Repository/ITaskRepository'; |
||
| 5 | import { Task } from 'src/Domain/Task/Task.entity'; |
||
| 6 | import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination'; |
||
| 7 | |||
| 8 | @Injectable() |
||
| 9 | export class TaskRepository implements ITaskRepository { |
||
| 10 | constructor( |
||
| 11 | @InjectRepository(Task) |
||
| 12 | private readonly repository: Repository<Task> |
||
| 13 | ) {} |
||
| 14 | |||
| 15 | public save(task: Task): Promise<Task> { |
||
| 16 | return this.repository.save(task); |
||
| 17 | } |
||
| 18 | |||
| 19 | public findOneByName(name: string): Promise<Task | undefined> { |
||
| 20 | return this.repository |
||
| 21 | .createQueryBuilder('task') |
||
| 22 | .where('LOWER(task.name) = LOWER(:name)', { name }) |
||
| 23 | .getOne(); |
||
| 24 | } |
||
| 25 | |||
| 26 | public findOneById(id: string): Promise<Task | undefined> { |
||
| 27 | return this.repository |
||
| 28 | .createQueryBuilder('task') |
||
| 29 | .where('task.id = :id', { id }) |
||
| 30 | .getOne(); |
||
| 31 | } |
||
| 32 | |||
| 33 | public findTasks(page: number = 1): Promise<[Task[], number]> { |
||
| 34 | return this.repository |
||
| 35 | .createQueryBuilder('task') |
||
| 36 | .select(['task.id', 'task.name']) |
||
| 37 | .orderBy('task.name', 'ASC') |
||
| 38 | .limit(MAX_ITEMS_PER_PAGE) |
||
| 39 | .offset((page - 1) * MAX_ITEMS_PER_PAGE) |
||
| 40 | .getManyAndCount(); |
||
| 41 | } |
||
| 42 | } |
||
| 43 |