from fastapi import FastAPI, Depends from fastapi.responses import JSONResponse from fastapi.requests import Request from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse from fastapi.encoders import jsonable_encoder from pydantic import BaseModel from sqlalchemy.orm import Session from . import models, schema, crud from .database import SessionLocal, engine models.Base.metadata.create_all(bind=engine) app = FastAPI() def get_db(): db = SessionLocal() try: yield db finally: db.close() @app.post("/users/") def create_user(user: schema.UserCreate, db: Session = Depends(get_db)): return crud.create_user(db=db, user=user) @app.get("/users/") def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return crud.get_users(db, skip=skip, limit=limit) @app.get("/users/{user_id}") def read_user(user_id: int, db: Session = Depends(get_db)): return crud.get_user(db, user_id=user_id) @app.post("/teams/") def create_team(team: schema.TeamCreate, db: Session = Depends(get_db)): return crud.create_team(db=db, team=team) @app.get("/teams/") def read_teams(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)): return crud.get_teams(db, skip=skip, limit=limit) @app.get("/teams/{team_id}") def read_team(team_id: int, db: Session = Depends(get_db)): return crud.get_team(db, team_id=team_id)