Spaces:
Build error
Build error
File size: 1,668 Bytes
cce8120 | 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 | from fastapi import FastAPI, Depends, HTTPException, status
from sqlalchemy.orm import Session
from . import models, schemas, crud, database
models.Base.metadata.create_all(bind=database.engine)
app = FastAPI(title="Test API")
def get_db():
db = database.SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/tests/", response_model=schemas.Test, status_code=status.HTTP_201_CREATED)
def create_test_endpoint(test: schemas.TestCreate, db: Session = Depends(get_db)):
return crud.create_test(db, test)
@app.get("/tests/{test_id}", response_model=schemas.Test)
def read_test(test_id: int, db: Session = Depends(get_db)):
db_test = crud.get_test(db, test_id)
if not db_test:
raise HTTPException(status_code=404, detail="Test not found")
return db_test
@app.get("/tests/", response_model=list[schemas.Test])
def list_tests(skip: int = 0, limit: int = 100, db: Session = Depends(get_db)):
return crud.get_tests(db, skip=skip, limit=limit)
@app.put("/tests/{test_id}", response_model=schemas.Test)
def update_test_endpoint(test_id: int, updates: schemas.TestUpdate, db: Session = Depends(get_db)):
db_test = crud.get_test(db, test_id)
if not db_test:
raise HTTPException(status_code=404, detail="Test not found")
return crud.update_test(db, db_test, updates)
@app.delete("/tests/{test_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_test_endpoint(test_id: int, db: Session = Depends(get_db)):
db_test = crud.get_test(db, test_id)
if not db_test:
raise HTTPException(status_code=404, detail="Test not found")
crud.delete_test(db, db_test)
return None
|