You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

49 lines
1.5 KiB

import { Inject, Injectable } from '@nestjs/common';
import { CreateProductDto, UpdateProductDto } from './dto';
import { PRODUCT_REPOSITORY } from 'src/core/constants';
import { Product } from './entities/product.entity';
import { ShoppingCardsService } from '../shopping-cards/shopping-cards.service';
@Injectable()
export class ProductsService {
constructor(
@Inject(PRODUCT_REPOSITORY)
private readonly productRepository: typeof Product,
// private readonly shoppingCardsService: ShoppingCardsService,
) {}
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;
}
async addProduct(id: number, userId: number) {
// const [card] = await this.shoppingCardsService.findbyUser(userId);
// card.products.push(id);
// return await card.save();
}
}