import { Inject, Injectable } from '@nestjs/common'; import { CreateProductDto, UpdateProductDto } from './dto'; import { PRODUCT_REPOSITORY } from 'src/core/constants'; import { Product } from './entities/product.entity'; @Injectable() export class ProductsService { constructor( @Inject(PRODUCT_REPOSITORY) private readonly productRepository: typeof Product, ) {} async create(createProductDto: CreateProductDto) { return await this.productRepository.create(createProductDto); } async findAll() { return await this.productRepository.findAll(); } async findOne(id: number) { return await this.productRepository.findAll({ where: { id } }); } async update(id: number, updateProductDto: UpdateProductDto) { const [numberOfAffectedRows, [updatedProduct]] = await this.productRepository.update( { ...updateProductDto }, { where: { id }, returning: true }, ); return { numberOfAffectedRows, updatedProduct }; } async remove(id: number) { const deletedProduct = await this.findOne(id); await this.productRepository.destroy({ where: { id } }); return deletedProduct; } }