Compare commits

..

No commits in common. 'cfd85e729b6a9ae88007aaffaeacae25de7eb650' and '238cd06920d0080ec847a65d4aba191b8c42d664' have entirely different histories.

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

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

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

Loading…
Cancel
Save