
- Set up project structure and dependencies - Create database models for users, posts, comments, and tags - Set up Alembic for database migrations - Implement user authentication (register, login) - Create CRUD endpoints for blog posts, comments, and tags - Add health check endpoint - Set up proper error handling - Update README with project details and setup instructions
51 lines
1.4 KiB
Python
51 lines
1.4 KiB
Python
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
from app.models.user import User as UserModel
|
|
from app.schemas.user import User, UserUpdate
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/me", response_model=User)
|
|
def get_current_user(
|
|
current_user: UserModel = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Get current user.
|
|
"""
|
|
return current_user
|
|
|
|
|
|
@router.put("/me", response_model=User)
|
|
def update_current_user(
|
|
*,
|
|
db: Session = Depends(deps.get_db),
|
|
user_in: UserUpdate,
|
|
current_user: UserModel = Depends(deps.get_current_active_user),
|
|
) -> Any:
|
|
"""
|
|
Update current user.
|
|
"""
|
|
if user_in.username and user_in.username != current_user.username:
|
|
user = crud.user.get_by_username(db, username=user_in.username)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Username already registered",
|
|
)
|
|
|
|
if user_in.email and user_in.email != current_user.email:
|
|
user = crud.user.get_by_email(db, email=user_in.email)
|
|
if user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_400_BAD_REQUEST,
|
|
detail="Email already registered",
|
|
)
|
|
|
|
user = crud.user.update(db, db_obj=current_user, obj_in=user_in)
|
|
return user |