Passed
Push — master ( f58cd4...647f1d )
by Mathieu
01:52
created

DiscountRepository.findOneById   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 6
rs 10
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 { Discount } from 'src/Domain/School/Discount.entity';
5
import { IDiscountRepository } from 'src/Domain/School/Repository/IDiscountRepository';
6
import { School } from 'src/Domain/School/School.entity';
7
8
@Injectable()
9
export class DiscountRepository implements IDiscountRepository {
10
  constructor(
11
    @InjectRepository(Discount)
12
    private readonly repository: Repository<Discount>
13
  ) {}
14
15
  public save(discount: Discount): Promise<Discount> {
16
    return this.repository.save(discount);
17
  }
18
19
  public remove(discount: Discount): void {
20
    this.repository.delete(discount.getId());
21
  }
22
23
  public findOneById(id: string): Promise<Discount | undefined> {
24
    return this.repository
25
      .createQueryBuilder('discount')
26
      .select(['discount.id'])
27
      .where('discount.id = :id', { id })
28
      .getOne();
29
  }
30
31
  public findOneByAmountAndSchool(amount: number, school: School): Promise<Discount | undefined> {
32
    return this.repository
33
      .createQueryBuilder('discount')
34
      .select(['discount.id'])
35
      .innerJoin('discount.school', 'school', 'school.id = :school', {
36
        school: school.getId()
37
      })
38
      .where('discount.amount = :amount', { amount })
39
      .getOne();
40
  }
41
42
  public findBySchool(schoolId: string): Promise<Discount[]> {
43
    return this.repository
44
      .createQueryBuilder('discount')
45
      .select([
46
        'discount.id',
47
        'discount.type',
48
        'discount.amount',
49
        'discount.value',
50
      ])
51
      .innerJoin('discount.school', 'school', 'school.id = :schoolId', { schoolId })
52
      .orderBy('discount.amount', 'ASC')
53
      .getMany();
54
  }
55
56
  public countBySchool(id: string): Promise<number> {
57
    return this.repository
58
      .createQueryBuilder('discount')
59
      .select('discount.id')
60
      .innerJoin('discount.school', 'school', 'school.id = :id', { id })
61
      .getCount();
62
  }
63
}
64