1
This commit is contained in:
@@ -32,11 +32,16 @@ def update_students(s:StudentRequest
|
|||||||
,stu_id:int
|
,stu_id:int
|
||||||
,db=Depends(get_db)):
|
,db=Depends(get_db)):
|
||||||
d = s.model_dump(exclude_unset=True)
|
d = s.model_dump(exclude_unset=True)
|
||||||
|
d.pop('stu_id', None)
|
||||||
if not d:
|
if not d:
|
||||||
raise HTTPException(status_code=400, detail='更新内容不能为空')
|
raise HTTPException(status_code=400, detail='更新内容不能为空')
|
||||||
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
|
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:
|
if not r:
|
||||||
raise HTTPException(status_code=500, detail='没有更新')
|
raise HTTPException(status_code=404, detail='没有更新')
|
||||||
return {'code':200,'totals':r,'detail':'更新成功'}
|
return {'code':200,'totals':r,'detail':'更新成功'}
|
||||||
|
|
||||||
@StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息')
|
@StudentAPI.delete('/{stu_id}',summary='学生信息删除接口',description='删除学生信息')
|
||||||
|
|||||||
@@ -36,15 +36,28 @@ def delete_student_dao(stu_id,db):
|
|||||||
return rows
|
return rows
|
||||||
|
|
||||||
def update_student_dao(stu_id,update_data,db):
|
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:
|
try:
|
||||||
rows = (db.query(Student_Model)
|
rows = (db.query(Student_Model)
|
||||||
.fiter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
|
.filter(Student_Model.stu_id == stu_id,
|
||||||
|
Student_Model.delete_status == 0)
|
||||||
.update(update_data))
|
.update(update_data))
|
||||||
except:
|
|
||||||
db.rollback()
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
db.commit()
|
db.commit()
|
||||||
|
except IntegrityError:
|
||||||
|
db.rollback()
|
||||||
|
return 'conflict'
|
||||||
|
except Exception:
|
||||||
|
db.rollback()
|
||||||
|
return 'error'
|
||||||
|
|
||||||
return rows
|
return rows
|
||||||
|
|
||||||
def get_student_dao(stu_id:Optional[int]
|
def get_student_dao(stu_id:Optional[int]
|
||||||
|
|||||||
Reference in New Issue
Block a user