Spaces:
Running
on
Zero
Running
on
Zero
File size: 2,210 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
from fastapi import FastAPI, Depends
from sqlalchemy.orm import Session
from models import User, Team
from database import engine, SessionLocal
app = FastAPI()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/register")
async def register_user(username: str, password: str, db: Session = Depends(get_db)):
if User.exists(username, db):
return {"error": "Username already exists"}
user = User(username, password)
db.add(user)
db.commit()
return {"message": "User created successfully"}
@app.post("/login")
async def login_user(username: str, password: str, db: Session = Depends(get_db)):
user = User.authenticate(username, password, db)
if user:
return {"message": "Login successful"}
return {"error": "Invalid username or password"}
@app.get("/teams")
async def get_teams(db: Session = Depends(get_db)):
teams = db.query(Team).order_by(Team.created_at.desc()).all()
return [{"id": team.id, "name": team.name} for team in teams]
@app.post("/teams")
async def create_team(name: str, db: Session = Depends(get_db)):
team = Team(name)
db.add(team)
db.commit()
return {"message": "Team created successfully"}
@app.get("/users")
async def get_users(db: Session = Depends(get_db)):
users = db.query(User).order_by(User.created_at.desc()).all()
return [{"id": user.id, "username": user.username, "profile": user.profile} for user in users]
@app.get("/users/{user_id}")
async def get_user(user_id: int, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if user:
return {"username": user.username, "team": user.team.name, "profile": user.profile, "tags": user.tags}
return {"error": "User not found"}
@app.put("/users/{user_id}")
async def update_user(user_id: int, team_id: int, profile: str, tags: str, db: Session = Depends(get_db)):
user = db.query(User).filter(User.id == user_id).first()
if user:
user.team_id = team_id
user.profile = profile
user.tags = tags
db.commit()
return {"message": "User updated successfully"}
return {"error": "User not found"} |