Passed
Push — master ( 2cdb9e...45e047 )
by Mathieu
01:31
created

ShootingRepository.findOneById   A

Complexity

Conditions 1

Size

Total Lines 11
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
1
import { Injectable } from '@nestjs/common';
2
import { Repository } from 'typeorm';
3
import { InjectRepository } from '@nestjs/typeorm';
4
import { IShootingRepository } from 'src/Domain/School/Repository/IShootingRepository';
5
import { Shooting } from 'src/Domain/School/Shooting.entity';
6
7
@Injectable()
8
export class ShootingRepository implements IShootingRepository {
9
  constructor(
10
    @InjectRepository(Shooting)
11
    private readonly repository: Repository<Shooting>
12
  ) {}
13
14
  public save(shooting: Shooting): Promise<Shooting> {
15
    return this.repository.save(shooting);
16
  }
17
18
  public findBySchool(schoolId: string): Promise<Shooting[]> {
19
    return this.repository
20
      .createQueryBuilder('shooting')
21
      .select([
22
        'shooting.id',
23
        'shooting.name',
24
        'shooting.status',
25
        'shooting.shootingDate',
26
        'shooting.closingDate'
27
      ])
28
      .innerJoin('shooting.school', 'school', 'school.id = :schoolId', { schoolId })
29
      .orderBy('shooting.shootingDate', 'DESC')
30
      .getMany();
31
  }
32
33
  public findOneById(id: string): Promise<Shooting | undefined> {
34
    return this.repository
35
      .createQueryBuilder('shooting')
36
      .select([
37
        'shooting.name',
38
        'shooting.status',
39
        'shooting.shootingDate',
40
        'shooting.closingDate'
41
      ])
42
      .where('shooting.id = :id', { id })
43
      .getOne();
44
  }
45
46
  public countBySchool(id: string): Promise<number> {
47
    return this.repository
48
      .createQueryBuilder('shooting')
49
      .select('shooting.id')
50
      .innerJoin('shooting.school', 'school', 'school.id = :id', { id })
51
      .getCount();
52
  }
53
}
54