|
1
|
|
|
import {Injectable} from '@nestjs/common'; |
|
2
|
|
|
import {InjectRepository} from '@nestjs/typeorm'; |
|
3
|
|
|
import {Repository} from 'typeorm'; |
|
4
|
|
|
import {IProjectRepository} from 'src/Domain/Project/Repository/IProjectRepository'; |
|
5
|
|
|
import {Project} from 'src/Domain/Project/Project.entity'; |
|
6
|
|
|
import {MAX_ITEMS_PER_PAGE} from 'src/Application/Common/Pagination'; |
|
7
|
|
|
|
|
8
|
|
|
@Injectable() |
|
9
|
|
|
export class ProjectRepository implements IProjectRepository { |
|
10
|
|
|
constructor( |
|
11
|
|
|
@InjectRepository(Project) |
|
12
|
|
|
private readonly repository: Repository<Project> |
|
13
|
|
|
) {} |
|
14
|
|
|
|
|
15
|
|
|
public save(project: Project): Promise<Project> { |
|
16
|
|
|
return this.repository.save(project); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
public findOneByName(name: string): Promise<Project | undefined> { |
|
20
|
|
|
return this.repository |
|
21
|
|
|
.createQueryBuilder('project') |
|
22
|
|
|
.where('LOWER(project.name) = LOWER(:name)', {name}) |
|
23
|
|
|
.getOne(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public findOneById(id: string): Promise<Project | undefined> { |
|
27
|
|
|
return this.repository |
|
28
|
|
|
.createQueryBuilder('project') |
|
29
|
|
|
.select([ |
|
30
|
|
|
'project.id', |
|
31
|
|
|
'project.name', |
|
32
|
|
|
'project.invoiceUnit', |
|
33
|
|
|
'project.dayDuration', |
|
34
|
|
|
'customer.id', |
|
35
|
|
|
'customer.name' |
|
36
|
|
|
]) |
|
37
|
|
|
.innerJoin('project.customer', 'customer') |
|
38
|
|
|
.where('project.id = :id', {id}) |
|
39
|
|
|
.getOne(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public findProjects( |
|
43
|
|
|
page: number = 1, |
|
44
|
|
|
customerId?: string |
|
45
|
|
|
): Promise<[Project[], number]> { |
|
46
|
|
|
const query = this.repository |
|
47
|
|
|
.createQueryBuilder('project') |
|
48
|
|
|
.select([ |
|
49
|
|
|
'project.id', |
|
50
|
|
|
'project.name', |
|
51
|
|
|
'project.dayDuration', |
|
52
|
|
|
'project.invoiceUnit', |
|
53
|
|
|
'customer.id', |
|
54
|
|
|
'customer.name' |
|
55
|
|
|
]) |
|
56
|
|
|
.innerJoin('project.customer', 'customer') |
|
57
|
|
|
.orderBy('project.name', 'ASC') |
|
58
|
|
|
.addOrderBy('customer.name', 'ASC') |
|
59
|
|
|
.limit(MAX_ITEMS_PER_PAGE) |
|
60
|
|
|
.offset((page - 1) * MAX_ITEMS_PER_PAGE); |
|
61
|
|
|
|
|
62
|
|
|
if (customerId) { |
|
63
|
|
|
query.andWhere('customer.id = :customerId', {customerId}); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return query.getManyAndCount(); |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|