Basket Service Features
The Basket service provides four core features for managing shopping carts in the e-commerce platform.Get Basket
Retrieve a user’s shopping cart with all items and calculated total price.Implementation
Query Handler (GetBasketHandler.cs:6-15):
GetBasketEndpoints.cs:10-16):
Behavior
- Checks Redis cache first for performance
- Falls back to PostgreSQL if not cached
- Automatically caches result for subsequent requests
- Throws
BasketNotFoundExceptionif basket doesn’t exist
Response Model
Store Basket
Create or update a shopping cart with automatic discount application.Implementation
Command Handler (StoreBasketHandler.cs:17-39):
StoreBasketEndpoints.cs:10-18):
Key Features
- Discount Integration: Automatically fetches and applies discounts via gRPC
- Price Calculation: Deducts coupon amounts from item prices
- Dual Persistence: Saves to both PostgreSQL and Redis cache
- Validation: Ensures cart and username are provided
Validation Rules
Request Model
Delete Basket
Remove a shopping cart from both database and cache.Implementation
Command Handler (DeleteBasketHandler.cs:14-24):
DeleteBasketEndpoints.cs:10-16):
Behavior
- Removes basket from PostgreSQL
- Invalidates Redis cache entry
- Returns success status
Validation Rules
Checkout Basket
Process basket checkout, publish event to message broker, and clean up cart.Implementation
Command Handler (CheckoutBasketHandler.cs:21-47):
CheckoutBasketEndpoints.cs:10-18):
Checkout Process
- Retrieve Basket: Gets the basket with calculated total price
- Create Event: Maps checkout data to
BasketCheckoutEvent - Set Total Price: Adds calculated total to the event
- Publish Event: Sends event to RabbitMQ via MassTransit
- Clean Up: Deletes the basket from storage and cache
Checkout Data Transfer Object
Validation Rules
Event-Driven Integration
The checkout operation publishes aBasketCheckoutEvent that triggers downstream processes:
- Order creation in the Ordering service
- Payment processing
- Inventory updates
- Email notifications
Price Calculation
The shopping cart automatically calculates the total price:- Multiplies each item’s price by quantity
- Prices already include applied discounts from gRPC calls
- Calculated on-the-fly when accessed
Error Handling
All features include comprehensive error handling:- Validation Errors: Returns 400 Bad Request with validation details
- Not Found Errors: Returns 404 when basket doesn’t exist
- Server Errors: Returns 500 with problem details
Related Topics
- API Reference - Complete endpoint documentation
- Caching - Redis caching implementation
- Overview - Service architecture
