OpenGenAI / main.py
moriire's picture
latest
c5d420d
raw
history blame
1.99 kB
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)