Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d4d1d4fe2d |
@@ -35,7 +35,7 @@ def get_students(g:get_student_dao = Depends()
|
||||
,description=f'查询每次考试成绩都在80分以上的学生的编号,姓名和成绩。'
|
||||
)
|
||||
def get_students(db=Depends(get_db)):
|
||||
result = get_student_dao( db=db )
|
||||
result = get_score_80( db=db )
|
||||
if result:
|
||||
return result
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
@@ -39,9 +39,11 @@ def get_class_count(g , db):
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_score_80(db):
|
||||
q = (db.query(Score)
|
||||
.filter(Score.score>=80)
|
||||
.all())
|
||||
q = ( db.query(Score)
|
||||
.group_by(Score.score)
|
||||
.having( min(Score.score) >= 80)
|
||||
.all()
|
||||
)
|
||||
return q
|
||||
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from StatCalc.api.statcalc_api import statcalc_api
|
||||
from databases import *
|
||||
from StatCalc.model import statcalc_model
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
StatCalc_API = APIRouter()
|
||||
|
||||
+2
-2
@@ -11,9 +11,9 @@ Teachers_API.include_router(tea_api)
|
||||
app = FastAPI()
|
||||
app.include_router(tea_api,tags=["老师"])
|
||||
|
||||
Base.metadata.create_all(engine1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
Base.metadata.create_all(engine1)
|
||||
|
||||
import uvicorn
|
||||
uvicorn.run('main:app', host='0.0.0.0', port=55000)
|
||||
"""
|
||||
|
||||
@@ -7,13 +7,13 @@ from class_management.database import Base,engine
|
||||
Classes_API = APIRouter()
|
||||
Classes_API.include_router(class_api)
|
||||
|
||||
# 自测接口
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(class_api)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
if __name__ =='__main__':
|
||||
# 自测接口
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(class_api)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
import uvicorn
|
||||
uvicorn.run('main:app',host='127.0.0.1',port=12345)
|
||||
@@ -17,9 +17,10 @@ app.include_router(Teachers_API,tags=["teachers"])
|
||||
# app.include_router(StatCalc_API,tags=["StatCalc"])
|
||||
|
||||
|
||||
Base_all.metadata.create_all(bind=engine_all)
|
||||
|
||||
if __name__ == "__main__":
|
||||
Base_all.metadata.create_all(bind=engine_all)
|
||||
|
||||
def main():
|
||||
print("Hello from student-manage-system!")
|
||||
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
#scores模块解释
|
||||
|
||||
##项目层
|
||||
|
||||
web框架:FastAPI(生成接口)
|
||||
|
||||
ORM:sqlalchemy
|
||||
|
||||
校验:MYSQL(通过PyMysql驱动)
|
||||
|
||||
运行:uvicorn,端口12346
|
||||
|
||||
用FastAPI显露HTTP接口 ,对MysQL的scores表进行一个增,删,改,查的动作,删除是软删除,
|
||||
|
||||
api是接口层,成绩信息的增加,删除,更新,查询
|
||||
|
||||
dao是数据操作层,查询get_scores_dao
|
||||
|
||||
更新add_scores_dao
|
||||
|
||||
增加update_scores_dao
|
||||
|
||||
删除delete_scores_dao
|
||||
|
||||
统计查询80分以上的get_top_students_dao
|
||||
|
||||
查询连续两次不及格的get_failing_students_dao
|
||||
|
||||
班级平均分get_class_avg_dao
|
||||
|
||||
model是数据库表模型层,字段信息 id *pk*
|
||||
|
||||
sid 链接学生表的学生ID
|
||||
|
||||
cid 链接班级表的班级ID
|
||||
|
||||
num 考试序次
|
||||
|
||||
score 考试成绩
|
||||
|
||||
t_subject 链接科目表的科目ID
|
||||
|
||||
create_date 创建数据的时间
|
||||
|
||||
update_date 更新数据的是时间
|
||||
|
||||
is_deleted 逻辑删除
|
||||
|
||||
deleted_date 删除的时间
|
||||
|
||||
schema是请求体和响应体模型层, 查询的ScoreQuery
|
||||
|
||||
更新,增加,删除的ScoreRequest
|
||||
|
||||
databases是数据库定义配置文件, 链接引擎对象db_url,链接地址engine,
|
||||
|
||||
建表的基类Base,数据库会话管理Session
|
||||
|
||||
会话实例def get_db()
|
||||
|
||||
main是主程序文件,将配置文件的接口导入,创建接口运行
|
||||
@@ -37,7 +37,7 @@ def update_scores(score:ScoreRequest,sid:int,cid:int,num:int,t_subject:int,db=De
|
||||
@score_api.delete('/Score',summary='成绩删除',description=f'查询条件为学号,班级号,考试科目,然后进行删除')
|
||||
def delete_scores(sid:int,cid:int,t_subject:int,db=Depends(get_db)):
|
||||
aaa = delete_scores_dao(sid,cid,t_subject,db)
|
||||
if aaa==-1:
|
||||
if aaa==1:
|
||||
raise HTTPException(status_code=500,detail='删除失败')
|
||||
if aaa==0:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from scores.database import get_db
|
||||
from scores.dao.statistic_dao import get_top_students_dao,get_failing_students_dao,get_class_avg_dao
|
||||
|
||||
statistic_api = APIRouter()
|
||||
|
||||
@statistic_api.get('/statistic', summary='查询每次考试都在80分以上的学生')
|
||||
def get_top_students(db=Depends(get_db)):
|
||||
q = get_top_students_dao(db)
|
||||
if not q:
|
||||
return []
|
||||
return q
|
||||
|
||||
@statistic_api.get('/Score/failing', summary='查询两次以上不及格的学生')
|
||||
def get_failing_students(db=Depends(get_db)):
|
||||
result = get_failing_students_dao(db)
|
||||
if not result:
|
||||
return []
|
||||
return result
|
||||
|
||||
@statistic_api.get('/Score/class-avg', summary='统计每次考试每个班级的平均分')
|
||||
def get_class_avg(db=Depends(get_db)):
|
||||
result = get_class_avg_dao(db)
|
||||
if not result:
|
||||
return []
|
||||
return result
|
||||
@@ -19,7 +19,7 @@ def get_scores_dao(s,db):
|
||||
if aa:
|
||||
return [{'id':i.id,'sid':i.sid
|
||||
,'cid':i.cid,'num':i.num
|
||||
,'score': i.score,'t_subject':i.t_subject
|
||||
,'score': i.score,'t_subject':i.tsub
|
||||
,'create_date':i.create_date,'update_date':i.update_date
|
||||
,'is_deleted': i.is_deleted,'deleted_date': i.deleted_date
|
||||
}for i in aa]
|
||||
@@ -28,7 +28,8 @@ def get_scores_dao(s,db):
|
||||
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.t_subject==b['t_subject']).all()
|
||||
aa = db.query(Score).filter(Score.sid==b['sid']
|
||||
,Score.t_subject==b['t_subject']).all()
|
||||
if aa:
|
||||
raise ValueError
|
||||
else:
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
from sqlalchemy import func
|
||||
from scores.model.score_model import Score
|
||||
from students.model.students_model import Students
|
||||
from class_management.model.class_management_model import ClassInfo
|
||||
|
||||
def get_top_students_dao(db):
|
||||
top_sids = (db.query(Score.sid)
|
||||
.filter(Score.is_deleted == 0)
|
||||
.group_by(Score.sid, Score.num)
|
||||
.having(func.min(Score.score) >= 80)
|
||||
.subquery() )
|
||||
|
||||
results = (db.query(Students.id,
|
||||
Students.name,
|
||||
Score.num,
|
||||
Score.t_subject,
|
||||
Score.score,)
|
||||
.join(Score, Students.id == Score.sid)
|
||||
.filter( Score.sid.in_(top_sids),
|
||||
Score.is_deleted == 0,)
|
||||
.order_by(Students.id, Score.num)
|
||||
.all())
|
||||
|
||||
return [{"sid": r.sid,
|
||||
"name": r.name,
|
||||
"score": r.score,}
|
||||
for r in results]
|
||||
|
||||
|
||||
def get_failing_students_dao(db):
|
||||
failing_sids = (db.query(Score.sid)
|
||||
.filter(Score.score < 60,
|
||||
Score.is_deleted == 0,)
|
||||
.group_by(Score.sid)
|
||||
.having(func.count() >= 2)
|
||||
.subquery())
|
||||
|
||||
results = (db.query(
|
||||
Students.id,
|
||||
Students.name,
|
||||
ClassInfo.name.label("class_name"),
|
||||
Score.num,
|
||||
Score.t_subject,
|
||||
Score.score,)
|
||||
.join(Score, Students.id == Score.sid)
|
||||
.join(ClassInfo, Score.cid == ClassInfo.id)
|
||||
.filter(Score.sid.in_(failing_sids),
|
||||
Score.score < 60,
|
||||
Score.is_deleted == 0,)
|
||||
.order_by(ClassInfo.id, Score.num)
|
||||
.all())
|
||||
|
||||
return [{"sid": r.sid,
|
||||
"name": r.name,
|
||||
"class_name": r.class_name,
|
||||
"num": r.num,
|
||||
"t_subject": r.tsub,
|
||||
"score": r.score,}
|
||||
for r in results]
|
||||
|
||||
|
||||
|
||||
def get_class_avg_dao(db):
|
||||
results = (db.query(Score.num,
|
||||
Score.cid,
|
||||
ClassInfo.name.label("class_name"),
|
||||
func.avg(Score.score).label("avg_score"))
|
||||
.join(ClassInfo, Score.cid == ClassInfo.id)
|
||||
.filter(Score.is_deleted == 0)
|
||||
.group_by(Score.num, Score.cid, ClassInfo.name)
|
||||
.order_by(func.avg(Score.score).desc())
|
||||
.all())
|
||||
|
||||
return [ {"num": r.num,
|
||||
"cid": r.cid,
|
||||
"class_name": r.class_name,
|
||||
"avg_score": round(float(r.avg_score), 2),}
|
||||
for r in results]
|
||||
+4
-6
@@ -1,21 +1,19 @@
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from scores.api.score_api import score_api
|
||||
from scores.database import engine,Base
|
||||
from scores.api.statistic_api import statistic_api
|
||||
|
||||
# 合并接口
|
||||
Scores_API = APIRouter()
|
||||
Scores_API.include_router(score_api)
|
||||
|
||||
# 自测接口
|
||||
app = FastAPI(tags=['成绩'], title='成绩管理系统')
|
||||
app.include_router(score_api)
|
||||
app.include_router(statistic_api)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# 自测接口
|
||||
app = FastAPI(tags=['成绩'], title='成绩管理系统')
|
||||
app.include_router(score_api)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
import uvicorn
|
||||
uvicorn.run('scores.main:app',host='127.0.0.1',port=12346)
|
||||
uvicorn.run('main:app',host='127.0.0.1',port=12346)
|
||||
+3
-2
@@ -11,12 +11,13 @@ Shtudent_Jiuye.include_router(CURD)
|
||||
|
||||
|
||||
app=FastAPI()
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
app.include_router(CURD,tags=['就业管理系统'])
|
||||
|
||||
|
||||
|
||||
if __name__=='__main__':
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
import uvicorn
|
||||
uvicorn.run('main:app', host='127.0.0.1', port=12395)
|
||||
|
||||
|
||||
+11
-10
@@ -8,18 +8,19 @@ from contextlib import asynccontextmanager
|
||||
Students_API = APIRouter()
|
||||
Students_API.include_router(s_api)
|
||||
|
||||
# 自测接口
|
||||
@asynccontextmanager
|
||||
async def S_Fastapi(app: FastAPI):
|
||||
Base.metadata.create_all(engine1)
|
||||
yield
|
||||
engine1.dispose()
|
||||
|
||||
app = FastAPI(title="学生管理系统",lifespan=S_Fastapi) # 这里必须把上面写的加上
|
||||
app.include_router(s_api,tags=["students"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# 自测接口
|
||||
@asynccontextmanager
|
||||
async def S_Fastapi(app: FastAPI):
|
||||
Base.metadata.create_all(engine1)
|
||||
yield
|
||||
engine1.dispose()
|
||||
|
||||
|
||||
app = FastAPI(title="学生管理系统", lifespan=S_Fastapi) # 这里必须把上面写的加上
|
||||
app.include_router(s_api, tags=["students"])
|
||||
|
||||
def main():
|
||||
print("Hello from student-manage-system!")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user