GreenBuy lΓ mα»t nα»n tαΊ£ng thΖ°Ζ‘ng mαΊ‘i Δiα»n tα» hiα»n ΔαΊ‘i Δược xΓ’y dα»±ng vα»i FastAPI vΓ PostgreSQL. Hα» thα»ng hα» trợ ΔαΊ§y Δα»§ cΓ‘c tΓnh nΔng tα»« quαΊ£n lΓ½ sαΊ£n phαΊ©m, giα» hΓ ng, ΔΖ‘n hΓ ng ΔαΊΏn thanh toΓ‘n vΓ chat real-time.
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β FastAPI App β β PostgreSQL β β WebSocket β
β (Python 3.12) βββββΊβ (TimescaleDB) β β (Chat) β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β β β
βΌ βΌ βΌ
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
β REST API β β Database β β Real-time β
β Endpoints β β Migrations β β Messaging β
βββββββββββββββββββ βββββββββββββββββββ βββββββββββββββββββ
| Layer | Technology | Version | Purpose |
|---|---|---|---|
| Framework | FastAPI | 0.115.12 | Modern, fast web framework |
| Database | PostgreSQL + TimescaleDB | 17 | Time-series & relational data |
| ORM | SQLModel | Latest | Type-safe database operations |
| Authentication | JWT + bcrypt | Latest | Secure user authentication |
| Real-time | WebSocket | Latest | Live chat functionality |
| Container | Docker | Latest | Containerization |
| Deployment | Docker Compose | Latest | Multi-service orchestration |
erDiagram
users ||--o{ addresses : has
users ||--o{ orders : places
users ||--o{ payment_methods : owns
users ||--o{ user_follows : follows
users ||--o{ shop_follows : follows
users ||--o{ user_ratings : rates
users ||--o{ shop_ratings : rates
users ||--o{ chat_rooms : participates
users ||--o{ chat_messages : sends
users ||--o{ events : tracks
users ||--|| cart : has
users ||--|| shop : owns
users ||--o{ approved_products : approves
users ||--o{ refund_requests : requests
users ||--o{ online_status : tracks
shop ||--o{ products : sells
shop ||--o{ shop_follows : followed_by
shop ||--o{ shop_ratings : rated_by
category ||--o{ sub_categories : contains
sub_categories ||--o{ products : categorizes
products ||--o{ attributes : has
products ||--o{ order_items : included_in
products ||--o{ approved_products : approved_by
attributes ||--o{ cart_items : selected_in
attributes ||--o{ order_items : specified_in
cart ||--o{ cart_items : contains
orders ||--o{ order_items : contains
orders ||--o{ payments : paid_by
payment_methods ||--o{ payments : used_for
payments ||--o{ refund_requests : refunded_by
chat_rooms ||--o{ chat_messages : contains
chat_messages ||--o{ chat_messages : replies_to
users {
int id PK
string email
string password_hash
string username
string first_name
string last_name
string avatar
datetime birth_date
string phone_number
string bio
boolean is_active
boolean is_online
boolean is_verified
enum role
datetime created_at
datetime updated_at
datetime last_login
int failed_login_attempts
datetime locked_until
datetime password_changed_at
string email_verification_token
datetime email_verified_at
string password_reset_token
datetime password_reset_expires
}
shop {
int id PK
int user_id FK
string name
string avatar
string phone_number
boolean is_active
boolean is_online
datetime create_at
}
category {
int id PK
string name
string description
datetime created_at
}
sub_categories {
int id PK
int category_id FK
string name
string description
datetime created_at
}
products {
int product_id PK
int shop_id FK
int sub_category_id FK
string name
string description
string cover
float price
boolean is_approved
string approval_note
int approver_id FK
datetime create_at
}
attributes {
int attribute_id PK
int product_id FK
string color
string size
float price
string image
int quantity
datetime create_at
}
cart {
int id PK
int user_id FK
datetime created_at
datetime updated_at
}
cart_items {
int id PK
int cart_id FK
int attribute_id FK
int quantity
datetime added_at
}
orders {
int id PK
string order_number
int user_id FK
int status
float subtotal
float tax_amount
float shipping_fee
float discount_amount
float total_amount
string shipping_address
string billing_address
string phone_number
string recipient_name
string delivery_notes
datetime created_at
datetime updated_at
datetime confirmed_at
datetime shipped_at
datetime delivered_at
datetime cancelled_at
string notes
string internal_notes
}
order_items {
int id PK
int order_id FK
int product_id FK
int attribute_id FK
string product_name
string product_image
string attribute_details
int quantity
float unit_price
float total_price
datetime created_at
}
payment_method {
int id PK
int user_id FK
enum type
string card_number
string card_holder_name
int expiry_month
int expiry_year
string paypal_email
string bank_name
string account_number
string account_holder
boolean is_default
boolean is_active
datetime created_at
datetime updated_at
}
payments {
int id PK
int order_id FK
int payment_method_id FK
float amount
string currency
string status
string transaction_id
string gateway_response
string failure_reason
datetime created_at
datetime processed_at
datetime completed_at
string notes
}
refund_requests {
int id PK
int payment_id FK
int user_id FK
float amount
string reason
string status
string admin_note
int processed_by FK
datetime created_at
datetime processed_at
}
addresses {
int id PK
int user_id FK
string street
string city
string state
string zipcode
string country
string phone_number
boolean is_default
datetime created_at
}
user_follows {
int id PK
int follower_id FK
int following_id FK
datetime created_at
}
shop_follows {
int id PK
int user_id FK
int shop_id FK
datetime created_at
}
user_ratings {
int id PK
int rater_id FK
int rated_user_id FK
int rating
string comment
datetime created_at
datetime updated_at
}
shop_ratings {
int id PK
int user_id FK
int shop_id FK
int rating
string comment
datetime created_at
datetime updated_at
}
chat_rooms {
int id PK
int user1_id FK
int user2_id FK
datetime created_at
datetime updated_at
boolean is_active
int last_message_id
datetime last_activity
}
chat_messages {
int id PK
int room_id FK
int sender_id FK
string content
enum type
enum status
datetime timestamp
string file_url
int file_size
string file_name
string thumbnail_url
int duration
float latitude
float longitude
int reply_to_id FK
boolean is_edited
datetime edited_at
boolean is_deleted
datetime deleted_at
}
online_status {
int user_id PK
boolean is_online
datetime last_seen
string device_info
datetime updated_at
}
events {
int id PK
int user_id FK
string page
string descriptions
datetime created_at
datetime update_at
}
POST /token # Login
POST /token/refresh # Refresh token
POST /logout # Logout
POST /logout-all # Logout all devices
GET /api/user/me # Get current user
PUT /api/user/profile # Update profileGET /api/shops/me # Get my shop
POST /api/shops/ # Create shop
PUT /api/shops/me # Update shop
GET /api/shops/{id} # Get shop by ID
GET /api/shops/me/stats # Shop statisticsGET /api/product/ # List products (with pagination)
POST /api/product/ # Create product
GET /api/product/{id} # Get product details
PUT /api/product/{id} # Update product
DELETE /api/product/{id} # Delete product
GET /api/product/by-status/{status} # Filter by statusGET /api/cart/me # Get my cart
POST /api/cart/items # Add to cart
PUT /api/cart/items/{id} # Update cart item
DELETE /api/cart/items/{id} # Remove from cart
GET /api/order/ # List orders
POST /api/order/ # Create order
GET /api/order/{id} # Get order details
PATCH /api/order/{id}/status # Update order statusGET /api/payment/methods # List payment methods
POST /api/payment/methods # Add payment method
POST /api/payment/process/{order_id} # Process payment
POST /api/payment/refund # Request refundGET /api/chat/rooms # List chat rooms
POST /api/chat/rooms # Create chat room
GET /api/chat/rooms/{id}/messages # Get messages
POST /api/chat/rooms/{id}/messages # Send message- Docker & Docker Compose
- Python 3.12+
- PostgreSQL (TimescaleDB)
- Clone repository
git clone <repository-url>
cd GreenBuy- Setup environment
cp .env.example .env
# Edit .env with your database credentials- Run with Docker
docker-compose up -d- Run migrations
docker-compose exec app alembic upgrade head- Access API
- API Documentation: http://localhost:8002/docs
- Health Check: http://localhost:8002/healthz
- Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies
pip install -r requirements.txt- Run development server
cd src
uvicorn main:app --reload --host 0.0.0.0 --port 8002- β QuαΊ£n lΓ½ sαΊ£n phαΊ©m vα»i attributes (mΓ u sαΊ―c, kΓch thΖ°α»c)
- β Giα» hΓ ng thΓ΄ng minh
- β Hα» thα»ng ΔΖ‘n hΓ ng vα»i trαΊ‘ng thΓ‘i chi tiαΊΏt
- β Thanh toΓ‘n Δa phΖ°Ζ‘ng thα»©c
- β ΔΓ‘nh giΓ‘ vΓ review
- β Theo dΓ΅i shop/user
- β Chat real-time giα»―a users
- β Follow/Unfollow users vΓ shops
- β Rating vΓ review system
- β Event tracking
- β JWT Authentication
- β Password hashing vα»i bcrypt
- β Role-based access control
- β Token blacklisting
- β Rate limiting
- β RESTful API design
- β Pagination support
- β File upload handling
- β Real-time notifications
- β Optimized responses
GreenBuy/
βββ src/
β βββ api/
β β βββ auth/ # Authentication & JWT
β β βββ user/ # User management
β β βββ shop/ # Shop management
β β βββ product/ # Product management
β β βββ cart/ # Shopping cart
β β βββ order/ # Order management
β β βββ payment/ # Payment processing
β β βββ chat/ # Real-time chat
β β βββ address/ # Address management
β β βββ category/ # Product categories
β β βββ attribute/ # Product attributes
β β βββ db/ # Database configuration
β βββ static/ # Static files (images)
β βββ alembic/ # Database migrations
β βββ main.py # FastAPI application
βββ docker-compose.yml # Docker orchestration
βββ Dockerfile.web # Docker configuration
βββ requirements.txt # Python dependencies
βββ README.md # This file
Hα» thα»ng sα» dα»₯ng Alembic Δα» quαΊ£n lΓ½ database migrations:
# TαΊ‘o migration mα»i
alembic revision --autogenerate -m "Description"
# ChαΊ‘y migrations
alembic upgrade head
# Rollback migration
alembic downgrade -1# Run tests (if available)
pytest
# Run with coverage
pytest --cov=src- Database: TimescaleDB cho time-series data
- Caching: Redis (cΓ³ thα» thΓͺm)
- Load Balancing: Docker Compose
- Monitoring: Health check endpoints
- Authentication: JWT tokens
- Password: bcrypt hashing
- CORS: Configured for cross-origin requests
- Input Validation: Pydantic models
- SQL Injection: SQLModel ORM protection
Truy cαΊp Swagger UI tαΊ‘i: http://localhost:8002/docs
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Email: support@greenbuy.com
- Documentation: API Docs
- Issues: GitHub Issues
GreenBuy - Modern E-commerce Platform π