|
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
|
|
|
|