Compare commits

...

3 Commits

  1. 2
      src/app.module.ts
  2. 22
      src/products/entities/product.entity.ts
  3. 23
      src/products/products.controller.ts
  4. 12
      src/products/products.module.ts
  5. 53
      src/products/products.service.ts
  6. 23
      src/users/users.controller.ts

@ -5,6 +5,7 @@ import { ConfigModule } from "@nestjs/config";
import { SequelizeModule } from "@nestjs/sequelize";
import { databaseConfig } from "./config/database.config";
import { UsersModule } from './users/users.module';
import { ProductsModule } from './products/products.module';
@Module({
imports: [
@ -13,6 +14,7 @@ import { UsersModule } from './users/users.module';
}),
SequelizeModule.forRoot(databaseConfig),
UsersModule,
ProductsModule,
],
controllers: [AppController],
providers: [AppService],

@ -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);
}
}
}

@ -1,25 +1,20 @@
import { Controller, Post, Body, Res } from '@nestjs/common';
import { UsersService } from './users.service';
import { Response } from 'express';
import { Controller, Post, Body, Res, UseGuards, Get } from "@nestjs/common";
import { UsersService } from "./users.service";
import { Response } from "express";
import { JwtAuthGuard } from "src/guard/auth.guard";
@Controller('users')
@Controller("users")
export class UsersController {
constructor(private readonly usersService: UsersService) {}
@Post('register')
async register(
@Body() body: { email: string; password: string },
@Res() res: Response
): Promise<Response> {
@Post("register")
async register(@Body() body: { email: string; password: string }, @Res() res: Response): Promise<Response> {
const { email, password } = body;
return this.usersService.register(email, password, res);
}
@Post('login')
async login(
@Body() body: { email: string; password: string },
@Res() res: Response
): Promise<Response> {
@Post("login")
async login(@Body() body: { email: string; password: string }, @Res() res: Response): Promise<Response> {
const { email, password } = body;
return this.usersService.login(email, password, res);
}

Loading…
Cancel
Save