Passed
Push — master ( 647f1d...d78cb9 )
by Mathieu
02:20 queued 24s
created

ProductRepository.remove   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
1
import { Injectable } from '@nestjs/common';
2
import { Repository } from 'typeorm';
3
import { InjectRepository } from '@nestjs/typeorm';
4
import { Product } from 'src/Domain/Product/Product.entity';
5
import { IProductRepository } from 'src/Domain/Product/Repository/IProductRepository';
6
import { MAX_ITEMS_PER_PAGE } from 'src/Application/Common/Pagination';
7
8
@Injectable()
9
export class ProductRepository implements IProductRepository {
10
  constructor(
11
    @InjectRepository(Product)
12
    private readonly repository: Repository<Product>
13
  ) {}
14
15
  public save(product: Product): Promise<Product> {
16
    return this.repository.save(product);
17
  }
18
19
  public remove(product: Product): void {
20
    this.repository.delete(product.getId());
21
  }
22
23
  public findOneByTitle(title: string): Promise<Product | undefined> {
24
    return this.repository
25
      .createQueryBuilder('product')
26
      .select([
27
        'product.id'
28
      ])
29
      .where('lower(product.title) = :title', { title: title.toLowerCase() })
30
      .getOne();
31
  }
32
33
  public findOneById(id: string): Promise<Product | undefined> {
34
    return this.repository
35
      .createQueryBuilder('product')
36
      .select([
37
        'product.id',
38
        'product.title',
39
        'product.description',
40
        'product.unitPrice'
41
      ])
42
      .where('product.id = :id', { id })
43
      .getOne();
44
  }
45
46
  public findProducts(page: number = 1): Promise<[Product[], number]> {
47
    return this.repository
48
      .createQueryBuilder('product')
49
      .select([
50
        'product.id',
51
        'product.title',
52
        'product.description',
53
        'product.unitPrice'
54
      ])
55
      .orderBy('product.title', 'ASC')
56
      .limit(MAX_ITEMS_PER_PAGE)
57
      .offset((page - 1) * MAX_ITEMS_PER_PAGE)
58
      .getManyAndCount();
59
  }
60
}
61