parent
44b3017ce4
commit
780fb8bc1f
3 changed files with 36 additions and 10 deletions
@ -1,8 +1,18 @@ |
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; |
import { Controller, Get, Post, Body, Patch, Param, Delete, Res } from "@nestjs/common"; |
||||||
import { ProductsService } from './products.service'; |
import { ProductsService } from "./products.service"; |
||||||
|
import { Product } from "./entities/product.entity"; |
||||||
|
|
||||||
@Controller('products') |
@Controller("products") |
||||||
export class ProductsController { |
export class ProductsController { |
||||||
constructor(private readonly productsService: ProductsService) {} |
constructor(private readonly productsService: ProductsService) {} |
||||||
|
@Post() |
||||||
|
async create(@Body() body: { name: string; description: string; price: number }) { |
||||||
|
const { name, description, price } = body; |
||||||
|
const product = await this.productsService.create(name, description, price); |
||||||
|
return { |
||||||
|
message: 'Product created successfully!', |
||||||
|
product |
||||||
|
}; |
||||||
|
} |
||||||
} |
} |
||||||
|
|
||||||
|
@ -1,6 +1,19 @@ |
|||||||
import { Injectable } from '@nestjs/common'; |
import { Injectable } from "@nestjs/common"; |
||||||
|
import { InjectModel } from "@nestjs/sequelize"; |
||||||
|
import { Product } from "./entities/product.entity"; |
||||||
@Injectable() |
@Injectable() |
||||||
export class ProductsService { |
export class ProductsService { |
||||||
|
constructor(@InjectModel(Product) private readonly productModel: typeof Product) {} |
||||||
|
|
||||||
|
async create(name: string, description: string, price: number): Promise<Product> { |
||||||
|
try { |
||||||
|
if (!name || !description || price <= 0) { |
||||||
|
throw new Error("Invalid input data"); |
||||||
|
} |
||||||
|
const product = await this.productModel.create({ name, description, price }); |
||||||
|
return product; |
||||||
|
} catch (error) { |
||||||
|
throw new Error("Error creating product"); |
||||||
|
} |
||||||
|
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue