fers web
This commit is contained in:
@@ -13,8 +13,9 @@ router = APIRouter(tags=["users"], prefix="/users")
|
||||
|
||||
|
||||
class UserCreate(BaseModel):
|
||||
first_name: str
|
||||
last_name: str
|
||||
first_name: Optional[str] = None
|
||||
last_name: Optional[str] = None
|
||||
name: Optional[str] = None
|
||||
|
||||
|
||||
class UserUpdate(BaseModel):
|
||||
@@ -28,11 +29,21 @@ def get_session():
|
||||
return sessionmaker(bind=engine, future=True)
|
||||
|
||||
|
||||
@router.post("/", status_code=201)
|
||||
@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)
|
||||
# Support payload with 'name' (single field) or first_name+last_name
|
||||
first = payload.first_name
|
||||
last = payload.last_name
|
||||
if not (first or last) and payload.name:
|
||||
parts = payload.name.strip().split()
|
||||
if parts:
|
||||
first = parts[0]
|
||||
last = " ".join(parts[1:]) if len(parts) > 1 else ""
|
||||
if not first and not last:
|
||||
raise HTTPException(status_code=422, detail="Missing name or first_name/last_name")
|
||||
user = User(first_name=first or "", last_name=last or "")
|
||||
session.add(user)
|
||||
session.commit()
|
||||
session.refresh(user)
|
||||
@@ -76,7 +87,7 @@ def delete_user(user_id: str):
|
||||
return {}
|
||||
|
||||
|
||||
@router.get("/")
|
||||
@router.get("")
|
||||
def list_users():
|
||||
Session = get_session()
|
||||
with Session() as session:
|
||||
|
||||
Reference in New Issue
Block a user