Compare commits
3 Commits
238cd06920
...
cfd85e729b
Author | SHA1 | Date |
---|---|---|
|
cfd85e729b | 2 months ago |
|
780fb8bc1f | 2 months ago |
|
44b3017ce4 | 2 months ago |
6 changed files with 121 additions and 14 deletions
@ -0,0 +1,22 @@ |
|||||||
|
import { Model, Table, Column, DataType } from "sequelize-typescript"; |
||||||
|
|
||||||
|
@Table |
||||||
|
export class Product extends Model<Product> { |
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
name: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.STRING, |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
description: string; |
||||||
|
|
||||||
|
@Column({ |
||||||
|
type: DataType.DECIMAL(10, 2), |
||||||
|
allowNull: false, |
||||||
|
}) |
||||||
|
price: number; |
||||||
|
} |
@ -0,0 +1,23 @@ |
|||||||
|
import { Controller, Get, Post, Body, Patch, Param, Delete, Res, Query } from "@nestjs/common"; |
||||||
|
import { ProductsService } from "./products.service"; |
||||||
|
import { Product } from "./entities/product.entity"; |
||||||
|
|
||||||
|
@Controller("products") |
||||||
|
export class ProductsController { |
||||||
|
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 |
||||||
|
}; |
||||||
|
} |
||||||
|
@Get() |
||||||
|
async findAll(@Query() query: { search?: string; priceMin?: number; priceMax?: number }){ |
||||||
|
const { search, priceMin, priceMax } = query; |
||||||
|
return this.productsService.findAll(search, priceMin, priceMax); |
||||||
|
} |
||||||
|
} |
||||||
|
|
@ -0,0 +1,12 @@ |
|||||||
|
import { Module } from "@nestjs/common"; |
||||||
|
import { ProductsService } from "./products.service"; |
||||||
|
import { ProductsController } from "./products.controller"; |
||||||
|
import { SequelizeModule } from "@nestjs/sequelize"; |
||||||
|
import { Product } from "./entities/product.entity"; |
||||||
|
|
||||||
|
@Module({ |
||||||
|
imports: [SequelizeModule.forFeature([Product])], |
||||||
|
controllers: [ProductsController], |
||||||
|
providers: [ProductsService], |
||||||
|
}) |
||||||
|
export class ProductsModule {} |
@ -0,0 +1,53 @@ |
|||||||
|
import { Injectable } from "@nestjs/common"; |
||||||
|
import { InjectModel } from "@nestjs/sequelize"; |
||||||
|
import { Product } from "./entities/product.entity"; |
||||||
|
import { Op } from "sequelize"; |
||||||
|
import { HttpException, HttpStatus } from "@nestjs/common"; |
||||||
|
@Injectable() |
||||||
|
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"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
async findAll(search?: string, priceMin?: number, priceMax?: number): Promise<Product[]> { |
||||||
|
const where: any = {}; |
||||||
|
|
||||||
|
try { |
||||||
|
if (search) { |
||||||
|
where.name = { |
||||||
|
[Op.like]: `%${search}%`, |
||||||
|
}; |
||||||
|
} |
||||||
|
|
||||||
|
if (priceMin || priceMax) { |
||||||
|
where.price = {}; |
||||||
|
if (priceMin) where.price[Op.gte] = priceMin; |
||||||
|
if (priceMax) where.price[Op.lte] = priceMax; |
||||||
|
} |
||||||
|
|
||||||
|
const products = await this.productModel.findAll({ where }); |
||||||
|
|
||||||
|
if (!products || products.length === 0) { |
||||||
|
throw new HttpException("No products found matching the given criteria.", HttpStatus.NOT_FOUND); |
||||||
|
} |
||||||
|
|
||||||
|
return products; |
||||||
|
} catch (error) { |
||||||
|
if (error instanceof HttpException) { |
||||||
|
throw error; |
||||||
|
} |
||||||
|
|
||||||
|
throw new HttpException("An error occurred while retrieving products.", HttpStatus.INTERNAL_SERVER_ERROR); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue