76 lines
1.5 KiB
Python
76 lines
1.5 KiB
Python
# api/score.py
|
|||
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||
|
|
from sqlalchemy.orm import Session
|
||
|
|
from dao import score_dao
|
||
|
|
from database import get_db
|
||
|
|
|
||
|
|
# 创建路由器
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# 不及格学生表
|
||
|
|
|
||
|
|
@router.get("/warning/list")
|
||
|
|
async def warning_list(db: Session = Depends(get_db)):
|
||
|
|
return score_dao.get_warning(db)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# 查某学生所有成绩
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/{stu_id}")
|
||
|
|
def get_scores(stu_id: int, db: Session = Depends(get_db)):
|
||
|
|
return score_dao.get_score(db, stu_id)
|
||
|
|
|
||
|
|
|
||
|
|
# 增
|
||
|
|
|
||
|
|
@router.post("/")
|
||
|
|
def add_score(
|
||
|
|
student_id: int = Query(...),
|
||
|
|
exam_seq: int = Query(...),
|
||
|
|
score: float = Query(...),
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
):
|
||
|
|
obj, err = score_dao.add_score(db, student_id, exam_seq, score)
|
||
|
|
if err:
|
||
|
|
raise HTTPException(status_code=400, detail=err)
|
||
|
|
return obj
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
# 改
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/update")
|
||
|
|
def update_score(
|
||
|
|
student_id: int = Query(...),
|
||
|
|
exam_seq: int = Query(...),
|
||
|
|
score: float = Query(...),
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
):
|
||
|
|
obj, err = score_dao.update_score(db, student_id, exam_seq, score)
|
||
|
|
if err:
|
||
|
|
raise HTTPException(status_code=404, detail=err)
|
||
|
|
return obj
|
||
|
|
|
||
|
|
|
||
|
|
# 逻辑删除
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/delete")
|
||
|
|
def delete_score(
|
||
|
|
student_id: int = Query(...),
|
||
|
|
exam_seq: int = Query(...),
|
||
|
|
db: Session = Depends(get_db),
|
||
|
|
):
|
||
|
|
obj, err = score_dao.delete_score(db, student_id, exam_seq)
|
||
|
|
if err:
|
||
|
|
raise HTTPException(status_code=404, detail=err)
|
||
|
|
return {"message": "已删除,数据仍保留在后台"}
|
||
|
|
|
||
|
|
|