Enhance product models

master
nicekid1 2 months ago
parent 6945420e27
commit 716c4a6782
  1. 65
      migrations/20250104112403-create-product.js
  2. 7
      src/admin/admin.service.ts
  3. 37
      src/products/entities/product.entity.ts
  4. 4
      src/products/products.controller.ts
  5. 11
      src/products/products.module.ts

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

@ -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

@ -19,4 +19,41 @@ export class Product extends Model<Product> {
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;
}

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

@ -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 {}

Loading…
Cancel
Save