A comprehensive ticket service API built with FastAPI and SQLModel, featuring order management, cart functionality, user management, and analytics.
- Ticket Management: Create, read, update, delete tickets with status tracking
- Order Management: Complete order lifecycle from creation to completion
- Shopping Cart: Add tickets to cart, manage quantities, checkout
- User Management: User registration, profile management, order history
- Transaction Processing: Payment processing and transaction tracking
- Analytics: Comprehensive reporting and analytics dashboard
- User: User accounts and profiles
- Ticket: Event tickets with availability tracking
- Order: Order management with status tracking
- OrderItem: Individual items within orders
- Cart: Shopping cart functionality
- Transaction: Payment and transaction records
- UserOrder: Additional user-order relationships
Ticket_Service/
├── main.py # FastAPI application entry point
├── database.py # Database configuration and session management
├── models.py # SQLModel database models
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── Ticket/ # Ticket management module
│ ├── __init__.py
│ ├── routers/
│ │ ├── __init__.py
│ │ └── ticket.py # Ticket CRUD operations
│ └── services/
│ └── __init__.py
└── Order/ # Order management module
├── __init__.py
├── routers/
│ ├── __init__.py
│ ├── order.py # Order CRUD operations
│ ├── cart.py # Cart management
│ ├── user.py # User management
│ ├── transaction.py # Transaction management
│ └── analytics.py # Analytics and reporting
└── services/
├── __init__.py
└── order_service.py # Complex order business logic
- Clone the repository
git clone <repository-url>
cd Ticket_Service- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Set up environment variables
cp .env.example .env
# Edit .env file with your configuration- Run the application
uvicorn main:app --reloadThe API will be available at http://localhost:8000
Once the application is running, you can access:
- Interactive API docs:
http://localhost:8000/docs - Alternative docs:
http://localhost:8000/redoc
POST /- Create ticketGET /- List tickets with filteringGET /{ticket_id}- Get specific ticketPUT /{ticket_id}- Update ticketDELETE /{ticket_id}- Delete ticketPATCH /{ticket_id}/status- Update ticket statusGET /search/by-event- Search tickets by criteria
POST /- Create orderGET /- List orders with filteringGET /{order_id}- Get specific orderPUT /{order_id}- Update orderDELETE /{order_id}- Delete orderPOST /{order_id}/items- Add item to orderGET /{order_id}/items- Get order itemsPOST /from-cart/{user_id}- Create order from cartPATCH /{order_id}/status- Update order statusPOST /{order_id}/cancel- Cancel order
POST /- Add item to cartGET /user/{user_id}- Get user's cartPUT /{cart_item_id}- Update cart itemDELETE /{cart_item_id}- Remove from cartDELETE /user/{user_id}/clear- Clear user's cartGET /user/{user_id}/total- Get cart total
POST /- Create userGET /- List usersGET /{user_id}- Get specific userGET /username/{username}- Get user by usernamePUT /{user_id}- Update userDELETE /{user_id}- Delete userPATCH /{user_id}/deactivate- Deactivate userPATCH /{user_id}/activate- Activate user
POST /- Create transactionGET /- List transactionsGET /{transaction_id}- Get specific transactionPUT /{transaction_id}- Update transactionPATCH /{transaction_id}/status- Update transaction statusPOST /{transaction_id}/refund- Process refundGET /order/{order_id}- Get order transactions
GET /dashboard- Dashboard analyticsGET /sales- Sales analyticsGET /revenue- Revenue analyticsGET /users- User analyticsGET /tickets- Ticket analytics
curl -X POST "http://localhost:8000/api/users/" \
-H "Content-Type: application/json" \
-d '{
"username": "john_doe",
"email": "john@example.com",
"full_name": "John Doe",
"phone_number": "+1234567890"
}'curl -X POST "http://localhost:8000/api/tickets/" \
-H "Content-Type: application/json" \
-d '{
"event_name": "Concert 2024",
"event_description": "Amazing concert event",
"event_date": "2024-12-25T20:00:00",
"venue": "Main Arena",
"price": 99.99,
"available_quantity": 100,
"total_quantity": 100,
"category": "VIP"
}'curl -X POST "http://localhost:8000/api/cart/" \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"ticket_id": 1,
"quantity": 2
}'curl -X POST "http://localhost:8000/api/orders/from-cart/1"- TicketStatus: available, reserved, sold, cancelled
- OrderStatus: pending, confirmed, cancelled, completed
- TransactionStatus: pending, success, failed, refunded
- User → Orders (One-to-Many)
- User → Cart Items (One-to-Many)
- Order → Order Items (One-to-Many)
- Ticket → Order Items (One-to-Many)
- Order → Transactions (One-to-Many)
# Install test dependencies
pip install pytest pytest-asyncio httpx
# Run tests
pytestThe project follows Python best practices:
- Type hints throughout
- Pydantic models for data validation
- SQLModel for database operations
- FastAPI for API framework
- Environment Variables: Set production values in
.env - Database: Configure production database (PostgreSQL recommended)
- Security: Update SECRET_KEY and implement proper authentication
- CORS: Configure CORS settings for your frontend
- Load Balancer: Use nginx or similar for production deployment
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.