nb
This commit is contained in:
@@ -14,17 +14,27 @@ def get_grade(stu_id: int,db=Depends(get_db)):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise HTTPException(status_code=500,detail=f'数据库查询异常:{str(e)}')
|
raise HTTPException(status_code=500,detail=f'数据库查询异常:{str(e)}')
|
||||||
|
|
||||||
@gradeAPI.post('/{stu_id}',response_model=GradeResponse,summary='添加学生成绩')
|
@gradeAPI.post('/grade',response_model=GradeResponse,summary='添加学生成绩')
|
||||||
def post_grade(o:GradeRequest=Form(),db=Depends(get_db)):
|
def post_grade(o:GradeRequest=Form(),db=Depends(get_db)):
|
||||||
|
|
||||||
exist_obj = get_grade_dao(db,o.stu_id)
|
|
||||||
if exist_obj:
|
|
||||||
raise HTTPException(status_code=409, detail="该学生此考核序次成绩已存在!")
|
|
||||||
|
|
||||||
d=o.model_dump()
|
d=o.model_dump()
|
||||||
r=add_grade_dao(g=d,db=db)
|
r=add_grade_dao(g=d,db=db)
|
||||||
if not r:
|
if not r:
|
||||||
raise HTTPException(status_code=400, detail='学生不存在')
|
raise HTTPException(status_code=400, detail='学生不存在')
|
||||||
return o
|
return o
|
||||||
|
|
||||||
|
@gradeAPI.put('/{score_id}',summary='成绩修改')
|
||||||
|
def put_grade(score_id: int,o:GradeUpdate,db=Depends(get_db)):
|
||||||
|
d = o.model_update(exclude_unset=True)
|
||||||
|
r = update_grade_dao(score_id=score_id
|
||||||
|
,update_data=d
|
||||||
|
,db=db)
|
||||||
|
if not r:
|
||||||
|
raise HTTPException(status_code=500,detail='没有更新')
|
||||||
|
return {'code':200,'totals':r,'detail':'更新成功'}
|
||||||
|
|
||||||
|
@gradeAPI.delete('/{score_id}',summary='删除成绩')
|
||||||
|
def delete_grade(score_id:int,db=Depends(get_db)):
|
||||||
|
r = delete_grade_dao(stu_id=score_id,db=db)
|
||||||
|
if not r:
|
||||||
|
raise HTTPException(status_code=404,detail='删除失败')
|
||||||
|
return {'code':200,'totals':r,'detail':'删除成功'}
|
||||||
@@ -21,4 +21,30 @@ def add_grade_dao(g,db):
|
|||||||
return o1
|
return o1
|
||||||
except Exception:
|
except Exception:
|
||||||
db.rollback()
|
db.rollback()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
def update_grade_dao(score_id:int, update_data, db):
|
||||||
|
try:
|
||||||
|
rows = db.query(WlScore).filter(
|
||||||
|
WlScore.stu_id == score_id,
|
||||||
|
WlScore.delete_status == 0
|
||||||
|
).update(update_data)
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return rows
|
||||||
|
|
||||||
|
def delete_grade_dao(stu_id:int,db):
|
||||||
|
try:
|
||||||
|
rows = db.query(WlScore).filter(
|
||||||
|
WlScore.stu_id == stu_id,
|
||||||
|
WlScore.delete_status == 0
|
||||||
|
).update({'delete_status':1})
|
||||||
|
except:
|
||||||
|
db.rollback()
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
db.commit()
|
||||||
|
return rows
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ class GradeRequest(BaseModel):
|
|||||||
exam_order:int = Field(description='考核序次')
|
exam_order:int = Field(description='考核序次')
|
||||||
score:float = Field(description='成绩')
|
score:float = Field(description='成绩')
|
||||||
|
|
||||||
|
class GradeUpdate(BaseModel):#修改请求体
|
||||||
|
exam_order: int = Field(description='考核序次')
|
||||||
|
score: float = Field(description='成绩')
|
||||||
|
|
||||||
class GradeResponse(BaseModel):
|
class GradeResponse(BaseModel):
|
||||||
code: int = 200
|
code: int = 200
|
||||||
detail: str = 'OK'
|
detail: str = 'OK'
|
||||||
|
|||||||
Reference in New Issue
Block a user