Create FastAPI E-commerce API with SQLite
This commit includes: - Project structure setup with FastAPI - Database models with SQLAlchemy (users, products, categories, orders) - SQLite database configuration - Alembic migration scripts - API endpoints for authentication, users, products, categories, and orders - JWT authentication - Comprehensive documentation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
d4c82d1ee7
commit
4e92bb1338
150
README.md
150
README.md
@ -1,3 +1,149 @@
|
||||
# FastAPI Application
|
||||
# E-commerce API
|
||||
|
||||
This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform.
|
||||
A RESTful API for an e-commerce application built with FastAPI and SQLite.
|
||||
|
||||
## Features
|
||||
|
||||
- User authentication and authorization
|
||||
- Product management
|
||||
- Category management
|
||||
- Shopping cart and order processing
|
||||
- Admin dashboard
|
||||
|
||||
## Tech Stack
|
||||
|
||||
- **Framework**: FastAPI
|
||||
- **Database**: SQLite with SQLAlchemy ORM
|
||||
- **Migration**: Alembic
|
||||
- **Authentication**: JWT tokens
|
||||
- **API Documentation**: Swagger UI and ReDoc (auto-generated)
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
/
|
||||
├── alembic/ # Database migrations
|
||||
├── app/ # Main application directory
|
||||
│ ├── api/ # API endpoints
|
||||
│ │ ├── endpoints/ # API route handlers
|
||||
│ ├── core/ # Core components
|
||||
│ ├── crud/ # CRUD operations
|
||||
│ ├── db/ # Database sessions and connections
|
||||
│ ├── models/ # SQLAlchemy models
|
||||
│ └── schemas/ # Pydantic models/schemas
|
||||
├── main.py # FastAPI application instance
|
||||
└── requirements.txt # Project dependencies
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
1. **Clone the repository**
|
||||
|
||||
2. **Install dependencies**:
|
||||
```bash
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. **Run database migrations**:
|
||||
```bash
|
||||
alembic upgrade head
|
||||
```
|
||||
|
||||
4. **Start the API server**:
|
||||
```bash
|
||||
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
|
||||
```
|
||||
|
||||
## API Endpoints
|
||||
|
||||
### Authentication
|
||||
|
||||
- `POST /api/v1/auth/register` - Register a new user
|
||||
- `POST /api/v1/auth/login` - Login and get access token
|
||||
|
||||
### Users
|
||||
|
||||
- `GET /api/v1/users/me` - Get current user info
|
||||
- `PUT /api/v1/users/me` - Update current user info
|
||||
- `GET /api/v1/users/{user_id}` - Get user by ID
|
||||
- `GET /api/v1/users/` - Get all users (admin only)
|
||||
- `PUT /api/v1/users/{user_id}` - Update user by ID (admin only)
|
||||
|
||||
### Categories
|
||||
|
||||
- `GET /api/v1/categories/` - List all categories
|
||||
- `POST /api/v1/categories/` - Create a new category (admin only)
|
||||
- `GET /api/v1/categories/{category_id}` - Get category by ID
|
||||
- `PUT /api/v1/categories/{category_id}` - Update category (admin only)
|
||||
- `DELETE /api/v1/categories/{category_id}` - Delete category (admin only)
|
||||
|
||||
### Products
|
||||
|
||||
- `GET /api/v1/products/` - List all active products
|
||||
- `POST /api/v1/products/` - Create a new product (admin only)
|
||||
- `GET /api/v1/products/{product_id}` - Get product by ID
|
||||
- `PUT /api/v1/products/{product_id}` - Update product (admin only)
|
||||
- `DELETE /api/v1/products/{product_id}` - Delete product (admin only)
|
||||
- `GET /api/v1/products/category/{category_id}` - Get products by category
|
||||
|
||||
### Orders
|
||||
|
||||
- `GET /api/v1/orders/` - List user's orders (or all orders for admin)
|
||||
- `POST /api/v1/orders/` - Create a new order
|
||||
- `GET /api/v1/orders/{order_id}` - Get order by ID
|
||||
- `PUT /api/v1/orders/{order_id}` - Update order
|
||||
- `PUT /api/v1/orders/{order_id}/status` - Update order status (admin only)
|
||||
- `GET /api/v1/orders/status/{status}` - Get orders by status (admin only)
|
||||
|
||||
## API Documentation
|
||||
|
||||
When the server is running, the API documentation is available at:
|
||||
|
||||
- Swagger UI: [http://localhost:8000/docs](http://localhost:8000/docs)
|
||||
- ReDoc: [http://localhost:8000/redoc](http://localhost:8000/redoc)
|
||||
|
||||
## Database Schema
|
||||
|
||||
### Users
|
||||
- id: Integer (Primary Key)
|
||||
- email: String (Unique)
|
||||
- username: String (Unique)
|
||||
- hashed_password: String
|
||||
- full_name: String (Optional)
|
||||
- is_active: Boolean
|
||||
- is_admin: Boolean
|
||||
- created_at: DateTime
|
||||
- updated_at: DateTime
|
||||
|
||||
### Categories
|
||||
- id: Integer (Primary Key)
|
||||
- name: String (Unique)
|
||||
- description: Text (Optional)
|
||||
|
||||
### Products
|
||||
- id: Integer (Primary Key)
|
||||
- name: String
|
||||
- description: Text (Optional)
|
||||
- price: Float
|
||||
- stock: Integer
|
||||
- is_active: Boolean
|
||||
- category_id: Integer (Foreign Key)
|
||||
- image_url: String (Optional)
|
||||
- created_at: DateTime
|
||||
- updated_at: DateTime
|
||||
|
||||
### Orders
|
||||
- id: Integer (Primary Key)
|
||||
- user_id: Integer (Foreign Key)
|
||||
- status: Enum (PENDING, PROCESSING, SHIPPED, DELIVERED, CANCELLED)
|
||||
- total_amount: Float
|
||||
- shipping_address: String
|
||||
- created_at: DateTime
|
||||
- updated_at: DateTime
|
||||
|
||||
### Order Items
|
||||
- id: Integer (Primary Key)
|
||||
- order_id: Integer (Foreign Key)
|
||||
- product_id: Integer (Foreign Key)
|
||||
- quantity: Integer
|
||||
- price: Float (Price at the time of purchase)
|
||||
|
100
alembic.ini
Normal file
100
alembic.ini
Normal file
@ -0,0 +1,100 @@
|
||||
# A generic, single database configuration.
|
||||
|
||||
[alembic]
|
||||
# path to migration scripts
|
||||
script_location = alembic
|
||||
|
||||
# template used to generate migration files
|
||||
# file_template = %%(rev)s_%%(slug)s
|
||||
|
||||
# sys.path path, will be prepended to sys.path if present.
|
||||
# defaults to the current working directory.
|
||||
prepend_sys_path = .
|
||||
|
||||
# timezone to use when rendering the date within the migration file
|
||||
# as well as the filename.
|
||||
# If specified, requires the python-dateutil library that can be
|
||||
# installed by adding `alembic[tz]` to the pip requirements
|
||||
# timezone = UTC
|
||||
|
||||
# max length of characters to apply to the
|
||||
# "slug" field
|
||||
# truncate_slug_length = 40
|
||||
|
||||
# set to 'true' to run the environment during
|
||||
# the 'revision' command, regardless of autogenerate
|
||||
# revision_environment = false
|
||||
|
||||
# set to 'true' to allow .pyc and .pyo files without
|
||||
# a source .py file to be detected as revisions in the
|
||||
# versions/ directory
|
||||
# sourceless = false
|
||||
|
||||
# version location specification; This defaults
|
||||
# to alembic/versions. When using multiple version
|
||||
# directories, initial revisions must be specified with --version-path.
|
||||
# The path separator used here should be the separator specified by "version_path_separator" below.
|
||||
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
|
||||
|
||||
# version path separator; As mentioned above, this is the character used to split
|
||||
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
|
||||
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
|
||||
# Valid values for version_path_separator are:
|
||||
#
|
||||
# version_path_separator = :
|
||||
# version_path_separator = ;
|
||||
# version_path_separator = space
|
||||
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
|
||||
|
||||
# the output encoding used when revision files
|
||||
# are written from script.py.mako
|
||||
# output_encoding = utf-8
|
||||
|
||||
sqlalchemy.url = driver://user:pass@localhost/dbname
|
||||
|
||||
|
||||
[post_write_hooks]
|
||||
# post_write_hooks defines scripts or Python functions that are run
|
||||
# on newly generated revision scripts. See the documentation for further
|
||||
# detail and examples
|
||||
|
||||
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||
# hooks = black
|
||||
# black.type = console_scripts
|
||||
# black.entrypoint = black
|
||||
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||
|
||||
# Logging configuration
|
||||
[loggers]
|
||||
keys = root,sqlalchemy,alembic
|
||||
|
||||
[handlers]
|
||||
keys = console
|
||||
|
||||
[formatters]
|
||||
keys = generic
|
||||
|
||||
[logger_root]
|
||||
level = WARN
|
||||
handlers = console
|
||||
qualname =
|
||||
|
||||
[logger_sqlalchemy]
|
||||
level = WARN
|
||||
handlers =
|
||||
qualname = sqlalchemy.engine
|
||||
|
||||
[logger_alembic]
|
||||
level = INFO
|
||||
handlers =
|
||||
qualname = alembic
|
||||
|
||||
[handler_console]
|
||||
class = StreamHandler
|
||||
args = (sys.stderr,)
|
||||
level = NOTSET
|
||||
formatter = generic
|
||||
|
||||
[formatter_generic]
|
||||
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||
datefmt = %H:%M:%S
|
84
alembic/env.py
Normal file
84
alembic/env.py
Normal file
@ -0,0 +1,84 @@
|
||||
from logging.config import fileConfig
|
||||
|
||||
from sqlalchemy import engine_from_config
|
||||
from sqlalchemy import pool
|
||||
|
||||
from alembic import context
|
||||
|
||||
# this is the Alembic Config object, which provides
|
||||
# access to the values within the .ini file in use.
|
||||
config = context.config
|
||||
|
||||
# Interpret the config file for Python logging.
|
||||
# This line sets up loggers basically.
|
||||
if config.config_file_name is not None:
|
||||
fileConfig(config.config_file_name)
|
||||
|
||||
# Import your models and database configuration
|
||||
from app.db.session import Base
|
||||
from app.core.config import settings
|
||||
from app.models import User, Product, Category, Order, OrderItem
|
||||
|
||||
# Set the SQLAlchemy URL in the Alembic configuration
|
||||
config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URL)
|
||||
|
||||
# add your model's MetaData object here
|
||||
# for 'autogenerate' support
|
||||
target_metadata = Base.metadata
|
||||
|
||||
# other values from the config, defined by the needs of env.py,
|
||||
# can be acquired:
|
||||
# my_important_option = config.get_main_option("my_important_option")
|
||||
# ... etc.
|
||||
|
||||
|
||||
def run_migrations_offline() -> None:
|
||||
"""Run migrations in 'offline' mode.
|
||||
|
||||
This configures the context with just a URL
|
||||
and not an Engine, though an Engine is acceptable
|
||||
here as well. By skipping the Engine creation
|
||||
we don't even need a DBAPI to be available.
|
||||
|
||||
Calls to context.execute() here emit the given string to the
|
||||
script output.
|
||||
|
||||
"""
|
||||
url = config.get_main_option("sqlalchemy.url")
|
||||
context.configure(
|
||||
url=url,
|
||||
target_metadata=target_metadata,
|
||||
literal_binds=True,
|
||||
dialect_opts={"paramstyle": "named"},
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
def run_migrations_online() -> None:
|
||||
"""Run migrations in 'online' mode.
|
||||
|
||||
In this scenario we need to create an Engine
|
||||
and associate a connection with the context.
|
||||
|
||||
"""
|
||||
connectable = engine_from_config(
|
||||
config.get_section(config.config_ini_section, {}),
|
||||
prefix="sqlalchemy.",
|
||||
poolclass=pool.NullPool,
|
||||
)
|
||||
|
||||
with connectable.connect() as connection:
|
||||
context.configure(
|
||||
connection=connection, target_metadata=target_metadata
|
||||
)
|
||||
|
||||
with context.begin_transaction():
|
||||
context.run_migrations()
|
||||
|
||||
|
||||
if context.is_offline_mode():
|
||||
run_migrations_offline()
|
||||
else:
|
||||
run_migrations_online()
|
26
alembic/script.py.mako
Normal file
26
alembic/script.py.mako
Normal file
@ -0,0 +1,26 @@
|
||||
"""${message}
|
||||
|
||||
Revision ID: ${up_revision}
|
||||
Revises: ${down_revision | comma,n}
|
||||
Create Date: ${create_date}
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
${imports if imports else ""}
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = ${repr(up_revision)}
|
||||
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
${upgrades if upgrades else "pass"}
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
${downgrades if downgrades else "pass"}
|
113
alembic/versions/01_initial.py
Normal file
113
alembic/versions/01_initial.py
Normal file
@ -0,0 +1,113 @@
|
||||
"""Initial
|
||||
|
||||
Revision ID: 01_initial
|
||||
Revises:
|
||||
Create Date: 2025-05-11
|
||||
|
||||
"""
|
||||
from typing import Sequence, Union
|
||||
|
||||
from alembic import op
|
||||
import sqlalchemy as sa
|
||||
from sqlalchemy.dialects import sqlite
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision: str = '01_initial'
|
||||
down_revision: Union[str, None] = None
|
||||
branch_labels: Union[str, Sequence[str], None] = None
|
||||
depends_on: Union[str, Sequence[str], None] = None
|
||||
|
||||
|
||||
def upgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
# Create users table
|
||||
op.create_table('users',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('email', sa.String(), nullable=False),
|
||||
sa.Column('username', sa.String(), nullable=False),
|
||||
sa.Column('hashed_password', sa.String(), nullable=False),
|
||||
sa.Column('full_name', sa.String(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||
sa.Column('is_admin', sa.Boolean(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
|
||||
op.create_index(op.f('ix_users_id'), 'users', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_users_username'), 'users', ['username'], unique=True)
|
||||
|
||||
# Create categories table
|
||||
op.create_table('categories',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_categories_id'), 'categories', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_categories_name'), 'categories', ['name'], unique=True)
|
||||
|
||||
# Create products table
|
||||
op.create_table('products',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('name', sa.String(), nullable=False),
|
||||
sa.Column('description', sa.Text(), nullable=True),
|
||||
sa.Column('price', sa.Float(), nullable=False),
|
||||
sa.Column('stock', sa.Integer(), nullable=True),
|
||||
sa.Column('is_active', sa.Boolean(), nullable=True),
|
||||
sa.Column('category_id', sa.Integer(), nullable=True),
|
||||
sa.Column('image_url', sa.String(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['category_id'], ['categories.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_products_id'), 'products', ['id'], unique=False)
|
||||
op.create_index(op.f('ix_products_name'), 'products', ['name'], unique=False)
|
||||
|
||||
# Create orders table
|
||||
op.create_table('orders',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('user_id', sa.Integer(), nullable=True),
|
||||
sa.Column('status', sa.Enum('PENDING', 'PROCESSING', 'SHIPPED', 'DELIVERED', 'CANCELLED', name='orderstatus'), nullable=True),
|
||||
sa.Column('total_amount', sa.Float(), nullable=True),
|
||||
sa.Column('shipping_address', sa.String(), nullable=True),
|
||||
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=True),
|
||||
sa.Column('updated_at', sa.DateTime(timezone=True), nullable=True),
|
||||
sa.ForeignKeyConstraint(['user_id'], ['users.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_orders_id'), 'orders', ['id'], unique=False)
|
||||
|
||||
# Create order_items table
|
||||
op.create_table('order_items',
|
||||
sa.Column('id', sa.Integer(), nullable=False),
|
||||
sa.Column('order_id', sa.Integer(), nullable=True),
|
||||
sa.Column('product_id', sa.Integer(), nullable=True),
|
||||
sa.Column('quantity', sa.Integer(), nullable=True),
|
||||
sa.Column('price', sa.Float(), nullable=True),
|
||||
sa.ForeignKeyConstraint(['order_id'], ['orders.id'], ),
|
||||
sa.ForeignKeyConstraint(['product_id'], ['products.id'], ),
|
||||
sa.PrimaryKeyConstraint('id')
|
||||
)
|
||||
op.create_index(op.f('ix_order_items_id'), 'order_items', ['id'], unique=False)
|
||||
# ### end Alembic commands ###
|
||||
|
||||
|
||||
def downgrade() -> None:
|
||||
# ### commands auto generated by Alembic - please adjust! ###
|
||||
op.drop_index(op.f('ix_order_items_id'), table_name='order_items')
|
||||
op.drop_table('order_items')
|
||||
op.drop_index(op.f('ix_orders_id'), table_name='orders')
|
||||
op.drop_table('orders')
|
||||
op.drop_index(op.f('ix_products_name'), table_name='products')
|
||||
op.drop_index(op.f('ix_products_id'), table_name='products')
|
||||
op.drop_table('products')
|
||||
op.drop_index(op.f('ix_categories_name'), table_name='categories')
|
||||
op.drop_index(op.f('ix_categories_id'), table_name='categories')
|
||||
op.drop_table('categories')
|
||||
op.drop_index(op.f('ix_users_username'), table_name='users')
|
||||
op.drop_index(op.f('ix_users_id'), table_name='users')
|
||||
op.drop_index(op.f('ix_users_email'), table_name='users')
|
||||
op.drop_table('users')
|
||||
# ### end Alembic commands ###
|
0
app/__init__.py
Normal file
0
app/__init__.py
Normal file
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
10
app/api/api.py
Normal file
10
app/api/api.py
Normal file
@ -0,0 +1,10 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.endpoints import auth, users, products, categories, orders
|
||||
|
||||
api_router = APIRouter()
|
||||
api_router.include_router(auth.router, prefix="/auth", tags=["authentication"])
|
||||
api_router.include_router(users.router, prefix="/users", tags=["users"])
|
||||
api_router.include_router(products.router, prefix="/products", tags=["products"])
|
||||
api_router.include_router(categories.router, prefix="/categories", tags=["categories"])
|
||||
api_router.include_router(orders.router, prefix="/orders", tags=["orders"])
|
71
app/api/deps.py
Normal file
71
app/api/deps.py
Normal file
@ -0,0 +1,71 @@
|
||||
from typing import Generator, Optional
|
||||
|
||||
from fastapi import Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordBearer
|
||||
from jose import jwt, JWTError
|
||||
from pydantic import ValidationError
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.session import SessionLocal
|
||||
from app.models.user import User
|
||||
from app.core.config import settings
|
||||
from app.core.security import ALGORITHM
|
||||
from app.crud.user import user
|
||||
from app.schemas.user import TokenPayload
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(
|
||||
tokenUrl=f"{settings.API_V1_STR}/auth/login"
|
||||
)
|
||||
|
||||
|
||||
def get_db() -> Generator:
|
||||
try:
|
||||
db = SessionLocal()
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
def get_current_user(
|
||||
db: Session = Depends(get_db), token: str = Depends(oauth2_scheme)
|
||||
) -> User:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
token, settings.SECRET_KEY, algorithms=[ALGORITHM]
|
||||
)
|
||||
token_data = TokenPayload(**payload)
|
||||
except (JWTError, ValidationError):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="Could not validate credentials",
|
||||
)
|
||||
|
||||
db_user = user.get(db, id=token_data.sub)
|
||||
if not db_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found",
|
||||
)
|
||||
return db_user
|
||||
|
||||
|
||||
def get_current_active_user(
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> User:
|
||||
if not user.is_active(current_user):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Inactive user",
|
||||
)
|
||||
return current_user
|
||||
|
||||
|
||||
def get_current_admin_user(
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
) -> User:
|
||||
if not user.is_admin(current_user):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="The user doesn't have enough privileges",
|
||||
)
|
||||
return current_user
|
0
app/api/endpoints/__init__.py
Normal file
0
app/api/endpoints/__init__.py
Normal file
71
app/api/endpoints/auth.py
Normal file
71
app/api/endpoints/auth.py
Normal file
@ -0,0 +1,71 @@
|
||||
from datetime import timedelta
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from fastapi.security import OAuth2PasswordRequestForm
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_db
|
||||
from app.core.config import settings
|
||||
from app.core.security import create_access_token
|
||||
from app.crud.user import user
|
||||
from app.schemas.user import Token, User, UserCreate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/login", response_model=Token)
|
||||
def login_access_token(
|
||||
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
||||
) -> Any:
|
||||
"""
|
||||
OAuth2 compatible token login, get an access token for future requests.
|
||||
"""
|
||||
auth_user = user.authenticate(
|
||||
db, email=form_data.username, password=form_data.password
|
||||
)
|
||||
if not auth_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||
detail="Incorrect email or password",
|
||||
)
|
||||
if not user.is_active(auth_user):
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Inactive user",
|
||||
)
|
||||
|
||||
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||
|
||||
return {
|
||||
"access_token": create_access_token(
|
||||
auth_user.id, expires_delta=access_token_expires
|
||||
),
|
||||
"token_type": "bearer",
|
||||
}
|
||||
|
||||
|
||||
@router.post("/register", response_model=User)
|
||||
def register_new_user(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
user_in: UserCreate,
|
||||
) -> Any:
|
||||
"""
|
||||
Create new user.
|
||||
"""
|
||||
db_user = user.get_by_email(db, email=user_in.email)
|
||||
if db_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Email already registered",
|
||||
)
|
||||
|
||||
db_user = user.get_by_username(db, username=user_in.username)
|
||||
if db_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Username already registered",
|
||||
)
|
||||
|
||||
return user.create(db, obj_in=user_in)
|
108
app/api/endpoints/categories.py
Normal file
108
app/api/endpoints/categories.py
Normal file
@ -0,0 +1,108 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_user, get_db
|
||||
from app.crud.product import category
|
||||
from app.models.user import User
|
||||
from app.schemas.product import Category, CategoryCreate, CategoryUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Category])
|
||||
def read_categories(
|
||||
db: Session = Depends(get_db),
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve categories.
|
||||
"""
|
||||
return category.get_multi(db, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.post("/", response_model=Category)
|
||||
def create_category(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
category_in: CategoryCreate,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Create new category. Only for admins.
|
||||
"""
|
||||
db_category = category.get_by_name(db, name=category_in.name)
|
||||
if db_category:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Category with this name already exists",
|
||||
)
|
||||
return category.create(db, obj_in=category_in)
|
||||
|
||||
|
||||
@router.get("/{category_id}", response_model=Category)
|
||||
def read_category(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
category_id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Get category by ID.
|
||||
"""
|
||||
db_category = category.get(db, id=category_id)
|
||||
if not db_category:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Category not found",
|
||||
)
|
||||
return db_category
|
||||
|
||||
|
||||
@router.put("/{category_id}", response_model=Category)
|
||||
def update_category(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
category_id: int,
|
||||
category_in: CategoryUpdate,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update a category. Only for admins.
|
||||
"""
|
||||
db_category = category.get(db, id=category_id)
|
||||
if not db_category:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Category not found",
|
||||
)
|
||||
|
||||
if category_in.name:
|
||||
existing = category.get_by_name(db, name=category_in.name)
|
||||
if existing and existing.id != category_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Category with this name already exists",
|
||||
)
|
||||
|
||||
return category.update(db, db_obj=db_category, obj_in=category_in)
|
||||
|
||||
|
||||
@router.delete("/{category_id}", response_model=Category)
|
||||
def delete_category(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
category_id: int,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Delete a category. Only for admins.
|
||||
"""
|
||||
db_category = category.get(db, id=category_id)
|
||||
if not db_category:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Category not found",
|
||||
)
|
||||
return category.remove(db, id=category_id)
|
161
app/api/endpoints/orders.py
Normal file
161
app/api/endpoints/orders.py
Normal file
@ -0,0 +1,161 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_active_user, get_current_admin_user, get_db
|
||||
from app.crud.order import order, order_item
|
||||
from app.models.order import OrderStatus
|
||||
from app.models.user import User
|
||||
from app.schemas.order import (
|
||||
Order, OrderCreate, OrderUpdate,
|
||||
OrderItem, OrderItemCreate, OrderItemUpdate
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/", response_model=List[Order])
|
||||
def read_orders(
|
||||
db: Session = Depends(get_db),
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve orders.
|
||||
"""
|
||||
if current_user.is_admin:
|
||||
return order.get_multi(db, skip=skip, limit=limit)
|
||||
return order.get_by_user(db, user_id=current_user.id, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.post("/", response_model=Order)
|
||||
def create_order(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
order_in: OrderCreate,
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Create new order.
|
||||
"""
|
||||
# Ensure user can only create orders for themselves
|
||||
if order_in.user_id != current_user.id and not current_user.is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You can only create orders for yourself",
|
||||
)
|
||||
|
||||
try:
|
||||
return order.create(db, obj_in=order_in)
|
||||
except ValueError as e:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/{order_id}", response_model=Order)
|
||||
def read_order(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
order_id: int,
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Get order by ID.
|
||||
"""
|
||||
db_order = order.get(db, id=order_id)
|
||||
if not db_order:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Order not found",
|
||||
)
|
||||
|
||||
# Ensure users can only view their own orders unless they're admins
|
||||
if db_order.user_id != current_user.id and not current_user.is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You can only view your own orders",
|
||||
)
|
||||
|
||||
return db_order
|
||||
|
||||
|
||||
@router.put("/{order_id}/status", response_model=Order)
|
||||
def update_order_status(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
order_id: int,
|
||||
status: OrderStatus,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update order status. Only for admins.
|
||||
"""
|
||||
db_order = order.get(db, id=order_id)
|
||||
if not db_order:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Order not found",
|
||||
)
|
||||
|
||||
return order.update_status(db, db_obj=db_order, status=status)
|
||||
|
||||
|
||||
@router.put("/{order_id}", response_model=Order)
|
||||
def update_order(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
order_id: int,
|
||||
order_in: OrderUpdate,
|
||||
current_user: User = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update an order.
|
||||
"""
|
||||
db_order = order.get(db, id=order_id)
|
||||
if not db_order:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Order not found",
|
||||
)
|
||||
|
||||
# Users can only update their own orders
|
||||
if db_order.user_id != current_user.id and not current_user.is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You can only update your own orders",
|
||||
)
|
||||
|
||||
# Regular users can only update the shipping address if the order is still pending
|
||||
if not current_user.is_admin and db_order.status != OrderStatus.PENDING:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="You can only update pending orders",
|
||||
)
|
||||
|
||||
# Regular users can't update status
|
||||
if order_in.status and not current_user.is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="You cannot update order status",
|
||||
)
|
||||
|
||||
return order.update(db, db_obj=db_order, obj_in=order_in)
|
||||
|
||||
|
||||
@router.get("/status/{status}", response_model=List[Order])
|
||||
def read_orders_by_status(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
status: OrderStatus,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Get orders by status. Only for admins.
|
||||
"""
|
||||
return order.get_by_status(db, status=status, skip=skip, limit=limit)
|
117
app/api/endpoints/products.py
Normal file
117
app/api/endpoints/products.py
Normal file
@ -0,0 +1,117 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_admin_user, get_current_active_user, get_db
|
||||
from app.crud.product import product, category
|
||||
from app.models.user import User
|
||||
from app.schemas.product import (
|
||||
Product, ProductCreate, ProductUpdate,
|
||||
Category, CategoryCreate, CategoryUpdate,
|
||||
)
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
# Product routes
|
||||
@router.get("/", response_model=List[Product])
|
||||
def read_products(
|
||||
db: Session = Depends(get_db),
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve products.
|
||||
"""
|
||||
return product.get_active(db, skip=skip, limit=limit)
|
||||
|
||||
|
||||
@router.post("/", response_model=Product)
|
||||
def create_product(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
product_in: ProductCreate,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Create new product. Only for admins.
|
||||
"""
|
||||
return product.create(db, obj_in=product_in)
|
||||
|
||||
|
||||
@router.get("/{product_id}", response_model=Product)
|
||||
def read_product(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
product_id: int,
|
||||
) -> Any:
|
||||
"""
|
||||
Get product by ID.
|
||||
"""
|
||||
db_product = product.get(db, id=product_id)
|
||||
if not db_product:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Product not found",
|
||||
)
|
||||
return db_product
|
||||
|
||||
|
||||
@router.put("/{product_id}", response_model=Product)
|
||||
def update_product(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
product_id: int,
|
||||
product_in: ProductUpdate,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update a product. Only for admins.
|
||||
"""
|
||||
db_product = product.get(db, id=product_id)
|
||||
if not db_product:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Product not found",
|
||||
)
|
||||
return product.update(db, db_obj=db_product, obj_in=product_in)
|
||||
|
||||
|
||||
@router.delete("/{product_id}", response_model=Product)
|
||||
def delete_product(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
product_id: int,
|
||||
current_user: User = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Delete a product. Only for admins.
|
||||
"""
|
||||
db_product = product.get(db, id=product_id)
|
||||
if not db_product:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Product not found",
|
||||
)
|
||||
return product.remove(db, id=product_id)
|
||||
|
||||
|
||||
@router.get("/category/{category_id}", response_model=List[Product])
|
||||
def read_products_by_category(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
category_id: int,
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
) -> Any:
|
||||
"""
|
||||
Get products by category.
|
||||
"""
|
||||
db_category = category.get(db, id=category_id)
|
||||
if not db_category:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Category not found",
|
||||
)
|
||||
return product.get_by_category(db, category_id=category_id, skip=skip, limit=limit)
|
95
app/api/endpoints/users.py
Normal file
95
app/api/endpoints/users.py
Normal file
@ -0,0 +1,95 @@
|
||||
from typing import Any, List
|
||||
|
||||
from fastapi import APIRouter, Body, Depends, HTTPException, status
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.api.deps import get_current_active_user, get_current_admin_user, get_db
|
||||
from app.crud.user import user
|
||||
from app.models.user import User as UserModel
|
||||
from app.schemas.user import User, UserUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/me", response_model=User)
|
||||
def read_user_me(
|
||||
current_user: UserModel = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Get current user.
|
||||
"""
|
||||
return current_user
|
||||
|
||||
|
||||
@router.put("/me", response_model=User)
|
||||
def update_user_me(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
user_in: UserUpdate,
|
||||
current_user: UserModel = Depends(get_current_active_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update own user.
|
||||
"""
|
||||
return user.update(db, db_obj=current_user, obj_in=user_in)
|
||||
|
||||
|
||||
@router.get("/{user_id}", response_model=User)
|
||||
def read_user_by_id(
|
||||
user_id: int,
|
||||
current_user: UserModel = Depends(get_current_active_user),
|
||||
db: Session = Depends(get_db),
|
||||
) -> Any:
|
||||
"""
|
||||
Get a specific user by id.
|
||||
"""
|
||||
db_user = user.get(db, id=user_id)
|
||||
if not db_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found",
|
||||
)
|
||||
|
||||
if user_id != current_user.id and not current_user.is_admin:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN,
|
||||
detail="The user doesn't have enough privileges",
|
||||
)
|
||||
|
||||
return db_user
|
||||
|
||||
|
||||
@router.get("/", response_model=List[User])
|
||||
def read_users(
|
||||
db: Session = Depends(get_db),
|
||||
skip: int = 0,
|
||||
limit: int = 100,
|
||||
current_user: UserModel = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Retrieve users. Only for admins.
|
||||
"""
|
||||
users = user.get_multi(db, skip=skip, limit=limit)
|
||||
return users
|
||||
|
||||
|
||||
@router.put("/{user_id}", response_model=User)
|
||||
def update_user(
|
||||
*,
|
||||
db: Session = Depends(get_db),
|
||||
user_id: int,
|
||||
user_in: UserUpdate,
|
||||
current_user: UserModel = Depends(get_current_admin_user),
|
||||
) -> Any:
|
||||
"""
|
||||
Update a user. Only for admins.
|
||||
"""
|
||||
db_user = user.get(db, id=user_id)
|
||||
if not db_user:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="User not found",
|
||||
)
|
||||
|
||||
return user.update(db, db_obj=db_user, obj_in=user_in)
|
0
app/core/__init__.py
Normal file
0
app/core/__init__.py
Normal file
27
app/core/config.py
Normal file
27
app/core/config.py
Normal file
@ -0,0 +1,27 @@
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from typing import Any, Dict, Optional
|
||||
|
||||
from pydantic import BaseSettings
|
||||
|
||||
# Build paths
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent.parent
|
||||
|
||||
class Settings(BaseSettings):
|
||||
API_V1_STR: str = "/api/v1"
|
||||
SECRET_KEY: str = secrets.token_urlsafe(32)
|
||||
# 60 minutes * 24 hours * 8 days = 8 days
|
||||
ACCESS_TOKEN_EXPIRE_MINUTES: int = 60 * 24 * 8
|
||||
|
||||
# CORS
|
||||
BACKEND_CORS_ORIGINS: list[str] = ["*"]
|
||||
|
||||
# Database
|
||||
DB_DIR = BASE_DIR.parent / "storage" / "db"
|
||||
DB_DIR.mkdir(parents=True, exist_ok=True)
|
||||
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||
|
||||
class Config:
|
||||
case_sensitive = True
|
||||
|
||||
settings = Settings()
|
33
app/core/security.py
Normal file
33
app/core/security.py
Normal file
@ -0,0 +1,33 @@
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, Union
|
||||
|
||||
from jose import jwt
|
||||
from passlib.context import CryptContext
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
||||
|
||||
ALGORITHM = "HS256"
|
||||
|
||||
|
||||
def create_access_token(
|
||||
subject: Union[str, Any], expires_delta: timedelta = None
|
||||
) -> str:
|
||||
if expires_delta:
|
||||
expire = datetime.utcnow() + expires_delta
|
||||
else:
|
||||
expire = datetime.utcnow() + timedelta(
|
||||
minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES
|
||||
)
|
||||
to_encode = {"exp": expire, "sub": str(subject)}
|
||||
encoded_jwt = jwt.encode(to_encode, settings.SECRET_KEY, algorithm=ALGORITHM)
|
||||
return encoded_jwt
|
||||
|
||||
|
||||
def verify_password(plain_password: str, hashed_password: str) -> bool:
|
||||
return pwd_context.verify(plain_password, hashed_password)
|
||||
|
||||
|
||||
def get_password_hash(password: str) -> str:
|
||||
return pwd_context.hash(password)
|
3
app/crud/__init__.py
Normal file
3
app/crud/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from app.crud.user import user
|
||||
from app.crud.product import product, category
|
||||
from app.crud.order import order, order_item
|
66
app/crud/base.py
Normal file
66
app/crud/base.py
Normal file
@ -0,0 +1,66 @@
|
||||
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||
|
||||
from fastapi.encoders import jsonable_encoder
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.session import Base
|
||||
|
||||
ModelType = TypeVar("ModelType", bound=Base)
|
||||
CreateSchemaType = TypeVar("CreateSchemaType", bound=BaseModel)
|
||||
UpdateSchemaType = TypeVar("UpdateSchemaType", bound=BaseModel)
|
||||
|
||||
|
||||
class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
||||
def __init__(self, model: Type[ModelType]):
|
||||
"""
|
||||
CRUD object with default methods to Create, Read, Update, Delete (CRUD).
|
||||
|
||||
**Parameters**
|
||||
|
||||
* `model`: A SQLAlchemy model class
|
||||
* `schema`: A Pydantic model (schema) class
|
||||
"""
|
||||
self.model = model
|
||||
|
||||
def get(self, db: Session, id: Any) -> Optional[ModelType]:
|
||||
return db.query(self.model).filter(self.model.id == id).first()
|
||||
|
||||
def get_multi(
|
||||
self, db: Session, *, skip: int = 0, limit: int = 100
|
||||
) -> List[ModelType]:
|
||||
return db.query(self.model).offset(skip).limit(limit).all()
|
||||
|
||||
def create(self, db: Session, *, obj_in: CreateSchemaType) -> ModelType:
|
||||
obj_in_data = jsonable_encoder(obj_in)
|
||||
db_obj = self.model(**obj_in_data)
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update(
|
||||
self,
|
||||
db: Session,
|
||||
*,
|
||||
db_obj: ModelType,
|
||||
obj_in: Union[UpdateSchemaType, Dict[str, Any]]
|
||||
) -> ModelType:
|
||||
obj_data = jsonable_encoder(db_obj)
|
||||
if isinstance(obj_in, dict):
|
||||
update_data = obj_in
|
||||
else:
|
||||
update_data = obj_in.dict(exclude_unset=True)
|
||||
for field in obj_data:
|
||||
if field in update_data:
|
||||
setattr(db_obj, field, update_data[field])
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def remove(self, db: Session, *, id: int) -> ModelType:
|
||||
obj = db.query(self.model).get(id)
|
||||
db.delete(obj)
|
||||
db.commit()
|
||||
return obj
|
107
app/crud/order.py
Normal file
107
app/crud/order.py
Normal file
@ -0,0 +1,107 @@
|
||||
from typing import List, Optional, Dict, Any, Union
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.crud.base import CRUDBase
|
||||
from app.models.order import Order, OrderItem, OrderStatus
|
||||
from app.schemas.order import OrderCreate, OrderUpdate, OrderItemCreate, OrderItemUpdate
|
||||
from app.models.product import Product
|
||||
|
||||
|
||||
class CRUDOrderItem(CRUDBase[OrderItem, OrderItemCreate, OrderItemUpdate]):
|
||||
def create_with_order(
|
||||
self, db: Session, *, obj_in: OrderItemCreate, order_id: int
|
||||
) -> OrderItem:
|
||||
# Get product to get current price if not provided
|
||||
product = db.query(Product).filter(Product.id == obj_in.product_id).first()
|
||||
if not product:
|
||||
raise ValueError(f"Product with id {obj_in.product_id} not found")
|
||||
|
||||
# Check if stock is available
|
||||
if product.stock < obj_in.quantity:
|
||||
raise ValueError(f"Not enough stock for product {product.name}")
|
||||
|
||||
# Create order item
|
||||
db_obj = OrderItem(
|
||||
order_id=order_id,
|
||||
product_id=obj_in.product_id,
|
||||
quantity=obj_in.quantity,
|
||||
price=obj_in.price if obj_in.price else product.price,
|
||||
)
|
||||
|
||||
# Update product stock
|
||||
product.stock -= obj_in.quantity
|
||||
|
||||
db.add(db_obj)
|
||||
db.add(product)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
|
||||
order_item = CRUDOrderItem(OrderItem)
|
||||
|
||||
|
||||
class CRUDOrder(CRUDBase[Order, OrderCreate, OrderUpdate]):
|
||||
def get_by_user(
|
||||
self, db: Session, *, user_id: int, skip: int = 0, limit: int = 100
|
||||
) -> List[Order]:
|
||||
return (
|
||||
db.query(Order)
|
||||
.filter(Order.user_id == user_id)
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
|
||||
def get_by_status(
|
||||
self, db: Session, *, status: OrderStatus, skip: int = 0, limit: int = 100
|
||||
) -> List[Order]:
|
||||
return (
|
||||
db.query(Order)
|
||||
.filter(Order.status == status)
|
||||
.offset(skip)
|
||||
.limit(limit)
|
||||
.all()
|
||||
)
|
||||
|
||||
def create(self, db: Session, *, obj_in: OrderCreate) -> Order:
|
||||
# Calculate total amount from items
|
||||
total_amount = 0
|
||||
for item in obj_in.items:
|
||||
product = db.query(Product).filter(Product.id == item.product_id).first()
|
||||
if not product:
|
||||
raise ValueError(f"Product with id {item.product_id} not found")
|
||||
if product.stock < item.quantity:
|
||||
raise ValueError(f"Not enough stock for product {product.name}")
|
||||
price = item.price if item.price else product.price
|
||||
total_amount += price * item.quantity
|
||||
|
||||
# Create order
|
||||
db_obj = Order(
|
||||
user_id=obj_in.user_id,
|
||||
status=OrderStatus.PENDING,
|
||||
total_amount=total_amount,
|
||||
shipping_address=obj_in.shipping_address,
|
||||
)
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
|
||||
# Create order items
|
||||
for item in obj_in.items:
|
||||
order_item.create_with_order(db=db, obj_in=item, order_id=db_obj.id)
|
||||
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update_status(
|
||||
self, db: Session, *, db_obj: Order, status: OrderStatus
|
||||
) -> Order:
|
||||
db_obj.status = status
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
|
||||
order = CRUDOrder(Order)
|
29
app/crud/product.py
Normal file
29
app/crud/product.py
Normal file
@ -0,0 +1,29 @@
|
||||
from typing import List, Optional
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.crud.base import CRUDBase
|
||||
from app.models.product import Product, Category
|
||||
from app.schemas.product import ProductCreate, ProductUpdate, CategoryCreate, CategoryUpdate
|
||||
|
||||
|
||||
class CRUDProduct(CRUDBase[Product, ProductCreate, ProductUpdate]):
|
||||
def get_by_name(self, db: Session, *, name: str) -> Optional[Product]:
|
||||
return db.query(Product).filter(Product.name == name).first()
|
||||
|
||||
def get_by_category(self, db: Session, *, category_id: int, skip: int = 0, limit: int = 100) -> List[Product]:
|
||||
return db.query(Product).filter(Product.category_id == category_id).offset(skip).limit(limit).all()
|
||||
|
||||
def get_active(self, db: Session, *, skip: int = 0, limit: int = 100) -> List[Product]:
|
||||
return db.query(Product).filter(Product.is_active == True).offset(skip).limit(limit).all()
|
||||
|
||||
|
||||
product = CRUDProduct(Product)
|
||||
|
||||
|
||||
class CRUDCategory(CRUDBase[Category, CategoryCreate, CategoryUpdate]):
|
||||
def get_by_name(self, db: Session, *, name: str) -> Optional[Category]:
|
||||
return db.query(Category).filter(Category.name == name).first()
|
||||
|
||||
|
||||
category = CRUDCategory(Category)
|
59
app/crud/user.py
Normal file
59
app/crud/user.py
Normal file
@ -0,0 +1,59 @@
|
||||
from typing import Any, Dict, Optional, Union
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.core.security import get_password_hash, verify_password
|
||||
from app.crud.base import CRUDBase
|
||||
from app.models.user import User
|
||||
from app.schemas.user import UserCreate, UserUpdate
|
||||
|
||||
|
||||
class CRUDUser(CRUDBase[User, UserCreate, UserUpdate]):
|
||||
def get_by_email(self, db: Session, *, email: str) -> Optional[User]:
|
||||
return db.query(User).filter(User.email == email).first()
|
||||
|
||||
def get_by_username(self, db: Session, *, username: str) -> Optional[User]:
|
||||
return db.query(User).filter(User.username == username).first()
|
||||
|
||||
def create(self, db: Session, *, obj_in: UserCreate) -> User:
|
||||
db_obj = User(
|
||||
email=obj_in.email,
|
||||
username=obj_in.username,
|
||||
hashed_password=get_password_hash(obj_in.password),
|
||||
full_name=obj_in.full_name,
|
||||
is_active=obj_in.is_active,
|
||||
)
|
||||
db.add(db_obj)
|
||||
db.commit()
|
||||
db.refresh(db_obj)
|
||||
return db_obj
|
||||
|
||||
def update(
|
||||
self, db: Session, *, db_obj: User, obj_in: Union[UserUpdate, Dict[str, Any]]
|
||||
) -> User:
|
||||
if isinstance(obj_in, dict):
|
||||
update_data = obj_in
|
||||
else:
|
||||
update_data = obj_in.dict(exclude_unset=True)
|
||||
if update_data.get("password"):
|
||||
hashed_password = get_password_hash(update_data["password"])
|
||||
del update_data["password"]
|
||||
update_data["hashed_password"] = hashed_password
|
||||
return super().update(db, db_obj=db_obj, obj_in=update_data)
|
||||
|
||||
def authenticate(self, db: Session, *, email: str, password: str) -> Optional[User]:
|
||||
user = self.get_by_email(db, email=email)
|
||||
if not user:
|
||||
return None
|
||||
if not verify_password(password, user.hashed_password):
|
||||
return None
|
||||
return user
|
||||
|
||||
def is_active(self, user: User) -> bool:
|
||||
return user.is_active
|
||||
|
||||
def is_admin(self, user: User) -> bool:
|
||||
return user.is_admin
|
||||
|
||||
|
||||
user = CRUDUser(User)
|
0
app/db/__init__.py
Normal file
0
app/db/__init__.py
Normal file
21
app/db/session.py
Normal file
21
app/db/session.py
Normal file
@ -0,0 +1,21 @@
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.core.config import settings
|
||||
|
||||
engine = create_engine(
|
||||
settings.SQLALCHEMY_DATABASE_URL,
|
||||
connect_args={"check_same_thread": False}
|
||||
)
|
||||
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
# Dependency
|
||||
def get_db():
|
||||
db = SessionLocal()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
3
app/models/__init__.py
Normal file
3
app/models/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from app.models.user import User
|
||||
from app.models.product import Product, Category
|
||||
from app.models.order import Order, OrderItem, OrderStatus
|
43
app/models/order.py
Normal file
43
app/models/order.py
Normal file
@ -0,0 +1,43 @@
|
||||
from sqlalchemy import Column, Integer, String, Float, DateTime, ForeignKey, Enum
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
import enum
|
||||
|
||||
from app.db.session import Base
|
||||
|
||||
class OrderStatus(enum.Enum):
|
||||
PENDING = "pending"
|
||||
PROCESSING = "processing"
|
||||
SHIPPED = "shipped"
|
||||
DELIVERED = "delivered"
|
||||
CANCELLED = "cancelled"
|
||||
|
||||
|
||||
class Order(Base):
|
||||
__tablename__ = "orders"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
user_id = Column(Integer, ForeignKey("users.id"))
|
||||
status = Column(Enum(OrderStatus), default=OrderStatus.PENDING)
|
||||
total_amount = Column(Float, default=0.0)
|
||||
shipping_address = Column(String)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
user = relationship("User", backref="orders")
|
||||
items = relationship("OrderItem", back_populates="order", cascade="all, delete-orphan")
|
||||
|
||||
|
||||
class OrderItem(Base):
|
||||
__tablename__ = "order_items"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
order_id = Column(Integer, ForeignKey("orders.id"))
|
||||
product_id = Column(Integer, ForeignKey("products.id"))
|
||||
quantity = Column(Integer, default=1)
|
||||
price = Column(Float) # Price at the time of purchase
|
||||
|
||||
# Relationships
|
||||
order = relationship("Order", back_populates="items")
|
||||
product = relationship("Product", back_populates="order_items")
|
34
app/models/product.py
Normal file
34
app/models/product.py
Normal file
@ -0,0 +1,34 @@
|
||||
from sqlalchemy import Column, Integer, String, Float, Text, Boolean, DateTime, ForeignKey
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from app.db.session import Base
|
||||
|
||||
class Category(Base):
|
||||
__tablename__ = "categories"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, unique=True, index=True, nullable=False)
|
||||
description = Column(Text)
|
||||
|
||||
# Relationship
|
||||
products = relationship("Product", back_populates="category")
|
||||
|
||||
|
||||
class Product(Base):
|
||||
__tablename__ = "products"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
name = Column(String, index=True, nullable=False)
|
||||
description = Column(Text)
|
||||
price = Column(Float, nullable=False)
|
||||
stock = Column(Integer, default=0)
|
||||
is_active = Column(Boolean, default=True)
|
||||
category_id = Column(Integer, ForeignKey("categories.id"))
|
||||
image_url = Column(String)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
||||
|
||||
# Relationships
|
||||
category = relationship("Category", back_populates="products")
|
||||
order_items = relationship("OrderItem", back_populates="product")
|
17
app/models/user.py
Normal file
17
app/models/user.py
Normal file
@ -0,0 +1,17 @@
|
||||
from sqlalchemy import Boolean, Column, Integer, String, DateTime
|
||||
from sqlalchemy.sql import func
|
||||
|
||||
from app.db.session import Base
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id = Column(Integer, primary_key=True, index=True)
|
||||
email = Column(String, unique=True, index=True, nullable=False)
|
||||
username = Column(String, unique=True, index=True, nullable=False)
|
||||
hashed_password = Column(String, nullable=False)
|
||||
full_name = Column(String)
|
||||
is_active = Column(Boolean, default=True)
|
||||
is_admin = Column(Boolean, default=False)
|
||||
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
||||
updated_at = Column(DateTime(timezone=True), onupdate=func.now())
|
3
app/schemas/__init__.py
Normal file
3
app/schemas/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
from app.schemas.user import User, UserCreate, UserUpdate, UserInDB, Token, TokenPayload
|
||||
from app.schemas.product import Product, ProductCreate, ProductUpdate, ProductInDB, Category, CategoryCreate, CategoryUpdate
|
||||
from app.schemas.order import Order, OrderCreate, OrderUpdate, OrderInDB, OrderItem, OrderItemCreate, OrderItemUpdate
|
71
app/schemas/order.py
Normal file
71
app/schemas/order.py
Normal file
@ -0,0 +1,71 @@
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel, Field
|
||||
|
||||
from app.models.order import OrderStatus
|
||||
from app.schemas.product import Product
|
||||
|
||||
|
||||
class OrderItemBase(BaseModel):
|
||||
product_id: int
|
||||
quantity: int = Field(1, gt=0)
|
||||
price: Optional[float] = None # If not provided, will be taken from the product
|
||||
|
||||
|
||||
class OrderItemCreate(OrderItemBase):
|
||||
pass
|
||||
|
||||
|
||||
class OrderItemUpdate(BaseModel):
|
||||
quantity: Optional[int] = Field(None, gt=0)
|
||||
|
||||
|
||||
class OrderItemInDBBase(OrderItemBase):
|
||||
id: int
|
||||
order_id: int
|
||||
price: float # Price at time of purchase
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class OrderItem(OrderItemInDBBase):
|
||||
product: Optional[Product] = None
|
||||
|
||||
|
||||
class OrderItemInDB(OrderItemInDBBase):
|
||||
pass
|
||||
|
||||
|
||||
class OrderBase(BaseModel):
|
||||
user_id: int
|
||||
shipping_address: str
|
||||
total_amount: Optional[float] = None # Will be calculated from items
|
||||
|
||||
|
||||
class OrderCreate(OrderBase):
|
||||
items: List[OrderItemCreate]
|
||||
|
||||
|
||||
class OrderUpdate(BaseModel):
|
||||
status: Optional[OrderStatus] = None
|
||||
shipping_address: Optional[str] = None
|
||||
|
||||
|
||||
class OrderInDBBase(OrderBase):
|
||||
id: int
|
||||
status: OrderStatus
|
||||
created_at: datetime
|
||||
updated_at: Optional[datetime] = None
|
||||
total_amount: float
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class Order(OrderInDBBase):
|
||||
items: List[OrderItem] = []
|
||||
|
||||
|
||||
class OrderInDB(OrderInDBBase):
|
||||
pass
|
68
app/schemas/product.py
Normal file
68
app/schemas/product.py
Normal file
@ -0,0 +1,68 @@
|
||||
from typing import Optional, List
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel, Field, HttpUrl
|
||||
|
||||
|
||||
class CategoryBase(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
|
||||
|
||||
class CategoryCreate(CategoryBase):
|
||||
pass
|
||||
|
||||
|
||||
class CategoryUpdate(CategoryBase):
|
||||
name: Optional[str] = None
|
||||
|
||||
|
||||
class CategoryInDBBase(CategoryBase):
|
||||
id: int
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class Category(CategoryInDBBase):
|
||||
pass
|
||||
|
||||
|
||||
class ProductBase(BaseModel):
|
||||
name: str
|
||||
description: Optional[str] = None
|
||||
price: float = Field(..., gt=0)
|
||||
stock: int = Field(0, ge=0)
|
||||
is_active: bool = True
|
||||
category_id: Optional[int] = None
|
||||
image_url: Optional[str] = None
|
||||
|
||||
|
||||
class ProductCreate(ProductBase):
|
||||
pass
|
||||
|
||||
|
||||
class ProductUpdate(BaseModel):
|
||||
name: Optional[str] = None
|
||||
description: Optional[str] = None
|
||||
price: Optional[float] = Field(None, gt=0)
|
||||
stock: Optional[int] = Field(None, ge=0)
|
||||
is_active: Optional[bool] = None
|
||||
category_id: Optional[int] = None
|
||||
image_url: Optional[str] = None
|
||||
|
||||
|
||||
class ProductInDBBase(ProductBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: Optional[datetime] = None
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class Product(ProductInDBBase):
|
||||
category: Optional[Category] = None
|
||||
|
||||
|
||||
class ProductInDB(ProductInDBBase):
|
||||
pass
|
49
app/schemas/user.py
Normal file
49
app/schemas/user.py
Normal file
@ -0,0 +1,49 @@
|
||||
from typing import Optional
|
||||
from datetime import datetime
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
|
||||
|
||||
class UserBase(BaseModel):
|
||||
email: EmailStr
|
||||
username: str
|
||||
full_name: Optional[str] = None
|
||||
is_active: Optional[bool] = True
|
||||
|
||||
|
||||
class UserCreate(UserBase):
|
||||
password: str
|
||||
|
||||
|
||||
class UserUpdate(BaseModel):
|
||||
email: Optional[EmailStr] = None
|
||||
username: Optional[str] = None
|
||||
full_name: Optional[str] = None
|
||||
password: Optional[str] = None
|
||||
is_active: Optional[bool] = None
|
||||
|
||||
|
||||
class UserInDBBase(UserBase):
|
||||
id: int
|
||||
created_at: datetime
|
||||
updated_at: Optional[datetime] = None
|
||||
is_admin: bool = False
|
||||
|
||||
class Config:
|
||||
orm_mode = True
|
||||
|
||||
|
||||
class User(UserInDBBase):
|
||||
pass
|
||||
|
||||
|
||||
class UserInDB(UserInDBBase):
|
||||
hashed_password: str
|
||||
|
||||
|
||||
class Token(BaseModel):
|
||||
access_token: str
|
||||
token_type: str
|
||||
|
||||
|
||||
class TokenPayload(BaseModel):
|
||||
sub: Optional[int] = None
|
32
main.py
Normal file
32
main.py
Normal file
@ -0,0 +1,32 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
|
||||
from app.api.api import api_router
|
||||
from app.core.config import settings
|
||||
|
||||
app = FastAPI(
|
||||
title="E-commerce API",
|
||||
description="FastAPI E-commerce API with SQLite",
|
||||
version="0.1.0",
|
||||
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
||||
)
|
||||
|
||||
# Configure CORS
|
||||
app.add_middleware(
|
||||
CORSMiddleware,
|
||||
allow_origins=settings.BACKEND_CORS_ORIGINS,
|
||||
allow_credentials=True,
|
||||
allow_methods=["*"], # Allows all methods
|
||||
allow_headers=["*"], # Allows all headers
|
||||
)
|
||||
|
||||
@app.get("/")
|
||||
async def root():
|
||||
return {"message": "Welcome to the E-commerce API"}
|
||||
|
||||
# Include API router
|
||||
app.include_router(api_router, prefix=settings.API_V1_STR)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
9
requirements.txt
Normal file
9
requirements.txt
Normal file
@ -0,0 +1,9 @@
|
||||
fastapi==0.108.0
|
||||
uvicorn==0.25.0
|
||||
sqlalchemy==2.0.23
|
||||
alembic==1.13.0
|
||||
pydantic==2.5.2
|
||||
python-jose==3.3.0
|
||||
passlib==1.7.4
|
||||
python-multipart==0.0.6
|
||||
bcrypt==4.0.1
|
Loading…
x
Reference in New Issue
Block a user