src/Infrastructure/Task/Repository/TaskRepository.ts   A
last analyzed

Complexity

Total Complexity 4
Complexity/F 1

Size

Lines of Code 43
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 34
mnd 0
bc 0
fnc 4
dl 0
loc 43
bpm 0
cpm 1
noi 0
c 0
b 0
f 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A TaskRepository.findTasks 0 8 1
A TaskRepository.save 0 3 1
A TaskRepository.findOneById 0 5 1
A TaskRepository.findOneByName 0 5 1
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