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.
 
 

41 lines
1.1 KiB

import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectModel } from '@nestjs/sequelize';
import { Product } from '../shop/entities/product.entity';
import { CreateProductDto } from './dto/create-product.dto';
import { UpdateProductDto } from './dto/update-product.dto';
@Injectable()
export class ProductsService {
constructor(
@InjectModel(Product)
private readonly productModel: typeof Product,
) {}
async create(createProductDto: CreateProductDto) {
return this.productModel.create(createProductDto);
}
async findAll() {
return this.productModel.findAll();
}
async findOne(id: number) {
const product = await this.productModel.findByPk(id);
if (!product) {
throw new NotFoundException(`Product with ID ${id} not found`);
}
return product;
}
async update(id: number, updateProductDto: UpdateProductDto) {
const product = await this.findOne(id);
await product.update(updateProductDto);
return product;
}
async remove(id: number) {
const product = await this.findOne(id);
await product.destroy();
return { message: 'Product deleted successfully' };
}
}