| 1 |
from fastapi import FastAPI |
| 2 |
from pydantic import BaseModel, EmailStr |
| 3 |
|
| 4 |
app = FastAPI() |
| 5 |
|
| 6 |
|
| 7 |
class UserBase(BaseModel): |
| 8 |
username: str |
| 9 |
email: EmailStr |
| 10 |
full_name: str | None = None |
| 11 |
|
| 12 |
|
| 13 |
class UserIn(UserBase): |
| 14 |
password: str |
| 15 |
|
| 16 |
|
| 17 |
class UserOut(UserBase): |
| 18 |
pass |
| 19 |
|
| 20 |
|
| 21 |
class UserInDB(UserBase): |
| 22 |
hashed_password: str |
| 23 |
|
| 24 |
|
| 25 |
def fake_password_hasher(raw_password: str): |
| 26 |
return "supersecret" + raw_password |
| 27 |
|
| 28 |
|
| 29 |
def fake_save_user(user_in: UserIn): |
| 30 |
hashed_password = fake_password_hasher(user_in.password) |
| 31 |
user_in_db = UserInDB(**user_in.model_dump(), hashed_password=hashed_password) |
| 32 |
print("User saved! ..not really") |
| 33 |
return user_in_db |
| 34 |
|
| 35 |
|
| 36 |
@app.post("/user/", response_model=UserOut) |
| 37 |
async def create_user(user_in: UserIn): |
| 38 |
user_saved = fake_save_user(user_in) |
| 39 |
return user_saved |