diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py index b71aafc..db947bf 100644 --- a/PythonProject/api/student_api.py +++ b/PythonProject/api/student_api.py @@ -1,11 +1,44 @@ -from fastapi import APIRouter -from dao.student_dao import select_age +from fastapi import APIRouter,Depends,HTTPException from schema.student_schema import StudentResponse +from dao.student_dao import delete_student_dao,add_student_dao,update_student_dao,get_student_dao,select_age +from schema.student_schema import StudentRequest,StudentResponse +from model.student_model import Student_Model +from util.database import get_db -studentapi = APIRouter() -@studentapi.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息') +StudentAPI = APIRouter() + +@StudentAPI.get("/age",response_model=StudentResponse,tags=['统计分析模块'],description='根据年龄查询学生信息') def get_students(min_age:int|None=None,max_age:int|None=None): l = select_age(min_age,max_age) return [i for i in l] +@StudentAPI.get('',description='查询学生信息') +def get_students(stu_id:int|None=None + ,stu_name:str|None=None + ,class_id:int|None=None + ,db=Depends(get_db) + ,page:int=1 + ,page_size:int=10): + r=get_student_dao(stu_id,stu_name,class_id,db,page,page_size) + if r: + return r + raise HTTPException(status_code=404,detail='学生不存在') + +@StudentAPI.put('/{stu_id}') +def update_orders(s:StudentRequest,stu_id:int,db=Depends(get_db)): + d = s.model_dump(exclude_unset=True) + r = update_student_dao( stu_id=stu_id,update_data=d,db=db ) + if not r: + raise HTTPException(status_code=500, detail='没有更新!') + return {'code':200,'totals':r,'detail':'更新成功'} + +@StudentAPI.delete('/{stu_id}') +def del_student(stu_id:int,db=Depends(get_db)): + rows=delete_student_dao( stu_id=stu_id,db=db ) + if not rows: + raise HTTPException(status_code=500,detail='没有删除') + return {'code':200,'totals':rows,'detail':'删除成功'} + + + diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index 55d7dd5..9806925 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -9,29 +9,44 @@ def select_age(min_age,max_age,session=Depends(get_db)): raise HTTPException(status_code=400, detail="请至少传入一个参数") session = session.query(Student_Model) if min_age is not None: - session = session.filter(Student_Model.age >= min_age) + session = session.filter(Student_Model.age >= min_age,Student_Model.delete_status == 0) if max_age is not None: - session = session.filter(Student_Model.age <= max_age) + session = session.filter(Student_Model.age <= max_age,Student_Model.delete_status == 0) return session.all() except HTTPException: raise except Exception: raise HTTPException(status_code=500,detail="查询异常") -def delete_student_dao(stu_id,db): +def add_student_dao(o,db=Depends(get_db)): try: - rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id).delete() + r = db.query(Student_Model).filter(Student_Model.stu_id == o['stu_id'],Student_Model.delete_status == 0).all() + if not r: + o1 = Student_Model( **o) + db.add(o1) + else: + raise ValueError + except: + db.rollback() + return False + else: + db.commit() + return True + +def delete_student_dao(stu_id,db=Depends(get_db)): + try: + rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0).delete() + Student_Model.delete_status = 1 + db.commit() except: db.rollback() rows = 0 - else: - db.commit() finally: return rows -def update_student_dao(stu_id,update_data,db): +def update_student_dao(stu_id,update_data,db=Depends(get_db)): try: - rows = db.query( Student_Model ).filter( Student_Model.stu_id == stu_id ).update( update_data ) + rows = db.query( Student_Model ).filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0).update( update_data ) except: db.rollback() return False @@ -42,17 +57,17 @@ def update_student_dao(stu_id,update_data,db): def get_student_dao(stu_id:Optional[int] ,stu_name:Optional[str] ,class_id:Optional[int] - ,db: Session ,page: int ,page_size: int + ,db=Depends(get_db) ) -> tuple[List[Dict[str, Any]], int]: q = db.query(Student_Model) if stu_id: - q = q.filter(Student_Model.stu_id == stu_id) + q = q.filter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0) if stu_name: - q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%")) + q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%"),Student_Model.delete_status == 0) if class_id: - q= q.filter(Student_Model.class_id == class_id) + q= q.filter(Student_Model.class_id == class_id,Student_Model.delete_status == 0) total = q.count() r = q.offset((page - 1) * page_size).limit(page_size).all() data = [] diff --git a/需求文档.md b/需求文档.md index 7b3b79e..c8bef6f 100644 --- a/需求文档.md +++ b/需求文档.md @@ -23,10 +23,10 @@ dao层(每一个功能就是一个函数,判断能否功能复用) 通用模块 学生模块 -实现增加学生的功能 **xxx 做成中** -实现查询学生的功能:需要能进行多条件查询 **xxx 做成中** -实现更新学生信息的功能 **xxx 做成中** -实现删除学生信息的功能 (更新删除状态这个列,即软删除)**xxx 做成中** +实现增加学生的功能 **msj 做成中** +实现查询学生的功能:需要能进行多条件查询 **msj 做成中** +实现更新学生信息的功能 **msj 做成中** +实现删除学生信息的功能 (更新删除状态这个列,即软删除)**msj 做成中** 成绩模块 实现增加学生成绩的功能 **hzl 做成中**