From 957e826e0f1187795265b54d57cc03a194563f11 Mon Sep 17 00:00:00 2001 From: 00MSJ1122 <1253313142@qq.com> Date: Tue, 22 Sep 2026 14:58:51 +0800 Subject: [PATCH] 1 --- PythonProject/api/student_api.py | 7 ++++++- PythonProject/dao/student_dao.py | 29 +++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py index 3b1bb2b..2cf2f1d 100644 --- a/PythonProject/api/student_api.py +++ b/PythonProject/api/student_api.py @@ -32,11 +32,16 @@ def update_students(s:StudentRequest ,stu_id:int ,db=Depends(get_db)): d = s.model_dump(exclude_unset=True) + d.pop('stu_id', None) if not d: raise HTTPException(status_code=400, detail='更新内容不能为空') r = update_student_dao( stu_id=stu_id,update_data=d,db=db ) + if r == 'conflict': + raise HTTPException(status_code=409, detail = '身份证号已被其他学生占用') + if r == 'error': + raise HTTPException(status_code=500, detail='更新失败,请稍后重试') if not r: - raise HTTPException(status_code=500, detail='没有更新') + raise HTTPException(status_code=404, detail='没有更新') return {'code':200,'totals':r,'detail':'更新成功'} @StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息') diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index 0822d9d..d947d0d 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -36,16 +36,29 @@ def delete_student_dao(stu_id,db): return rows def update_student_dao(stu_id,update_data,db): + if update_data.get('id_card'): + conflict = (db.query(Student_Model) + .filter(Student_Model.id_card == update_data['id_card'], + Student_Model.stu_id != stu_id, + Student_Model.delete_status == 0) + .first()) + if conflict: + return 'conflict' + try: - rows=(db.query(Student_Model) - .fiter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0) - .update(update_data)) - except: - db.rollback() - return False - else: + rows = (db.query(Student_Model) + .filter(Student_Model.stu_id == stu_id, + Student_Model.delete_status == 0) + .update(update_data)) db.commit() - return rows + except IntegrityError: + db.rollback() + return 'conflict' + except Exception: + db.rollback() + return 'error' + + return rows def get_student_dao(stu_id:Optional[int] ,stu_name:Optional[str]