import { Injectable , Inject } from '@nestjs/common' ; import { Repository } from 'typeorm' ; import { Product } from '../entities/product.entity' ; @ Injectable ( ) export class ProductStore { public constructor ( @ Inject ( 'PRODUCT_REPOSITORY' ) private productRepository ) { } public async getAllProducts ( ) { return await this . productRepository . find ( ) ; } public async getProductBySku ( sku ) { return await this . productRepository . findOne ( { sku } ) ; } public async register ( productEntity , sku ? ) { if ( sku ) { return await this . update ( productEntity , sku ) ; } else { return await this . create ( productEntity ) ; } } private async create ( productEntity ) { try { const product = this . productRepository . create ( productEntity ) ; return await this . productRepository . save ( product ) ; } catch ( e ) { return new Error ( e ) ; } } private async update ( productEntity , sku ) { try { await this . productRepository . update ( { sku } , productEntity ) ; return this . productRepository . findOne ( { sku } ) ; } catch ( e ) { return new Error ( e ) ; } } public async removeProduct ( sku ) { try { const product = this . productRepository . findOne ( { sku } ) ; await this . productRepository . delete ( { sku } ) ; return product ; } catch ( e ) { return new Error ( e ) ; } } }