学生
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
from fastapi import HTTPException,Depends
|
from fastapi import HTTPException,Depends
|
||||||
from util.database import get_db
|
from util.database import get_db
|
||||||
from model.student_model import Student_Model
|
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)):
|
def select_age(min_age,max_age,session=Depends(get_db)):
|
||||||
try:
|
try:
|
||||||
@@ -17,8 +18,48 @@ def select_age(min_age,max_age,session=Depends(get_db)):
|
|||||||
except Exception:
|
except Exception:
|
||||||
raise HTTPException(status_code=500,detail="查询异常")
|
raise HTTPException(status_code=500,detail="查询异常")
|
||||||
|
|
||||||
def abc():
|
def delete_student_dao(stu_id,db):
|
||||||
return None
|
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
|
||||||
|
|||||||
@@ -8,10 +8,10 @@ class Student_Model(Base):
|
|||||||
, primary_key=True
|
, primary_key=True
|
||||||
, autoincrement=True
|
, autoincrement=True
|
||||||
,comment='学生编号')
|
,comment='学生编号')
|
||||||
# class_id=Column(Integer
|
class_id=Column(Integer
|
||||||
# ,ForeignKey('wl_class.class_id')
|
,ForeignKey('wl_class.class_id')
|
||||||
# ,nullable=False
|
,nullable=False
|
||||||
# ,comment='班级编号')
|
,comment='班级编号')
|
||||||
stu_name=Column(String(100),comment='学生姓名')
|
stu_name=Column(String(100),comment='学生姓名')
|
||||||
age=Column(Integer,comment='年龄')
|
age=Column(Integer,comment='年龄')
|
||||||
gender=Column(String(50),comment='性别')
|
gender=Column(String(50),comment='性别')
|
||||||
@@ -30,4 +30,3 @@ class Student_Model(Base):
|
|||||||
,onupdate=datetime.now)
|
,onupdate=datetime.now)
|
||||||
|
|
||||||
Base.metadata.create_all(engine)
|
Base.metadata.create_all(engine)
|
||||||
|
|
||||||
|
|||||||
@@ -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):
|
class StudentResponse(BaseModel):
|
||||||
code:int = 200
|
code:int = 200
|
||||||
@@ -16,5 +45,3 @@ class StudentResponse(BaseModel):
|
|||||||
2:'已就业'
|
2:'已就业'
|
||||||
}
|
}
|
||||||
return d1.get(progress,'暂不明确')
|
return d1.get(progress,'暂不明确')
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user