Compare commits
No commits in common. '81d2629d0539c0a5999cc945a94fb71e57c3237d' and '199c2b35a21cb8c56b4ab408f6451da8f5d63031' have entirely different histories.
81d2629d05
...
199c2b35a2
6 changed files with 11 additions and 105 deletions
@ -1,18 +1,8 @@ |
|||||||
import { Controller, Get, Post, Body, Patch, Param, Delete } from "@nestjs/common"; |
import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common'; |
||||||
import { CartService } from "./cart.service"; |
import { CartService } from './cart.service'; |
||||||
import { Cart } from "./entities/cart.entity"; |
|
||||||
|
|
||||||
@Controller("cart") |
@Controller('cart') |
||||||
export class CartController { |
export class CartController { |
||||||
constructor(private readonly cartService: CartService) {} |
constructor(private readonly cartService: CartService) {} |
||||||
@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; |
|
||||||
} |
|
||||||
@Get(':userId') |
|
||||||
async getUserCart(@Param('userId') userId: number) { |
|
||||||
return this.cartService.getUserCart(userId); |
|
||||||
} |
|
||||||
} |
} |
||||||
|
@ -1,6 +0,0 @@ |
|||||||
import { Cart } from "./entities/cart.entity"; |
|
||||||
|
|
||||||
export interface CartResponse { |
|
||||||
message: string; |
|
||||||
cartItem: Cart;
|
|
||||||
} |
|
@ -1,79 +1,7 @@ |
|||||||
import { Injectable } from "@nestjs/common"; |
import { Injectable } from '@nestjs/common'; |
||||||
import { InjectModel } from "@nestjs/sequelize"; |
|
||||||
import { Cart } from "./entities/cart.entity"; |
|
||||||
import { HttpException, HttpStatus } from "@nestjs/common"; |
|
||||||
import { CartResponse } from "./cart.response"; |
|
||||||
import { User } from "src/users/entities/user.entity"; |
|
||||||
import { Product } from "src/products/entities/product.entity"; |
|
||||||
|
|
||||||
@Injectable() |
@Injectable() |
||||||
export class CartService { |
export class CartService { |
||||||
constructor( |
|
||||||
@InjectModel(Cart) private readonly cartModel: typeof Cart, |
|
||||||
@InjectModel(User) private readonly userModel: typeof User, |
|
||||||
@InjectModel(Product) private readonly productModel: typeof Product, |
|
||||||
) {} |
|
||||||
|
|
||||||
async addToCart(userId: number, productId: number, quantity: number): Promise<CartResponse> { |
|
||||||
try { |
|
||||||
if (!userId || !productId || !quantity) { |
|
||||||
throw new HttpException("Missing required parameters: userId, productId, and quantity are required.", HttpStatus.BAD_REQUEST); |
|
||||||
} |
|
||||||
|
|
||||||
const user = await this.userModel.findByPk(userId); |
|
||||||
if (!user) { |
|
||||||
throw new HttpException("User not found!", HttpStatus.NOT_FOUND); |
|
||||||
} |
|
||||||
|
|
||||||
const product = await this.productModel.findByPk(productId); |
|
||||||
if (!product) { |
|
||||||
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
||||||
} |
|
||||||
|
|
||||||
const existingCartItem = await this.cartModel.findOne({ |
|
||||||
where: { userId, productId }, |
|
||||||
}); |
|
||||||
|
|
||||||
if (existingCartItem) { |
|
||||||
existingCartItem.quantity += Number(quantity); |
|
||||||
await existingCartItem.save(); |
|
||||||
return { |
|
||||||
message: "Product quantity updated in cart successfully!", |
|
||||||
cartItem: existingCartItem, |
|
||||||
}; |
|
||||||
} |
|
||||||
|
|
||||||
const newCartItem = await this.cartModel.create({ userId, productId, quantity }); |
|
||||||
|
|
||||||
return { |
|
||||||
message: "Product added to cart successfully!", |
|
||||||
cartItem: newCartItem, |
|
||||||
}; |
|
||||||
} catch (error) { |
|
||||||
// Enhanced error handling
|
|
||||||
if (error instanceof HttpException) { |
|
||||||
throw error; |
|
||||||
} |
|
||||||
throw new HttpException(`An error occurred while adding the product to the cart: ${error.message}`, HttpStatus.INTERNAL_SERVER_ERROR); |
|
||||||
} |
|
||||||
} |
|
||||||
async getUserCart(userId: number): Promise<Cart[]> { |
|
||||||
try { |
|
||||||
const cartItems = await this.cartModel.findAll({ |
|
||||||
where: { userId }, |
|
||||||
include: ["product"], |
|
||||||
}); |
|
||||||
if (!cartItems || cartItems.length === 0) { |
|
||||||
throw new HttpException("No cart items found for the specified user.", HttpStatus.NOT_FOUND); |
|
||||||
} |
|
||||||
|
|
||||||
return cartItems; |
|
||||||
} catch (error) { |
|
||||||
if (error instanceof HttpException) { |
|
||||||
throw error; |
|
||||||
} |
|
||||||
|
|
||||||
throw new HttpException("An error occurred while retrieving the cart.", HttpStatus.INTERNAL_SERVER_ERROR); |
|
||||||
} |
|
||||||
} |
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue