两个模块写好
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
from fastapi import APIRouter,HTTPException,Depends
|
||||||
|
from DoubaoTeam.core.database import get_db
|
||||||
|
from DoubaoTeam.models.student import Student
|
||||||
|
from DoubaoTeam.models.score import Score
|
||||||
|
from DoubaoTeam.dao.statistics import *
|
||||||
|
from DoubaoTeam.schemas.common import SuccessResponse
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
|
||||||
|
statistics_router=APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@statistics_router.get("/scores/age-over-30",response_model=SuccessResponse)
|
||||||
|
def api_age_over_thirty(db:Session=Depends(get_db)):
|
||||||
|
try:
|
||||||
|
age_over_thirty_list=age_over_thirty(Student,db)
|
||||||
|
return SuccessResponse(status_code=200,msg=age_over_thirty_list)
|
||||||
|
except NoExist as e:
|
||||||
|
raise HTTPException(status_code=404,detail=str(e))
|
||||||
|
# 这边功能函数返回的是一个字典
|
||||||
|
@statistics_router.get("/classes/gender-count",response_model=SuccessResponse)
|
||||||
|
def api_gender_count(db:Session=Depends(get_db)):
|
||||||
|
try:
|
||||||
|
result = gender_count(Student,db)
|
||||||
|
return SuccessResponse(status_code=200,msg=result)
|
||||||
|
except NoExist as e:
|
||||||
|
raise HTTPException(status_code=404,detail=str(e))
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
from DoubaoTeam.schemas.statistics import NoExist
|
||||||
|
from sqlalchemy import func
|
||||||
|
|
||||||
|
#`GET /statistics/students/age-over-30`
|
||||||
|
#查询所有超过 30 岁的学员信息
|
||||||
|
|
||||||
|
def age_over_thirty(Student,db):
|
||||||
|
student_list=db.query(Student).filter(Student.age>=30).all()
|
||||||
|
if not student_list :
|
||||||
|
raise NoExist('无目标年龄大于30岁')
|
||||||
|
return student_list
|
||||||
|
|
||||||
|
def gender_count(Student,db):
|
||||||
|
result=db.query(Student.class_id,Student.gender,func.count(Student.id)).group_by(Student.class_id,Student.gender).all()
|
||||||
|
if not result :
|
||||||
|
raise NoExist('班里招点人吧,要倒闭了')
|
||||||
|
dict1 = {}
|
||||||
|
for i,j,k in result:
|
||||||
|
if i not in dict1 :
|
||||||
|
dict1[i]={'男':0,'女':0}
|
||||||
|
dict[i][j]+=k
|
||||||
|
return dict1
|
||||||
|
#查询每次考试成绩都在 80 分以上的学生编号、姓名和成绩
|
||||||
|
def score_over_eighty(Score,Student,db):
|
||||||
|
student_list=db.query(Student.id, Student.student_name)\
|
||||||
|
.join(Student,Score.student_id==Student.id).group_by(Student.student_no,Student.student_name)\
|
||||||
|
.having(func.min(Score.score)>80).all()
|
||||||
|
for i in student_list:
|
||||||
|
student_list2 = db.query(Student.student_no, Student.student_name,Score.score).join(Student,Score.student_id==Student.id).filter(i.id==Student.id).all()
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from api.example import example_router
|
from api.example import example_router
|
||||||
from middleware import log_middleware
|
from middleware import log_middleware
|
||||||
|
from api.statistics import statistics_router
|
||||||
import uvicorn
|
import uvicorn
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
app.middleware("http")(log_middleware)
|
app.middleware("http")(log_middleware)
|
||||||
@@ -15,6 +16,7 @@ app.middleware("http")(log_middleware)
|
|||||||
|
|
||||||
#-------------张昕浩---------------
|
#-------------张昕浩---------------
|
||||||
app.include_router(example_router)
|
app.include_router(example_router)
|
||||||
|
app.include_router(statistics_router, prefix="/statistics")
|
||||||
|
|
||||||
#-------------曾凯---------------
|
#-------------曾凯---------------
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,4 @@
|
|||||||
|
class NoExist(Exception):
|
||||||
|
def __init__(self,value):
|
||||||
|
self.value = value
|
||||||
|
pass
|
||||||
Reference in New Issue
Block a user