Compare commits
No commits in common. 'cfd85e729b6a9ae88007aaffaeacae25de7eb650' and '238cd06920d0080ec847a65d4aba191b8c42d664' have entirely different histories.
cfd85e729b
...
238cd06920
6 changed files with 14 additions and 121 deletions
@ -1,22 +0,0 @@ |
||||
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; |
||||
} |
@ -1,23 +0,0 @@ |
||||
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); |
||||
} |
||||
} |
||||
|
@ -1,12 +0,0 @@ |
||||
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 {} |
@ -1,53 +0,0 @@ |
||||
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