import { HttpException , HttpStatus , Injectable } from '@nestjs/common' ; import { InjectModel } from '@nestjs/mongoose' ; import { Model } from 'mongoose' ; import { Product } from '../types/product' ; import { User } from '../types/user' ; import { CreateProductDTO , UpdateProductDTO } from './product.dto' ; @ Injectable ( ) export class ProductService { constructor ( @ InjectModel ( 'Product' ) private productModel ) { } async findAll ( ) { return await this . productModel . find ( ) . populate ( 'owner' ) ; } async findByOwner ( userId ) { return await this . productModel . find ( { owner : userId } ) . populate ( 'owner' ) ; } async findById ( id ) { const product = await this . productModel . findById ( id ) . populate ( 'owner' ) ; if ( ! product ) { throw new HttpException ( 'Product not found' , HttpStatus . NO_CONTENT ) ; } return product ; } async create ( productDTO , user ) { const product = await this . productModel . create ( { ... productDTO , owner : user , } ) ; await product . save ( ) ; return product . populate ( 'owner' ) ; } async update ( id , productDTO , userId ) { const product = await this . productModel . findById ( id ) ; if ( userId !== product . owner . toString ( ) ) { throw new HttpException ( 'You do not own this product' , HttpStatus . UNAUTHORIZED ) ; } await product . update ( productDTO ) ; return await this . productModel . findById ( id ) . populate ( 'owner' ) ; } async delete ( id , userId ) { const product = await this . productModel . findById ( id ) ; if ( userId !== product . owner . toString ( ) ) { throw new HttpException ( 'You do not own this product' , HttpStatus . UNAUTHORIZED ) ; } await product . remove ( ) ; return product . populate ( 'owner' ) ; } }