Merge pull request 'Zbl' (#7) from zbl into main

Reviewed-on: #7
This commit was merged in pull request #7.
This commit is contained in:
2026-09-22 14:40:59 +08:00
4 changed files with 143 additions and 0 deletions
+45
View File
@@ -0,0 +1,45 @@
from dao import get_db
from schema import Test
from fastapi import APIRouter,Depends
from dao import a,a1,a2,a3,a4,a5
ScoreAPI=APIRouter()
ScoreAPI1=APIRouter()
@ScoreAPI.post('/scores',tags=['增'])
def scores(s:Test,db=Depends(get_db)):
a(s,db)
return {'code':200,'detail':'添加成功!',}
@ScoreAPI.delete('/scores',tags=['删'])
def scores1(stu_id:int,db=Depends(get_db)):
r = a1(stu_id,db)
if r==0:
return '没有该学生'
else:
return f'已删除学生编号为{stu_id}的学生'
@ScoreAPI.put('/scores',tags=['改'])
def scores2(stu_id:int,exam_seq:int,s:Test,db=Depends(get_db)):
r=a2(stu_id,exam_seq,s,db)
if r!=0:
return '更新成功'
else:
return '没有该数据'
@ScoreAPI.get('/scores',tags=['查'])
def Scores3(stu_id:int,exam_seq:int,db=Depends(get_db)):
r=a3(stu_id,exam_seq,db)
try:
return {'成绩编号':r.id,'学生编号':r.stu_id,f'第{exam_seq}次成绩:':r.score,'创建日期':r.create_date,'更新日期':r.update_date}
except:
return '没有该学生'
@ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
def scores4(stu_id:int,exam_seq:int,db=Depends(get_db)):
r=a4(stu_id, exam_seq, db)
if r ==0:
return '没有该数据'
else:
return '已删除'
@ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
def scores5(stu_id:int,db=Depends(get_db)):
s,r,a=a5(stu_id,db)
return f'总分:{s},平均分:{s/len(r)},最大值:{max(a)},最小值:{min(a)}'
+48
View File
@@ -0,0 +1,48 @@
from model import Session
from model import Scores
def get_db():
db = Session()
try:
yield db
finally:
db.close()
# @ScoreAPI.post('/scores')
def a(s,db):
d = s.model_dump()
o = Scores(**d)
db.add(o)
db.commit()
# @ScoreAPI.delete('/scores')
def a1(stu_id,db):
r=db.query(Scores).filter(Scores.stu_id==stu_id).delete()
db.commit()
return r
# @ScoreAPI.put('/scores')
def a2(stu_id,exam_seq,s,db):
r = db.query(Scores).filter((Scores.stu_id == stu_id) & (Scores.exam_seq == exam_seq)).update(
s.model_dump(exclude={'stu_id', 'exam_seq'}))
db.commit()
return r
#@ScoreAPI.get('/scores')
def a3(stu_id,exam_seq,db):
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).first()
return r
# @ScoreAPI1.put('/scores/{stu_id}',tags=['软删除'])
def a4(stu_id, exam_seq, db):
r=db.query(Scores).filter((Scores.stu_id==stu_id)&(Scores.exam_seq==exam_seq)).update({Scores.deleted:0})
db.commit()
return r
# @ScoreAPI1.get('/scores/{stu_id}',tags=['总分,平均分,最大值,最小值'])
def a5(stu_id,db):
r = db.query(Scores).filter((Scores.stu_id == stu_id)).all()
s = 0
a = []
for i in r:
s += i.score
a.append(i.score)
return s,r,a
+45
View File
@@ -0,0 +1,45 @@
from sqlalchemy import *
from datetime import datetime
from sqlalchemy.orm import declarative_base,sessionmaker
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/ai0824?charset=utf8'
engine=create_engine(db_url , pool_size=50,echo=True)
Base=declarative_base()
class Scores(Base):
__tablename__ = 's_scores'
id=Column( Integer
, primary_key=True
,autoincrement=True
,comment='成绩编号'
)
stu_id=Column(Integer
,comment='学生编号'
, nullable=False
)
exam_seq=Column(Integer
, nullable=True
,comment='考试序次'
)
score=Column(Float
,comment='分数'
)
create_date=Column(DATETIME
, default=datetime.now
,comment='创建时间'
)
update_date=Column(DATETIME
, default=datetime.now
,onupdate=datetime.now
,comment='更新时间'
)
deleted=Column(Integer
,default=1
, comment='软删除'
)
Base.metadata.create_all(engine)
Session = sessionmaker( bind=engine
,autoflush=False
,autocommit = False
)
+5
View File
@@ -0,0 +1,5 @@
from pydantic import BaseModel
class Test(BaseModel):
stu_id:int
exam_seq:int
score:float