File size: 1,992 Bytes
b94326e 014870a b94326e 014870a 1182d2f 014870a 8f08b42 b94326e 014870a 1cf701d b94326e 014870a 641b706 b94326e 641b706 b94326e 641b706 b94326e c7205fe b94326e |
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 |
from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy.orm import Session
import crud
import models
import schemas
from database import SessionLocal, engine
from app import llm_router
from fastapi import APIRouter, FastAPI
models.Base.metadata.create_all(bind=engine)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
user_router = APIRouter(prefix="/user")
@user_router.post("/users/", response_model=schemas.User, tags=["users"])
def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)):
db_user = crud.get_user_by_email(db, email=user.email)
if db_user:
raise HTTPException(status_code=400, detail="Email already registered")
return crud.create_user(db=db, user=user)
@user_router.get("/users/", response_model=list[schemas.User], tags=["users"])
def read_users(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
users = crud.get_users(db, skip=skip, limit=limit)
return users
@user_router.get("/users/{user_id}", response_model=schemas.User, tags=["users"] )
def read_user(user_id: int, db: Session = Depends(get_db)):
db_user = crud.get_user(db, user_id=user_id)
if db_user is None:
raise HTTPException(status_code=404, detail="User not found")
return db_user
@user_router.post("/users/{user_id}/items/", response_model=schemas.Item, tags=["users"] )
def create_item_for_user(
user_id: int, item: schemas.ItemCreate, db: Session = Depends(get_db)
):
return crud.create_user_item(db=db, item=item, user_id=user_id)
@user_router.get("/items/", response_model=list[schemas.Item], tags=["users"] )
def read_items(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
items = crud.get_items(db, skip=skip, limit=limit)
return items
app = FastAPI(
docs_url="/",
title="OpenGenAI",
description="Your Excellect AI Physician")
app.include_router(user_router)
app.include_router(llm_router) |