Passed
Pull Request — master (#216)
by
unknown
01:40
created

ContactRepository.findContacts   A

Complexity

Conditions 1

Size

Total Lines 17
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 16
dl 0
loc 17
rs 9.6
c 0
b 0
f 0
1
import { Injectable } from '@nestjs/common';
2
import { InjectRepository } from '@nestjs/typeorm';
3
import { Contact } from 'src/Domain/Contact/Contact.entity';
4
import { IContactRepository } from 'src/Domain/Contact/Repository/IContactRepository';
5
import { Repository } from 'typeorm';
6
import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination';
7
8
@Injectable()
9
export class ContactRepository implements IContactRepository {
10
  constructor(
11
    @InjectRepository(Contact)
12
    private readonly repository: Repository<Contact>
13
  ) {}
14
15
  public save(contact: Contact): Promise<Contact> {
16
    return this.repository.save(contact);
17
  }
18
19
  public findOneById(id: string): Promise<Contact | undefined> {
20
    return this.repository
21
      .createQueryBuilder('contact')
22
      .select([
23
        'contact.id',
24
        'contact.firstName',
25
        'contact.lastName',
26
        'contact.company',
27
        'contact.email',
28
        'contact.phoneNumber',
29
        'contact.notes'
30
      ])
31
      .where('contact.id = :id', { id })
32
      .getOne();
33
  }
34
35
  public findContacts(page: number): Promise<[Contact[], number]> {
36
    return this.repository
37
      .createQueryBuilder('contact')
38
      .select([
39
        'contact.id',
40
        'contact.firstName',
41
        'contact.lastName',
42
        'contact.company',
43
        'contact.email',
44
        'contact.phoneNumber',
45
        'contact.notes'
46
      ])
47
      .orderBy('contact.lastName', 'ASC')
48
      .addOrderBy('contact.firstName', 'ASC')
49
      .limit(MAX_ITEMS_PER_PAGE)
50
      .offset((page - 1) * MAX_ITEMS_PER_PAGE)
51
      .getManyAndCount();
52
  }
53
}
54