
- Setup project structure and FastAPI application - Create SQLAlchemy models for users, products, carts, and orders - Implement Alembic migrations - Add CRUD operations and endpoints for all resources - Setup authentication with JWT - Add role-based access control - Update documentation
149 lines
3.8 KiB
Python
149 lines
3.8 KiB
Python
from typing import Any, List
|
|
|
|
from fastapi import APIRouter, Body, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.v1.deps import get_current_active_superuser, get_current_active_user
|
|
from app.core.deps import get_db
|
|
from app.crud.user import user as user_crud
|
|
from app.models.user import User
|
|
from app.schemas.user import User as UserSchema
|
|
from app.schemas.user import UserCreate, UserUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=List[UserSchema])
|
|
def read_users(
|
|
db: Session = Depends(get_db),
|
|
skip: int = 0,
|
|
limit: int = 100,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Retrieve users.
|
|
|
|
Only superusers can access this endpoint.
|
|
"""
|
|
users = user_crud.get_multi(db, skip=skip, limit=limit)
|
|
return users
|
|
|
|
|
|
@router.post("/", response_model=UserSchema)
|
|
def create_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_in: UserCreate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Create new user.
|
|
|
|
Only superusers can create other users.
|
|
"""
|
|
user = user_crud.get_by_email(db, email=user_in.email)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="A user with this email already exists in the system",
|
|
)
|
|
user = user_crud.create(db, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.get("/me", response_model=UserSchema)
|
|
def read_user_me(
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get current user.
|
|
"""
|
|
return current_user
|
|
|
|
|
|
@router.put("/me", response_model=UserSchema)
|
|
def update_user_me(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
full_name: str = Body(None),
|
|
password: str = Body(None),
|
|
current_user: User = Depends(get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update own user.
|
|
"""
|
|
user_in = UserUpdate()
|
|
if password is not None:
|
|
user_in.password = password
|
|
if full_name is not None:
|
|
user_in.full_name = full_name
|
|
user = user_crud.update(db, db_obj=current_user, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.get("/{user_id}", response_model=UserSchema)
|
|
def read_user_by_id(
|
|
user_id: int,
|
|
current_user: User = Depends(get_current_active_user),
|
|
db: Session = Depends(get_db),
|
|
) -> Any:
|
|
"""
|
|
Get a specific user by id.
|
|
"""
|
|
user = user_crud.get(db, id=user_id)
|
|
if user == current_user:
|
|
return user
|
|
if not user_crud.is_superuser(current_user):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_403_FORBIDDEN,
|
|
detail="The user doesn't have enough privileges",
|
|
)
|
|
return user
|
|
|
|
|
|
@router.put("/{user_id}", response_model=UserSchema)
|
|
def update_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_id: int,
|
|
user_in: UserUpdate,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Update a user.
|
|
|
|
Only superusers can update other users.
|
|
"""
|
|
user = user_crud.get(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="The user with this ID does not exist in the system",
|
|
)
|
|
user = user_crud.update(db, db_obj=user, obj_in=user_in)
|
|
return user
|
|
|
|
|
|
@router.delete(
|
|
"/{user_id}", status_code=status.HTTP_204_NO_CONTENT, response_model=None
|
|
)
|
|
def delete_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_id: int,
|
|
current_user: User = Depends(get_current_active_superuser),
|
|
) -> Any:
|
|
"""
|
|
Delete a user.
|
|
|
|
Only superusers can delete users.
|
|
"""
|
|
user = user_crud.get(db, id=user_id)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="The user with this ID does not exist in the system",
|
|
)
|
|
user_crud.remove(db, id=user_id)
|
|
return None
|