99 lines
2.9 KiB
Python
99 lines
2.9 KiB
Python
from fastapi import APIRouter, HTTPException
|
|
from pydantic import BaseModel
|
|
from typing import Optional
|
|
import uuid
|
|
|
|
from bd import Settings
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
|
|
from bd.tables.users import User
|
|
|
|
router = APIRouter(tags=["users"], prefix="/users")
|
|
|
|
|
|
class UserCreate(BaseModel):
|
|
first_name: str
|
|
last_name: str
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
first_name: Optional[str]
|
|
last_name: Optional[str]
|
|
|
|
|
|
def get_session():
|
|
settings = Settings()
|
|
engine = create_engine(settings.DATABASE_URL_syncpg, future=True)
|
|
return sessionmaker(bind=engine, future=True)
|
|
|
|
|
|
@router.post("/", status_code=201)
|
|
def create_user(payload: UserCreate):
|
|
Session = get_session()
|
|
with Session() as session:
|
|
user = User(first_name=payload.first_name, last_name=payload.last_name)
|
|
session.add(user)
|
|
session.commit()
|
|
session.refresh(user)
|
|
return {"id": str(user.id), "first_name": user.first_name, "last_name": user.last_name}
|
|
|
|
|
|
@router.put("/{user_id}")
|
|
def update_user(user_id: str, payload: UserUpdate):
|
|
Session = get_session()
|
|
try:
|
|
uid = uuid.UUID(user_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
user = session.get(User, uid)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
if payload.first_name is not None:
|
|
user.first_name = payload.first_name
|
|
if payload.last_name is not None:
|
|
user.last_name = payload.last_name
|
|
session.add(user)
|
|
session.commit()
|
|
session.refresh(user)
|
|
return {"id": str(user.id), "first_name": user.first_name, "last_name": user.last_name}
|
|
|
|
|
|
@router.delete("/{user_id}", status_code=204)
|
|
def delete_user(user_id: str):
|
|
Session = get_session()
|
|
try:
|
|
uid = uuid.UUID(user_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
user = session.get(User, uid)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
session.delete(user)
|
|
session.commit()
|
|
return {}
|
|
|
|
|
|
@router.get("/")
|
|
def list_users():
|
|
Session = get_session()
|
|
with Session() as session:
|
|
rows = session.query(User).all()
|
|
return [{"id": str(u.id), "first_name": u.first_name, "last_name": u.last_name} for u in rows]
|
|
|
|
|
|
@router.get("/{user_id}")
|
|
def get_user(user_id: str):
|
|
Session = get_session()
|
|
try:
|
|
uid = uuid.UUID(user_id)
|
|
except Exception:
|
|
raise HTTPException(status_code=400, detail="Invalid UUID")
|
|
with Session() as session:
|
|
user = session.get(User, uid)
|
|
if not user:
|
|
raise HTTPException(status_code=404, detail="User not found")
|
|
return {"id": str(user.id), "first_name": user.first_name, "last_name": user.last_name}
|