Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

38 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›’ GreenBuy - E-commerce API Platform

πŸ“‹ Tα»•ng quan dα»± Γ‘n

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.

πŸ—οΈ KiαΊΏn trΓΊc hệ thα»‘ng

Backend Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   FastAPI App   β”‚    β”‚   PostgreSQL    β”‚    β”‚   WebSocket     β”‚
β”‚   (Python 3.12) │◄──►│   (TimescaleDB) β”‚    β”‚   (Chat)        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   REST API      β”‚    β”‚   Database      β”‚    β”‚   Real-time     β”‚
β”‚   Endpoints     β”‚    β”‚   Migrations    β”‚    β”‚   Messaging     β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Technology Stack

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

πŸ—„οΈ Database Schema

Core Entities & Relationships

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
    }
Loading

πŸš€ API Endpoints

Authentication & User Management

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 profile

Shop Management

GET    /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 statistics

Product Management

GET    /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 status

Cart & Orders

GET    /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 status

Payment System

GET    /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 refund

Real-time Chat

GET    /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

πŸ”§ CΓ i Δ‘αΊ·t & ChαΊ‘y

Prerequisites

  • Docker & Docker Compose
  • Python 3.12+
  • PostgreSQL (TimescaleDB)

Quick Start

  1. Clone repository
git clone <repository-url>
cd GreenBuy
  1. Setup environment
cp .env.example .env
# Edit .env with your database credentials
  1. Run with Docker
docker-compose up -d
  1. Run migrations
docker-compose exec app alembic upgrade head
  1. Access API

Development Setup

  1. Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies
pip install -r requirements.txt
  1. Run development server
cd src
uvicorn main:app --reload --host 0.0.0.0 --port 8002

πŸ“Š TΓ­nh nΔƒng chΓ­nh

πŸ›οΈ E-commerce Features

  • βœ… 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

πŸ’¬ Social Features

  • βœ… Chat real-time giα»―a users
  • βœ… Follow/Unfollow users vΓ  shops
  • βœ… Rating vΓ  review system
  • βœ… Event tracking

πŸ” Security Features

  • βœ… JWT Authentication
  • βœ… Password hashing vα»›i bcrypt
  • βœ… Role-based access control
  • βœ… Token blacklisting
  • βœ… Rate limiting

πŸ“± Mobile-Friendly

  • βœ… RESTful API design
  • βœ… Pagination support
  • βœ… File upload handling
  • βœ… Real-time notifications
  • βœ… Optimized responses

πŸ—‚οΈ Project Structure

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

πŸ”„ Database Migrations

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

πŸ§ͺ Testing

# Run tests (if available)
pytest

# Run with coverage
pytest --cov=src

πŸ“ˆ Performance

  • Database: TimescaleDB cho time-series data
  • Caching: Redis (cΓ³ thể thΓͺm)
  • Load Balancing: Docker Compose
  • Monitoring: Health check endpoints

πŸ”’ Security

  • Authentication: JWT tokens
  • Password: bcrypt hashing
  • CORS: Configured for cross-origin requests
  • Input Validation: Pydantic models
  • SQL Injection: SQLModel ORM protection

πŸ“ API Documentation

Truy cαΊ­p Swagger UI tαΊ‘i: http://localhost:8002/docs

🀝 Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ“ž Support


GreenBuy - Modern E-commerce Platform πŸš€

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages