Compare commits
4
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1558103dfa | ||
|
|
5c671020d7 | ||
|
|
de85f8ef06 | ||
|
|
1fe2b6bc60 |
+12
-11
@@ -4,9 +4,9 @@ from scores.dao.score_dao import get_scores_dao,add_scores_dao,update_scores_dao
|
||||
from scores.model.score_model import Score
|
||||
from scores.schema.score_request import ScoreRequest, ScoreResponse,ScoreQuery
|
||||
|
||||
score_api = APIRouter()
|
||||
score_api = APIRouter(tags=['成绩'])
|
||||
|
||||
@score_api.get('/Score')
|
||||
@score_api.get('/Score',summary='成绩查询',description=f'查询条件为空即查询所有成绩')
|
||||
def get_scores(score:ScoreQuery=Depends(),db=Depends(get_db)):
|
||||
a = score.model_dump()
|
||||
aa = get_scores_dao(s=a,db=db)
|
||||
@@ -14,28 +14,29 @@ def get_scores(score:ScoreQuery=Depends(),db=Depends(get_db)):
|
||||
return aa
|
||||
raise HTTPException(status_code=404,detail='不存在')
|
||||
|
||||
@score_api.post('/Score',response_model=ScoreResponse)
|
||||
@score_api.post('/Score',response_model=ScoreResponse
|
||||
,summary='成绩添加')
|
||||
def add_scores(score:ScoreRequest=Depends(),db=Depends(get_db)):
|
||||
a = score.model_dump()
|
||||
# print(a,type(a))
|
||||
aa = add_scores_dao(b=a,db=db)
|
||||
if aa:
|
||||
return ScoreResponse(data=a)
|
||||
raise HTTPException(status_code=500,detail='没有')
|
||||
raise HTTPException(status_code=500,detail='不可以')
|
||||
|
||||
@score_api.put('/Score')
|
||||
def update_scores(score:ScoreRequest,sid:int,cid:int,num:int,tsub:str,db=Depends(get_db)):
|
||||
a = score.model_dump(exclude_unset=True,exclude={'id','sid','cid','num','tsub'})
|
||||
aa = update_scores_dao(sid = sid,cid = cid,num = num,tsub = tsub,update_data = a,db=db)
|
||||
@score_api.put('/Score',summary='成绩更新',description=f'查询条件为学号,班级号,考试序次,考试科目,然后更改这位同学此次科目的成绩')
|
||||
def update_scores(score:ScoreRequest,sid:int,cid:int,num:int,t_subject:int,db=Depends(get_db)):
|
||||
a = score.model_dump(exclude_unset=True,exclude={'id','sid','cid','num','t_subject'})
|
||||
aa = update_scores_dao(sid = sid,cid = cid,num = num,t_subject = t_subject,update_data = a,db=db)
|
||||
if aa==-1:
|
||||
raise HTTPException(status_code=500,detail='没有更新')
|
||||
if aa==0:
|
||||
raise HTTPException(status_code=404,detail='不存在')
|
||||
return {'message':'更新成功','data':a}
|
||||
|
||||
@score_api.delete('/Score')
|
||||
def delete_scores(sid:int,cid:int,tsub:str,db=Depends(get_db)):
|
||||
aaa = delete_scores_dao(sid,cid,tsub,db)
|
||||
@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:
|
||||
raise HTTPException(status_code=500,detail='删除失败')
|
||||
if aaa==0:
|
||||
|
||||
+24
-24
@@ -13,13 +13,13 @@ def get_scores_dao(s,db):
|
||||
ab = ab.filter(Score.num==s.get('num'))
|
||||
if s.get('score'):
|
||||
ab = ab.filter(Score.score==s.get('score'))
|
||||
if s.get('tsub'):
|
||||
ab = ab.filter(Score.tsub==s.get('tsub'))
|
||||
if s.get('t_subject'):
|
||||
ab = ab.filter(Score.t_subject==s.get('t_subject'))
|
||||
aa = ab.all()
|
||||
if aa:
|
||||
return [{'id':i.id,'sid':i.sid
|
||||
,'cid':i.cid,'num':i.num
|
||||
,'score': i.score,'tsub':i.tsub
|
||||
,'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]
|
||||
@@ -29,7 +29,7 @@ def get_scores_dao(s,db):
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.sid==b['sid']
|
||||
,Score.tsub==b['tsub']).all()
|
||||
,Score.t_subject==b['t_subject']).all()
|
||||
if aa:
|
||||
raise ValueError
|
||||
else:
|
||||
@@ -43,36 +43,36 @@ def add_scores_dao(b,db):
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def update_scores_dao(sid:int,cid:int,num:int,tsub:str,update_date:dict,db):
|
||||
if not update_date:
|
||||
def update_scores_dao(sid:int,cid:int,num:int,t_subject:int,update_data:dict,db):
|
||||
if not update_data:
|
||||
return 0
|
||||
update_date['update_date']=datetime.now
|
||||
update_data['update_date']=datetime.now()
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.sid == sid
|
||||
,Score.cid == cid
|
||||
,Score.num == num
|
||||
,Score.tsub == tsub ).update(update_date)
|
||||
,Score.t_subject == t_subject ).update(update_data)
|
||||
db.commit()
|
||||
return aa
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(e)
|
||||
return -1
|
||||
|
||||
|
||||
def delete_scores_dao(sid:int,cid:int,tsub:str,db):
|
||||
try:
|
||||
aaa = db.query(Score).filter(Score.sid ==sid
|
||||
,Score.cid == cid
|
||||
,Score.tsub == tsub
|
||||
,Score.is_deleted==0
|
||||
).update({'is_deleted':1
|
||||
,'deleted_date':datetime.now})
|
||||
db.commit()
|
||||
return aaa
|
||||
except Exception :
|
||||
db.rollback()
|
||||
return -1
|
||||
|
||||
|
||||
def delete_scores_dao(sid:int,cid:int,t_subject:int,db):
|
||||
try:
|
||||
aaa = db.query(Score).filter(Score.sid ==sid
|
||||
,Score.cid == cid
|
||||
,Score.t_subject == t_subject
|
||||
,Score.is_deleted==0
|
||||
).update({'is_deleted':1
|
||||
,'deleted_date':datetime.now()})
|
||||
db.commit()
|
||||
return aaa
|
||||
except Exception as e :
|
||||
db.rollback()
|
||||
print(e)
|
||||
return -1
|
||||
|
||||
|
||||
|
||||
|
||||
+11
-1
@@ -1,7 +1,17 @@
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
|
||||
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/student_manage_system?charset=utf8mb4'
|
||||
# 导入.env参数
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv( )
|
||||
DB_USER = os.getenv("DB_USER", "root")
|
||||
DB_PASSWORD = os.getenv("DB_PASSWORD", "")
|
||||
DB_HOST = os.getenv("DB_HOST", "localhost")
|
||||
DB_PORT = os.getenv("DB_PORT", "3306")
|
||||
DB_NAME = os.getenv("DB_NAME", "student_manage_system")
|
||||
|
||||
db_url = f"mysql+pymysql://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}?charset=utf8mb4"
|
||||
engine = create_engine(db_url)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
+8
-3
@@ -1,8 +1,13 @@
|
||||
from fastapi import FastAPI
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from scores.api.score_api import score_api
|
||||
from scores.database import *
|
||||
app = FastAPI()
|
||||
from scores.database import engine,Base
|
||||
|
||||
# 合并接口
|
||||
Scores_API = APIRouter()
|
||||
Scores_API.include_router(score_api)
|
||||
|
||||
# 自测接口
|
||||
app = FastAPI(title='成绩管理系统')
|
||||
app.include_router(score_api)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
from scores.database import Base
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float,ForeignKey
|
||||
|
||||
|
||||
|
||||
class Score(Base):
|
||||
__tablename__='scores'
|
||||
id = Column(Integer
|
||||
@@ -9,16 +12,19 @@ class Score(Base):
|
||||
,comment ='编号'
|
||||
)
|
||||
sid = Column(Integer
|
||||
,ForeignKey('students.id')
|
||||
,comment ='学生ID'
|
||||
)
|
||||
cid = Column(Integer
|
||||
,ForeignKey('classes.id')
|
||||
,comment ='班级ID'
|
||||
)
|
||||
num = Column(Integer
|
||||
,comment ='考试序次'
|
||||
)
|
||||
score = Column(Integer,nullable = False)
|
||||
tsub = Column(String(100)
|
||||
t_subject = Column(Integer
|
||||
,ForeignKey('subject.id')
|
||||
,nullable = False
|
||||
)
|
||||
create_date = Column(DateTime
|
||||
|
||||
@@ -6,7 +6,7 @@ class ScoreRequest(BaseModel):
|
||||
cid:int
|
||||
num:int
|
||||
score:int
|
||||
tsub:str
|
||||
t_subject:int
|
||||
|
||||
class ScoreQuery(BaseModel):
|
||||
id:int|None = None
|
||||
@@ -14,7 +14,7 @@ class ScoreQuery(BaseModel):
|
||||
cid:int|None = None
|
||||
num:int|None = None
|
||||
score:int|None = None
|
||||
tsub:str|None = None
|
||||
t_subject:int|None = None
|
||||
|
||||
class ScoreResponse(BaseModel):
|
||||
code:int = 200
|
||||
|
||||
Reference in New Issue
Block a user