|
|
|
@ -11,7 +11,7 @@ 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,
|
|
|
|
|
@InjectModel(Product) private readonly productModel: typeof Product, |
|
|
|
|
) {} |
|
|
|
|
|
|
|
|
|
async addToCart(userId: number, productId: number, quantity: number): Promise<CartResponse> { |
|
|
|
@ -24,8 +24,8 @@ export class CartService { |
|
|
|
|
if (!user) { |
|
|
|
|
throw new HttpException("User not found!", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findByPk(productId);
|
|
|
|
|
|
|
|
|
|
const product = await this.productModel.findByPk(productId); |
|
|
|
|
if (!product) { |
|
|
|
|
throw new HttpException("Product not found!", HttpStatus.NOT_FOUND); |
|
|
|
|
} |
|
|
|
@ -57,4 +57,23 @@ export class CartService { |
|
|
|
|
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); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|