import { Injectable , Inject } from '@nestjs/common' ; import { Repository } from 'typeorm' ; import { Catalog } from '../entities/catalog.entity' ; @ Injectable ( ) export class CatalogStore { public constructor ( @ Inject ( 'CATALOG_REPOSITORY' ) private catalogRepository ) { } public async getAllCatalog ( ) { return await this . catalogRepository . find ( ) ; } public async getCatalogBySku ( sku ) { return await this . catalogRepository . findOne ( { sku } ) ; } public async register ( catalogEntity , sku ? ) { if ( sku ) { return await this . update ( catalogEntity , sku ) ; } else { return await this . create ( catalogEntity ) ; } } private async create ( catalogEntity ) { try { const catalog = this . catalogRepository . create ( catalogEntity ) ; return await this . catalogRepository . save ( catalog ) ; } catch ( e ) { return new Error ( e ) ; } } private async update ( catalogEntity , sku ) { try { await this . catalogRepository . update ( { sku } , catalogEntity ) ; return this . catalogRepository . findOne ( { sku } ) ; } catch ( e ) { return new Error ( e ) ; } } public async removeCatalog ( sku ) { try { const catalog = this . catalogRepository . findOne ( { sku } ) ; await this . catalogRepository . delete ( { sku } ) ; return catalog ; } catch ( e ) { return new Error ( e ) ; } } }