Spaces:
Running
on
Zero
Running
on
Zero
File size: 1,110 Bytes
df50319 |
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 26 27 28 |
from fastapi import APIRouter, HTTPException
from app.schemas import UserSchema
from app.models import User
router = APIRouter()
@router.post("/register")
async def register_user(user: UserSchema):
# Check if username already exists
existing_user = User.query.filter_by(username=user.username).first()
if existing_user:
raise HTTPException(status_code=400, detail="Username already exists")
new_user = User(username=user.username, password=user.password, profile=user.profile, team_id=user.team_id, tags=user.tags)
db.session.add(new_user)
db.session.commit()
return {"message": "User created successfully"}
@router.get("/users")
async def get_users():
users = User.query.all()
return [{"id": user.id, "username": user.username, "profile": user.profile} for user in users]
@router.get("/users/{user_id}")
async def get_user(user_id: int):
user = User.query.get(user_id)
if user is None:
raise HTTPException(status_code=404, detail="User not found")
return {"id": user.id, "username": user.username, "profile": user.profile, "tags": user.tags} |