init
This commit is contained in:
13
app/api/__init__.py
Normal file
13
app/api/__init__.py
Normal file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
from fastapi import APIRouter
|
||||
|
||||
import app.api.form
|
||||
import app.api.result
|
||||
import app.api.bench
|
||||
|
||||
api_router = APIRouter()
|
||||
|
||||
api_router.include_router(bench.router, prefix='/benches', tags=['benches'])
|
||||
api_router.include_router(form.router, prefix='/forms', tags=['forms'])
|
||||
api_router.include_router(result.router, prefix='/results', tags=['results'])
|
||||
44
app/api/bench.py
Normal file
44
app/api/bench.py
Normal file
@@ -0,0 +1,44 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
from fastapi import APIRouter, Body, Depends
|
||||
from loguru import logger
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import get_db
|
||||
from app.crud import form_crud, result_crud
|
||||
from app.schemas import Bench
|
||||
from app.schemas.form import Form
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get("/forms", response_model=list[Form])
|
||||
def read_bench_forms(db: Session = Depends(get_db), limit: int = 10):
|
||||
return form_crud.form.get_last_bench(db, num=limit)
|
||||
|
||||
|
||||
@router.post("/result")
|
||||
def read_bench_forms(uuid: str = Body(), db: Session = Depends(get_db), ):
|
||||
logger.debug(f"{uuid=}")
|
||||
result_dbs = result_crud.result.get_by_uuid(db, uuid=uuid)
|
||||
res = {
|
||||
"百川": {"value": ""},
|
||||
"ChatGPT": {"value": ""},
|
||||
"MyTwins": {"value": ""},
|
||||
}
|
||||
for result in result_dbs:
|
||||
res[result.name] = result
|
||||
return res
|
||||
|
||||
|
||||
@router.get("/", response_model=list[Bench])
|
||||
def read_forms(db: Session = Depends(get_db), limit: int = 10):
|
||||
res = []
|
||||
form_dbs = form_crud.form.get_last_bench(db, num=limit)
|
||||
for form_db in form_dbs:
|
||||
res.append(
|
||||
Bench(
|
||||
form=form_db,
|
||||
results=result_crud.result.get_by_uuid(db, uuid=form_db.uuid)
|
||||
))
|
||||
return res
|
||||
43
app/api/form.py
Normal file
43
app/api/form.py
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import get_db
|
||||
from app.crud import form_crud
|
||||
from app.schemas import Form, FormCreate, FormUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.put("/{item_id}", response_model=Form)
|
||||
def update_form(item_id: int, form_in: FormUpdate, db: Session = Depends(get_db)):
|
||||
form_db = form_crud.form.get(db, item_id)
|
||||
if not form_db:
|
||||
raise HTTPException(status_code=404, detail="Form not found")
|
||||
return form_crud.form.update(db=db, db_obj=form_db, obj_in=form_in)
|
||||
|
||||
|
||||
@router.delete("/{item_id}")
|
||||
def delete_form(item_id: int, db: Session = Depends(get_db)):
|
||||
form_db = form_crud.form.get(db, item_id)
|
||||
if not form_db:
|
||||
raise HTTPException(status_code=404, detail="Form not found")
|
||||
return form_crud.form.remove(db=db, id=item_id)
|
||||
|
||||
|
||||
@router.get("/{item_id}", response_model=Form)
|
||||
def read_form(item_id: int, db: Session = Depends(get_db)):
|
||||
return form_crud.form.get(db, item_id)
|
||||
|
||||
|
||||
@router.post("/", response_model=Form)
|
||||
def create_form(form_in: FormCreate, db: Session = Depends(get_db)):
|
||||
form_db = form_crud.form.create(db=db, obj_in=form_in)
|
||||
return form_db
|
||||
|
||||
|
||||
@router.get("/", response_model=list[Form])
|
||||
def read_forms(db: Session = Depends(get_db), skip: int = 0, limit: int = 100):
|
||||
return form_crud.form.get_multi(db, skip=skip, limit=limit)
|
||||
43
app/api/result.py
Normal file
43
app/api/result.py
Normal file
@@ -0,0 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding:utf-8 -*-
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.db.base import get_db
|
||||
from app.crud import result_crud
|
||||
from app.schemas import Result, ResultCreate, ResultUpdate
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.put("/{item_id}", response_model=Result)
|
||||
def update_result(item_id: int, result_in: ResultUpdate, db: Session = Depends(get_db)):
|
||||
result_db = result_crud.result.get(db, item_id)
|
||||
if not result_db:
|
||||
raise HTTPException(status_code=404, detail="Result not found")
|
||||
return result_crud.result.update(db=db, db_obj=result_db, obj_in=result_in)
|
||||
|
||||
|
||||
@router.delete("/{item_id}")
|
||||
def delete_result(item_id: int, db: Session = Depends(get_db)):
|
||||
result_db = result_crud.result.get(db, item_id)
|
||||
if not result_db:
|
||||
raise HTTPException(status_code=404, detail="Result not found")
|
||||
return result_crud.result.remove(db=db, id=item_id)
|
||||
|
||||
|
||||
@router.get("/{item_id}", response_model=Result)
|
||||
def read_result(item_id: int, db: Session = Depends(get_db)):
|
||||
return result_crud.result.get(db, item_id)
|
||||
|
||||
|
||||
@router.post("/", response_model=Result)
|
||||
def create_result(result_in: ResultCreate, db: Session = Depends(get_db)):
|
||||
result_db = result_crud.result.create(db=db, obj_in=result_in)
|
||||
return result_db
|
||||
|
||||
|
||||
@router.get("/", response_model=list[Result])
|
||||
def read_results(db: Session = Depends(get_db), skip: int = 0, limit: int = 100):
|
||||
return result_crud.result.get_multi(db, skip=skip, limit=limit)
|
||||
Reference in New Issue
Block a user