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