From 716c4a67828f570c6871fa48701ef2d567997e03 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 4 Jan 2025 15:23:37 +0330 Subject: [PATCH] Enhance product models --- migrations/20250104112403-create-product.js | 65 +++++++++++++++++++++ src/admin/admin.service.ts | 7 +-- src/products/entities/product.entity.ts | 37 ++++++++++++ src/products/products.controller.ts | 4 +- src/products/products.module.ts | 11 +++- 5 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 migrations/20250104112403-create-product.js diff --git a/migrations/20250104112403-create-product.js b/migrations/20250104112403-create-product.js new file mode 100644 index 0000000..7aa182e --- /dev/null +++ b/migrations/20250104112403-create-product.js @@ -0,0 +1,65 @@ +'use strict'; + +module.exports = { + up: async (queryInterface, Sequelize) => { + await queryInterface.createTable('Products', { + id: { + type: Sequelize.INTEGER, + allowNull: false, + autoIncrement: true, + primaryKey: true, + }, + name: { + type: Sequelize.STRING, + allowNull: false, + }, + description: { + type: Sequelize.STRING, + allowNull: false, + }, + price: { + type: Sequelize.DECIMAL(10, 2), + allowNull: false, + }, + imageUrl: { + type: Sequelize.STRING, + allowNull: true, + }, + tags: { + type: Sequelize.ARRAY(Sequelize.STRING), + allowNull: true, + }, + quantity: { + type: Sequelize.INTEGER, + allowNull: false, + defaultValue: 0, + }, + brand: { + type: Sequelize.STRING, + allowNull: true, + }, + color: { + type: Sequelize.STRING, + allowNull: true, + }, + category: { + type: Sequelize.STRING, + allowNull: false, + }, + createdAt: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, + updatedAt: { + type: Sequelize.DATE, + allowNull: false, + defaultValue: Sequelize.NOW, + }, + }); + }, + + down: async (queryInterface, Sequelize) => { + await queryInterface.dropTable('Products'); + }, +}; diff --git a/src/admin/admin.service.ts b/src/admin/admin.service.ts index 4990536..8fc6a09 100644 --- a/src/admin/admin.service.ts +++ b/src/admin/admin.service.ts @@ -41,12 +41,12 @@ export class AdminService { const admin = await this.adminModel.findOne({ where: { email: loginAdminDto.email } }); if (!admin) { - throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); + throw new HttpException("Invalid email or password or username", HttpStatus.UNAUTHORIZED); } const isValidPassword = await bcrypt.compare(loginAdminDto.password, admin.password); if (!isValidPassword) { - throw new HttpException("Invalid email or password", HttpStatus.UNAUTHORIZED); + throw new HttpException("Invalid email or password or username", HttpStatus.UNAUTHORIZED); } const token = this.jwtService.sign( @@ -59,8 +59,7 @@ export class AdminService { return { token }; } catch (error) { - console.log(error); - throw new HttpException("An error occurred during login.", HttpStatus.INTERNAL_SERVER_ERROR); + throw new HttpException(error.response, HttpStatus.INTERNAL_SERVER_ERROR); } } //edit admin profile method diff --git a/src/products/entities/product.entity.ts b/src/products/entities/product.entity.ts index 3f0c516..c1210a5 100644 --- a/src/products/entities/product.entity.ts +++ b/src/products/entities/product.entity.ts @@ -19,4 +19,41 @@ export class Product extends Model { allowNull: false, }) price: number; + + @Column({ + type: DataType.STRING, + allowNull: true, + }) + imageUrl: string; + + @Column({ + type: DataType.ARRAY(DataType.STRING), + allowNull: true, + }) + tags: string[]; + + @Column({ + type: DataType.INTEGER, + allowNull: false, + defaultValue: 0, + }) + quantity: number; + + @Column({ + type: DataType.STRING, + allowNull: true, + }) + brand: string; + + @Column({ + type: DataType.STRING, + allowNull: true, + }) + color: string; + + @Column({ + type: DataType.STRING, + allowNull: false, + }) + category: string; } diff --git a/src/products/products.controller.ts b/src/products/products.controller.ts index a050409..e93cc5d 100644 --- a/src/products/products.controller.ts +++ b/src/products/products.controller.ts @@ -1,10 +1,12 @@ -import { Controller, Get, Post, Body, Param, Delete, Query, Put } from "@nestjs/common"; +import { Controller, Get, Post, Body, Param, Delete, Query, Put, UseGuards } from "@nestjs/common"; import { ProductsService } from "./products.service"; import { Product } from "./entities/product.entity"; +import { RoleGuard } from "src/guard/role.guard"; @Controller("products") export class ProductsController { constructor(private readonly productsService: ProductsService) {} + @UseGuards(RoleGuard) @Post() async create(@Body() body: { name: string; description: string; price: number }) { const { name, description, price } = body; diff --git a/src/products/products.module.ts b/src/products/products.module.ts index f9d8400..00ab141 100644 --- a/src/products/products.module.ts +++ b/src/products/products.module.ts @@ -3,10 +3,17 @@ import { ProductsService } from "./products.service"; import { ProductsController } from "./products.controller"; import { SequelizeModule } from "@nestjs/sequelize"; import { Product } from "./entities/product.entity"; +import { RoleGuard } from "src/guard/role.guard"; +import { JwtModule } from "@nestjs/jwt"; @Module({ - imports: [SequelizeModule.forFeature([Product])], + imports: [SequelizeModule.forFeature([Product]), + JwtModule.register({ + secret: process.env.JWT_SECRET, + signOptions: { expiresIn: '1h' }, + }) +], controllers: [ProductsController], - providers: [ProductsService], + providers: [ProductsService,RoleGuard], }) export class ProductsModule {}