8.9 KiB
E-Commerce Website - Full Setup Guide
This is a complete, fully functional e-commerce website for selling clothes and shoes with both backend and frontend fully connected.
Project Structure
ecommerce/
├── backend/ # FastAPI backend
│ ├── app/
│ │ ├── main.py # Main FastAPI app
│ │ ├── config.py # Configuration
│ │ ├── database/ # Database setup
│ │ ├── models/ # SQLAlchemy models
│ │ ├── schemas/ # Pydantic schemas
│ │ ├── services/ # Business logic
│ │ └── routers/ # API endpoints
│ ├── seed.py # Seed data script
│ ├── requirements.txt # Python dependencies
│ └── .env.example # Environment variables example
│
└── frontend/ # React + Vite frontend
├── src/
│ ├── components/ # Reusable components
│ ├── pages/ # Page components
│ ├── context/ # React context
│ ├── styles/ # CSS styles
│ ├── App.jsx # Main App component
│ ├── main.jsx # Entry point
│ └── api.js # API client
├── index.html # HTML template
├── package.json # NPM dependencies
├── vite.config.js # Vite configuration
└── .env.example # Environment variables example
System Requirements
- Python 3.8+
- Node.js 16+
- PostgreSQL 12+ (running locally or accessible)
- npm or yarn
Backend Setup
1. Install PostgreSQL
Download and install PostgreSQL from https://www.postgresql.org/download/
After installation:
# Create database
psql -U postgres
CREATE DATABASE ecommerce_db;
\q
2. Create Virtual Environment
cd backend
# On Windows
python -m venv venv
venv\Scripts\activate
# On macOS/Linux
python3 -m venv venv
source venv/bin/activate
3. Install Dependencies
pip install -r requirements.txt
4. Configure Environment Variables
Copy .env.example to .env and update with your database credentials:
cp .env.example .env
Edit .env:
DATABASE_URL=postgresql://postgres:your_password@localhost:5432/ecommerce_db
JWT_SECRET_KEY=your-super-secret-key-change-this
JWT_ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
FRONTEND_URL=http://localhost:5173
5. Create Database Tables
python seed.py
This will create all tables and seed sample data including categories, products (with focus on shoes), and test users.
6. Run Backend Server
uvicorn app.main:app --reload --port 8000
Server will be available at: http://localhost:8000
API documentation: http://localhost:8000/docs
Frontend Setup
1. Install Node Modules
cd frontend
npm install
2. Configure Environment Variables
Copy .env.example to .env:
cp .env.example .env
Edit .env:
VITE_API_URL=http://localhost:8000/api
VITE_APP_NAME=StyleHub
3. Run Development Server
npm run dev
Frontend will be available at: http://localhost:5173
4. Build for Production
npm run build
Features
Authentication
- User registration and login
- JWT token-based authentication
- User profile management
- Persistent login using localStorage
Products
- Browse all products
- Filter by category, gender, price
- Search products by name or brand
- Product detail pages with images, specifications
- Product reviews and ratings
- Featured products and new arrivals
Shopping
- Add/remove items from cart
- Update quantities
- View cart with price calculations
- Checkout process
- Order history
User Features
- User registration
- Login/logout
- User profile with shipping address
- Order history
- Wishlist/favorites
- Add/remove from wishlist
Admin Features
- Add new products
- Edit existing products
- Delete products
- Manage stock quantities
- Mark products as featured or on sale
Pages
- Home - Hero section, featured products, categories, deals
- Products - Product listing with filters and sorting
- Product Detail - Full product information
- Categories - Men, Women, Shoes, Shirts, Pants, Hats, Accessories
- Sales - Discounted products and limited-time offers
- About - Company story and values
- Contact - Contact form, store information
- Cart - Shopping cart with summary
- Checkout - Order confirmation and shipping address
- Login/Register - Authentication pages
- Profile - User account information
- Orders - Order history and tracking
- Wishlist - Saved favorite products
API Endpoints
Authentication
POST /api/auth/register- Register new userPOST /api/auth/login- Login userPOST /api/auth/verify-token- Verify JWT token
Users
GET /api/users/me- Get current user profilePUT /api/users/me- Update user profileGET /api/users/{user_id}- Get user by ID
Products
GET /api/products- List products (with filters)GET /api/products/search- Search productsGET /api/products/{id}- Get product detailsPOST /api/products- Create product (admin)PUT /api/products/{id}- Update product (admin)DELETE /api/products/{id}- Delete product (admin)
Categories
GET /api/categories- List all categoriesGET /api/categories/{id}- Get categoryPOST /api/categories- Create category (admin)PUT /api/categories/{id}- Update category (admin)DELETE /api/categories/{id}- Delete category (admin)
Cart
GET /api/cart- Get user's cartPOST /api/cart/add- Add item to cartPUT /api/cart/{item_id}- Update cart itemDELETE /api/cart/{item_id}- Remove item from cartDELETE /api/cart- Clear cart
Orders
POST /api/orders- Create orderGET /api/orders/user/orders- Get user's ordersGET /api/orders/{id}- Get order details
Wishlist
GET /api/wishlist- Get user's wishlistPOST /api/wishlist/{product_id}- Add to wishlistDELETE /api/wishlist/{product_id}- Remove from wishlist
Contact
POST /api/contact- Send contact message
Test Credentials
Demo account for testing:
- Email: user@example.com
- Password: password123
Or create a new account through the registration page.
Database Schema
Users Table
- id (Primary Key)
- email (Unique)
- hashed_password
- full_name
- phone, address, city, postal_code, country
- is_active
- created_at
Products Table
- id (Primary Key)
- name, description
- price, discount_price
- category_id (Foreign Key)
- gender (men/women)
- brand
- sizes (JSON array)
- colors (JSON array)
- stock (quantity)
- images (JSON array)
- is_featured, is_on_sale
- created_at
Categories Table
- id (Primary Key)
- name (Unique)
- slug (Unique)
- description
Cart & CartItems Tables
- cart_id (Foreign Key to User)
- cart_item_id (Product, quantity, size, color)
Orders & OrderItems Tables
- order_id (User, status, total, shipping info)
- order_item_id (Product, quantity, price at purchase time)
Wishlist Table
- user_id, product_id (Foreign Keys)
- created_at
ContactMessages Table
- id (Primary Key)
- name, email, subject, message
- created_at
Troubleshooting
Backend Issues
PostgreSQL connection error:
- Ensure PostgreSQL is running
- Check DATABASE_URL in .env
- Verify username and password
Import errors:
- Make sure virtual environment is activated
- Run
pip install -r requirements.txtagain
Frontend Issues
API not responding:
- Check backend is running on port 8000
- Check VITE_API_URL in .env
Port already in use:
# Find and kill process on port
# On Windows
netstat -ano | findstr :5173
taskkill /PID <PID> /F
# On macOS/Linux
lsof -ti:5173 | xargs kill -9
Development Notes
- Backend is built with FastAPI for high performance
- Frontend uses React with Vite for fast development
- SQLAlchemy handles database operations
- JWT for secure authentication
- CORS enabled for frontend-backend communication
- All responses follow RESTful API standards
Production Deployment
For production:
- Set DEBUG=False in backend config
- Use a production database server
- Configure proper CORS settings
- Use environment-specific .env files
- Deploy frontend as static assets
- Use a production WSGI server (Gunicorn)
- Set up HTTPS/SSL certificates
Contributing
This is a template project. Feel free to customize and extend with:
- Payment gateway integration
- Email notifications
- Advanced analytics
- Admin dashboard
- Mobile app
- Inventory management system
License
This project is open source and available for personal and commercial use.
Support
For issues or questions, check:
- FastAPI docs: https://fastapi.tiangolo.com/
- React docs: https://react.dev/
- SQLAlchemy docs: https://docs.sqlalchemy.org/
- Vite docs: https://vitejs.dev/