File size: 889 Bytes
ba19a97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from fastapi import APIRouter, HTTPException
from app.schemas import UserSchema
from app.models import User
from sqlalchemy.orm import sessionmaker

router = APIRouter()

@router.post("/register")
async def register_user(username: str, password: str):
    user = User(username=username, password=password)
    session.add(user)
    session.commit()
    return {"message": "User created successfully"}

@router.get("/users/")
async def read_users():
    users = session.query(User).all()
    return [{"id": user.id, "username": user.username, "profile": user.profile} for user in users]

@router.get("/users/{user_id}")
async def read_user(user_id: int):
    user = session.query(User).filter(User.id == user_id).first()
    if user is None:
        raise HTTPException(status_code=404, detail="User not found")
    return {"id": user.id, "username": user.username, "profile": user.profile}