1
|
|
|
import { Injectable } from '@nestjs/common'; |
2
|
|
|
import { Repository } from 'typeorm'; |
3
|
|
|
import { InjectRepository } from '@nestjs/typeorm'; |
4
|
|
|
import { SchoolUser } from 'src/Domain/School/SchoolUser.entity'; |
5
|
|
|
import { ISchoolUserRepository } from 'src/Domain/School/Repository/ISchoolUserRepository'; |
6
|
|
|
import { School } from 'src/Domain/School/School.entity'; |
7
|
|
|
import { User } from 'src/Domain/User/User.entity'; |
8
|
|
|
|
9
|
|
|
@Injectable() |
10
|
|
|
export class SchoolUserRepository implements ISchoolUserRepository { |
11
|
|
|
constructor( |
12
|
|
|
@InjectRepository(SchoolUser) |
13
|
|
|
private readonly repository: Repository<SchoolUser> |
14
|
|
|
) {} |
15
|
|
|
|
16
|
|
|
public save(schoolUser: SchoolUser): Promise<SchoolUser> { |
17
|
|
|
return this.repository.save(schoolUser); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public remove(schoolUser: SchoolUser): void { |
21
|
|
|
this.repository.delete(schoolUser.getId()); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public findOneById(id: string): Promise<SchoolUser | undefined> { |
25
|
|
|
return this.repository |
26
|
|
|
.createQueryBuilder('schoolUser') |
27
|
|
|
.select('schoolUser.id') |
28
|
|
|
.where('schoolUser.id = :id', { id }) |
29
|
|
|
.getOne(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public findOneByUserAndSchool(user: User, school: School): Promise<SchoolUser | undefined> { |
33
|
|
|
return this.repository |
34
|
|
|
.createQueryBuilder('schoolUser') |
35
|
|
|
.select('schoolUser.id') |
36
|
|
|
.innerJoin( |
37
|
|
|
'schoolUser.user', |
38
|
|
|
'user', |
39
|
|
|
'user.id = :userId', |
40
|
|
|
{ userId: user.getId() } |
41
|
|
|
) |
42
|
|
|
.innerJoin( |
43
|
|
|
'schoolUser.school', |
44
|
|
|
'school', |
45
|
|
|
'school.id = :schoolId', |
46
|
|
|
{ schoolId: school.getId() } |
47
|
|
|
) |
48
|
|
|
.getOne(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public findUsersBySchool(schoolId: string): Promise<SchoolUser[]> { |
52
|
|
|
return this.repository |
53
|
|
|
.createQueryBuilder('schoolUser') |
54
|
|
|
.select([ 'schoolUser.id', 'user.email' ]) |
55
|
|
|
.innerJoin('schoolUser.user', 'user') |
56
|
|
|
.innerJoin('schoolUser.school', 'school', 'school.id = :schoolId', { schoolId }) |
57
|
|
|
.orderBy('user.lastName', 'ASC') |
58
|
|
|
.getMany(); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|