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.
 
 

113 lines
3.3 KiB

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);
}
}
async findOne(id: string): Promise<Product> {
try {
const product = await this.productModel.findByPk(id);
if (!product) {
throw new HttpException("Product not found with the given id.", HttpStatus.NOT_FOUND);
}
return product;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException("An error occurred while retrieving the product.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async update(id: string, name?: string, description?: string, price?: number): Promise<Product> {
try {
const product = await this.productModel.findByPk(id);
if (!product) {
throw new HttpException("Product not found.", HttpStatus.NOT_FOUND);
}
if (name) product.name = name;
if (description) product.description = description;
if (price) product.price = price;
await product.save();
return product;
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException("An error occurred while updating the product.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
async remove(id: string): Promise<{ message: string }> {
try {
const product = await this.productModel.findByPk(id);
if (!product) {
throw new HttpException("Product not found with the given id.", HttpStatus.NOT_FOUND);
}
await product.destroy();
return { message: "Product deleted successfully." };
} catch (error) {
if (error instanceof HttpException) {
throw error;
}
throw new HttpException("An error occurred while deleting the product.", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}