diff --git a/PythonProject/api/student_api.py b/PythonProject/api/student_api.py new file mode 100644 index 0000000..b71aafc --- /dev/null +++ b/PythonProject/api/student_api.py @@ -0,0 +1,11 @@ +from fastapi import APIRouter +from dao.student_dao import select_age +from schema.student_schema import StudentResponse + +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] + diff --git a/PythonProject/dao/student_dao.py b/PythonProject/dao/student_dao.py index db973ca..f6f5d02 100644 --- a/PythonProject/dao/student_dao.py +++ b/PythonProject/dao/student_dao.py @@ -1,2 +1,20 @@ -from model import student_model +from fastapi import HTTPException,Depends +from util.database import get_db +from model.student_model import Student_Model + + +def select_age(min_age,max_age,session=Depends(get_db)): + try: + if min_age is None and max_age is None: + 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) + if max_age is not None: + session = session.filter(Student_Model.age <= max_age) + return session.all() + except HTTPException: + raise + except Exception: + raise HTTPException(status_code=500,detail="查询异常") diff --git a/PythonProject/main.py b/PythonProject/main.py index b21386e..ce32cfe 100644 --- a/PythonProject/main.py +++ b/PythonProject/main.py @@ -1,4 +1,5 @@ from fastapi import FastAPI - +from api.student_api import * app = FastAPI() +app.include_router(studentapi) diff --git a/PythonProject/model/student_model.py b/PythonProject/model/student_model.py index c33c024..f631579 100644 --- a/PythonProject/model/student_model.py +++ b/PythonProject/model/student_model.py @@ -1,7 +1,6 @@ from sqlalchemy import DATETIME,Column, Integer, String, Date,ForeignKey -from sqlalchemy.orm import declarative_base -import datetime -Base = declarative_base() +from datetime import datetime +from util.database import Base,engine class Student_Model(Base): __tablename__ = 'wl_student' @@ -9,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='性别') @@ -23,12 +22,12 @@ class Student_Model(Base): degree=Column(String(50),comment='学历') admission_date=Column(Date,comment='入学日期') graduation_date=Column(Date,comment='毕业日期') + progress=Column(Integer,default=0,comment='课程进度:学习中=0,求职中=1,已就业=2') create_date=Column(DATETIME ,default=datetime.now) update_date=Column(DATETIME ,default=datetime.now ,onupdate=datetime.now) - progress=Column(Integer,default=0) - +Base.metadata.create_all(engine) diff --git a/PythonProject/schema/stu_emp_Info .py b/PythonProject/schema/stu_emp_Info .py deleted file mode 100644 index e69de29..0000000 diff --git a/PythonProject/schema/student_schema.py b/PythonProject/schema/student_schema.py new file mode 100644 index 0000000..c950945 --- /dev/null +++ b/PythonProject/schema/student_schema.py @@ -0,0 +1,20 @@ +from pydantic import BaseModel,field_serializer + +class StudentResponse(BaseModel): + code:int = 200 + detail:str = 'ok' + stu_name:str + age:int + gender:str + progress:int + + @field_serializer('progress') + def int_to_string(cls,progress:int): + d1 ={ + 0:'学习中', + 1:'求职中', + 2:'已就业' + } + return d1.get(progress,'暂不明确') + +