- Added CartItem, CartSummary, CartEmpty, CartSidebar, and CartSheet components for managing cart display and interactions. - Integrated useCart and useCartUI composables for cart state management and UI control. - Implemented API endpoints for cart operations, including fetching, adding, updating, and removing items. - Enhanced user experience with loading states and notifications using vue-sonner for cart actions. - Configured session management for guest and authenticated users, ensuring cart persistence across sessions. This commit completes the shopping cart feature, enabling users to add items, view their cart, and proceed to checkout. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
45 lines
1.1 KiB
TypeScript
45 lines
1.1 KiB
TypeScript
/**
|
|
* GET /api/cart
|
|
*
|
|
* Get the current user's shopping cart with all items
|
|
*
|
|
* Features:
|
|
* - Returns cart for authenticated users (by userId)
|
|
* - Returns cart for guest users (by sessionId)
|
|
* - Automatically removes unavailable products (inactive or out of stock)
|
|
* - Calculates totals and subtotals
|
|
* - Returns list of removed items if any were auto-cleaned
|
|
*
|
|
* Response:
|
|
* {
|
|
* cart: { id, userId, sessionId, createdAt, updatedAt },
|
|
* items: [{ id, product, quantity, subtotal, addedAt }],
|
|
* total: number,
|
|
* itemCount: number,
|
|
* removedItems?: string[] // Names of removed products
|
|
* }
|
|
*/
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
try {
|
|
// Get or create cart for current user/session
|
|
const cart = await getOrCreateCart(event)
|
|
|
|
// Get cart with items (auto-cleans unavailable products)
|
|
const cartSummary = await getCartWithItems(cart.id)
|
|
|
|
return cartSummary
|
|
} catch (error) {
|
|
// Log error for debugging
|
|
console.error('Error fetching cart:', error)
|
|
|
|
// Return empty cart on error
|
|
return {
|
|
cart: null,
|
|
items: [],
|
|
total: 0,
|
|
itemCount: 0,
|
|
}
|
|
}
|
|
})
|