Passed
Pull Request — master (#216)
by
unknown
02:07
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 findContacts(page: number): Promise<[Contact[], number]> {
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
      .orderBy('contact.lastName', 'ASC')
32
      .addOrderBy('contact.firstName', 'ASC')
33
      .limit(MAX_ITEMS_PER_PAGE)
34
      .offset((page - 1) * MAX_ITEMS_PER_PAGE)
35
      .getManyAndCount();
36
  }
37
}
38