数据库模型类名修改以及文件名修改
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -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="查询异常")
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from fastapi import FastAPI
|
||||
|
||||
from api.student_api import *
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(studentapi)
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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,'暂不明确')
|
||||
|
||||
|
||||
Reference in New Issue
Block a user