src/Infrastructure/Customer/Repository/CustomerRepository.ts   A
last analyzed

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 49
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 40
mnd 1
bc 1
fnc 4
dl 0
loc 49
rs 10
bpm 0.25
cpm 1.25
noi 0
c 0
b 0
f 0

4 Functions

Rating   Name   Duplication   Size   Complexity  
A CustomerRepository.findOneByName 0 5 1
A CustomerRepository.save 0 3 1
A CustomerRepository.findOneById 0 6 1
A CustomerRepository.findCustomers 0 14 2
1
import { Injectable } from '@nestjs/common';
2
import { InjectRepository } from '@nestjs/typeorm';
3
import { Repository } from 'typeorm';
4
import { ICustomerRepository } from 'src/Domain/Customer/Repository/ICustomerRepository';
5
import { Customer } from 'src/Domain/Customer/Customer.entity';
6
import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination';
7
8
@Injectable()
9
export class CustomerRepository implements ICustomerRepository {
10
  constructor(
11
    @InjectRepository(Customer)
12
    private readonly repository: Repository<Customer>
13
  ) {}
14
15
  public save(customer: Customer): Promise<Customer> {
16
    return this.repository.save(customer);
17
  }
18
19
  public findOneByName(name: string): Promise<Customer | undefined> {
20
    return this.repository
21
      .createQueryBuilder('customer')
22
      .where('LOWER(customer.name) = LOWER(:name)', { name })
23
      .getOne();
24
  }
25
26
  public findOneById(id: string): Promise<Customer | undefined> {
27
    return this.repository
28
      .createQueryBuilder('customer')
29
      .select(['customer.id', 'customer.name'])
30
      .where('customer.id = :id', { id })
31
      .getOne();
32
  }
33
34
  public findCustomers(page: number | null): Promise<[Customer[], number]> {
35
    let query = this.repository
36
      .createQueryBuilder('customer')
37
      .select(['customer.id', 'customer.name'])
38
      .orderBy('customer.name', 'ASC');
39
40
    if (typeof page === 'number') {
41
      query = query
42
        .limit(MAX_ITEMS_PER_PAGE)
43
        .offset((page - 1) * MAX_ITEMS_PER_PAGE);
44
    }
45
46
    return query.getManyAndCount();
47
  }
48
}
49