parent
47910e1ac6
commit
9e665f8710
7 changed files with 211 additions and 91 deletions
@ -0,0 +1,62 @@ |
||||
'use strict'; |
||||
|
||||
module.exports = { |
||||
up: async (queryInterface, Sequelize) => { |
||||
await queryInterface.createTable('Carts', { |
||||
id: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
autoIncrement: true, |
||||
primaryKey: true, |
||||
}, |
||||
userId: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
references: { |
||||
model: 'Users', |
||||
key: 'id', |
||||
}, |
||||
onDelete: 'CASCADE', |
||||
}, |
||||
productId: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
references: { |
||||
model: 'Products',
|
||||
key: 'id', |
||||
}, |
||||
onDelete: 'CASCADE', |
||||
}, |
||||
quantity: { |
||||
type: Sequelize.INTEGER, |
||||
allowNull: false, |
||||
}, |
||||
productPrice: { |
||||
type: Sequelize.DECIMAL(10, 2), |
||||
allowNull: false, |
||||
}, |
||||
totalPrice: { |
||||
type: Sequelize.DECIMAL(10, 2), |
||||
allowNull: false, |
||||
}, |
||||
productName: { |
||||
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('Carts'); |
||||
}, |
||||
}; |
@ -1,26 +1,46 @@ |
||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
||||
import { Controller, Get, Post, Patch, Delete, Body, Param, UseGuards, Request } from "@nestjs/common"; |
||||
import { CartService } from "./cart.service"; |
||||
import { JwtAuthGuard } from "src/guard/auth.guard"; |
||||
import { AddToCartDto } from "./dto/add-to-cart.dto"; |
||||
import { UpdateCartDto } from "./dto/update-cart.dto"; |
||||
import { Cart } from "./entities/cart.entity"; |
||||
|
||||
@Controller("cart") |
||||
export class CartController { |
||||
constructor(private readonly cartService: CartService) {} |
||||
|
||||
@UseGuards(JwtAuthGuard) |
||||
@Post() |
||||
async addToCart(@Body() body: { userId: number; productId: number; quantity: number }): Promise<{ message: string; cartItem: Cart }> { |
||||
const { userId, productId, quantity } = body; |
||||
const result = await this.cartService.addToCart(userId, productId, quantity); |
||||
return result; |
||||
async addToCart(@Body() addToCartDto: AddToCartDto, @Request() req: any): Promise<{ message: string; cartItem: Cart }> { |
||||
const userId = req.user.id; |
||||
return this.cartService.addToCart({ ...addToCartDto, userId }); |
||||
} |
||||
@Get(":userId") |
||||
async getUserCart(@Param("userId") userId: number) { |
||||
|
||||
@UseGuards(JwtAuthGuard) |
||||
@Get() |
||||
async getUserCart(@Request() req: any): Promise<{ cartItems: Cart[]; totalPrice: number }> { |
||||
const userId = req.user.id; |
||||
return this.cartService.getUserCart(userId); |
||||
} |
||||
|
||||
@Delete(":userId/:productId") |
||||
async removeFromCart( |
||||
@Param("userId") userId: number, |
||||
@Param("productId") productId: number,
|
||||
): Promise<{ message: string }> { |
||||
return this.cartService.removeFromCart(userId, productId); |
||||
@UseGuards(JwtAuthGuard) |
||||
@Patch(":productId") |
||||
async updateCart(@Param("productId") productId: number, @Body() updateCartDto: UpdateCartDto, @Request() req: any): Promise<{ message: string; updatedCart: Cart }> { |
||||
const userId = req.user.id; |
||||
const updatedCart = await this.cartService.updateCart(userId, productId, updateCartDto.quantity); |
||||
return { |
||||
message: "Cart updated successfully", |
||||
updatedCart, |
||||
}; |
||||
} |
||||
|
||||
@UseGuards(JwtAuthGuard) |
||||
@Delete(":productId") |
||||
async removeFromCart(@Param("productId") productId: number, @Request() req: any): Promise<{ message: string }> { |
||||
const userId = req.user.id; |
||||
await this.cartService.removeFromCart(userId, productId); |
||||
return { |
||||
message: "Product removed from cart successfully", |
||||
}; |
||||
} |
||||
} |
||||
|
@ -0,0 +1,13 @@ |
||||
// add-to-cart.dto.ts
|
||||
import { IsInt, IsNotEmpty, IsNumber, min, Min } from 'class-validator'; |
||||
|
||||
export class AddToCartDto { |
||||
@IsNumber()
|
||||
@IsNotEmpty() |
||||
productId: number; |
||||
|
||||
@IsInt() |
||||
@IsNotEmpty() |
||||
@Min(0) |
||||
quantity: number; |
||||
} |
@ -0,0 +1,8 @@ |
||||
// update-cart.dto.ts
|
||||
import { IsInt, Min } from 'class-validator'; |
||||
|
||||
export class UpdateCartDto { |
||||
@IsInt() |
||||
@Min(1) |
||||
quantity: number; |
||||
} |
Loading…
Reference in new issue