From f28b1f5e7c7dd16e4b42389a383689632098344c Mon Sep 17 00:00:00 2001 From: 00MSJ1122 <1253313142@qq.com> Date: Sun, 20 Sep 2026 16:30:23 +0800 Subject: [PATCH] text --- PythonProject/dao/student_dao.py | 49 ++++++++++++++++++++++++-- PythonProject/model/student_model.py | 8 ++--- PythonProject/schema/student_schema.py | 31 +++++++++++++++- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index b93a8ed..55d7dd5 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -1,6 +1,7 @@ from fastapi import HTTPException,Depends from util.database import get_db from model.student_model import Student_Model +from typing import List, Optional, Dict, Any def select_age(min_age,max_age,session=Depends(get_db)): try: @@ -17,9 +18,51 @@ def select_age(min_age,max_age,session=Depends(get_db)): except Exception: raise HTTPException(status_code=500,detail="查询异常") -def select_gender(gender,session=Depends(get_db)): - if gender is None and session is None: - return None +def delete_student_dao(stu_id,db): + try: + rows = db.query(Student_Model).filter(Student_Model.stu_id == stu_id).delete() + except: + db.rollback() + rows = 0 + else: + db.commit() + finally: + return rows +def update_student_dao(stu_id,update_data,db): + try: + rows = db.query( Student_Model ).filter( Student_Model.stu_id == stu_id ).update( update_data ) + except: + db.rollback() + return False + else: + db.commit() + return rows + +def get_student_dao(stu_id:Optional[int] + ,stu_name:Optional[str] + ,class_id:Optional[int] + ,db: Session + ,page: int + ,page_size: int + ) -> tuple[List[Dict[str, Any]], int]: + q = db.query(Student_Model) + if stu_id: + q = q.filter(Student_Model.stu_id == stu_id) + if stu_name: + q = q.filter(Student_Model.stu_name.like(f"%{stu_name}%")) + if class_id: + q= q.filter(Student_Model.class_id == class_id) + total = q.count() + r = q.offset((page - 1) * page_size).limit(page_size).all() + data = [] + for i in r: + data.append({'stu_id': i.stu_id + ,'stu_name': i.stu_name + ,'class_id': i.class_id + ,'create_date': i.create_date + ,'update_date': i.update_date + }) + return data, total diff --git a/PythonProject/model/student_model.py b/PythonProject/model/student_model.py index f631579..73dace8 100644 --- a/PythonProject/model/student_model.py +++ b/PythonProject/model/student_model.py @@ -8,10 +8,10 @@ class Student_Model(Base): , primary_key=True , autoincrement=True ,comment='学生编号') - # class_id=Column(Integer - # ,ForeignKey('wl_class.class_id') - # ,nullable=False - # ,comment='班级编号') + class_id=Column(Integer + ,ForeignKey('wl_class.class_id') + ,nullable=False + ,comment='班级编号') stu_name=Column(String(100),comment='学生姓名') age=Column(Integer,comment='年龄') gender=Column(String(50),comment='性别') diff --git a/PythonProject/schema/student_schema.py b/PythonProject/schema/student_schema.py index c950945..c4ca20d 100644 --- a/PythonProject/schema/student_schema.py +++ b/PythonProject/schema/student_schema.py @@ -1,4 +1,33 @@ -from pydantic import BaseModel,field_serializer +from datetime import date +from typing import Self +from pydantic import BaseModel, field_serializer, field_validator,model_validator + + +class StudentRequest(BaseModel): + class_id:int |None = None + stu_name:str |None = None + age:int |None = None + gender:str |None = None + native_place:str |None = None + birthday:date + school:str |None = None + major:str |None = None + degree:str |None = None + admission_date:date + graduation_date:date + @field_validator('age') + @classmethod + def check_age(cls, v): + if v is not None and v < 0: + raise ValueError('年龄不能为负数') + return v + + @model_validator(mode='after') + def check_admission_graduation(self) -> Self: + if self.admission_date and self.graduation_date: + if self.admission_date > self.graduation_date: + raise ValueError ('入学日期不能晚于毕业日期') + return self class StudentResponse(BaseModel): code:int = 200 -- 2.54.0