成绩建表
This commit is contained in:
+17
-17
@@ -4,36 +4,36 @@ 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
|
||||
|
||||
|
||||
score_api = APIRouter()
|
||||
|
||||
|
||||
@score_api.get('/Score')
|
||||
def get_scores(cid:int,score,subject:str,abc=Depends(get_db)):
|
||||
aa = get_scores_dao(cid,score,subject,abc)
|
||||
def get_scores(score:ScoreRequest=Depends(),db=Depends(get_db)):
|
||||
a = score.model_dump()
|
||||
aa = get_scores_dao(s=a,db=db)
|
||||
if aa:
|
||||
return aa
|
||||
raise HTTPException(status_code=404,detail='不存在')
|
||||
|
||||
@score_api.post('/Score',response_model=ScoreResponse)
|
||||
def add_scores(score:ScoreRequest,db=Depends(get_db)):
|
||||
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 not aa:
|
||||
raise HTTPException(status_code=500,detail='没有')
|
||||
return ScoreResponse(data=a)
|
||||
if aa:
|
||||
return ScoreResponse(data=a)
|
||||
raise HTTPException(status_code=500,detail='没有')
|
||||
|
||||
@score_api.put('/Score,{cid}')
|
||||
def get_scores(score:ScoreRequest,cid:int,db=Depends(get_db)):
|
||||
a = score.model_dump(exclude_unset=True)
|
||||
aa = update_scores_dao(cid = cid,update_date=a,db=db)
|
||||
@score_api.put('/Score')
|
||||
def update_scores(score:ScoreRequest,student_id:int,class_id:int,db=Depends(get_db)):
|
||||
a = score.model_dump()
|
||||
aa = update_scores_dao(student_id = student_id,class_id = class_id,update_date=a,db=db)
|
||||
if not aa:
|
||||
raise HTTPException(status_code=500,detail='没有更新')
|
||||
return {'code':200,'totals':a,'detail':'更新成功'}
|
||||
raise HTTPException(status_code=404,detail='没有更新')
|
||||
return {'message':'更新成功','data':a}
|
||||
|
||||
@score_api.delete('/Score,{cid}')
|
||||
def get_scores(cid:int,db=Depends(get_db)):
|
||||
aaa = delete_scores_dao(cid,db)
|
||||
@score_api.delete('/Score')
|
||||
def get_scores(student_id:int,class_id:int,db=Depends(get_db)):
|
||||
aaa = delete_scores_dao(student_id,class_id,db)
|
||||
if not aaa:
|
||||
raise HTTPException(status_code=500,detail='没有删除')
|
||||
return {'code':200,'totals':aaa,'detail':'更新成功'}
|
||||
|
||||
+19
-18
@@ -1,37 +1,38 @@
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_scores_dao(cid,score,subject,db):
|
||||
def get_scores_dao(s,db):
|
||||
ab = db.query(Score)
|
||||
if cid:
|
||||
ab = ab.filter(Score.cid==cid)
|
||||
if score:
|
||||
ab = ab.filter(Score.score==score)
|
||||
if subject:
|
||||
ab = ab.filter(Score.subject==subject)
|
||||
if s.get('score_id'):
|
||||
ab = ab.filter(Score.score_id==s.get('score_id'))
|
||||
aa = ab.all()
|
||||
if aa:
|
||||
return [{'Score_cid':i.cid,'Score_score':i.cscore
|
||||
,'Score_subject':i.subject,'reate_date':i.create_date
|
||||
,'update_date':i.update_date}for i in aa]
|
||||
return [{'Score.score_id':i.score_id,'Score.student_id':i.student_id
|
||||
,'Score.class_id':i.class_id,'Score.score_num':i.score_num
|
||||
,'Score.score': i.score,'Score.subject':i.subject
|
||||
,'Score.create_date':i.create_date,'Score.update_date':i.update_date
|
||||
,'Score.is_deleted': i.is_deleted,'Score.deleted_date': i.deleted_date
|
||||
}for i in aa]
|
||||
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.cid==b['cid']).all()
|
||||
aa = db.query(Score).filter(Score.student_id==b['student_id']
|
||||
,Score.subject==b['subject']).all()
|
||||
if aa:
|
||||
raise ValueError
|
||||
else:
|
||||
z = Score(**b)
|
||||
db.add(z)
|
||||
else:
|
||||
raise ValueError
|
||||
except:
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(e)
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def update_scores_dao(cid,update_date,db):
|
||||
def update_scores_dao(student_id:int,class_id:int,update_date,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.cid ==cid).update(update_date)
|
||||
aa = db.query(Score).filter(Score.student_id == student_id , Score.class_id == class_id,update_date=a).update()
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
@@ -39,9 +40,9 @@ def update_scores_dao(cid,update_date,db):
|
||||
db.commit()
|
||||
return aa
|
||||
|
||||
def delete_scores_dao(cid,db):
|
||||
def delete_scores_dao(student_id,class_id,db):
|
||||
try:
|
||||
aaa = db.query(Score).filter(Score.cid ==cid).delete()
|
||||
aaa = db.query(Score).filter(Score.student_id ==student_id,class_id = class_id).delete()
|
||||
except:
|
||||
db.rollback()
|
||||
aaa = 0
|
||||
|
||||
@@ -3,15 +3,20 @@ from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float
|
||||
class Score(Base):
|
||||
__tablename__='score'
|
||||
cid = Column(Integer
|
||||
score_id = Column(Integer
|
||||
,primary_key = True
|
||||
,autoincrement = True
|
||||
,comment ='编号'
|
||||
)
|
||||
student_id = Column(Integer
|
||||
,comment ='学生ID'
|
||||
)
|
||||
class_id = Column(Integer
|
||||
,comment ='班级ID'
|
||||
)
|
||||
score_num = Column(Integer
|
||||
,primary_key = True
|
||||
,comment ='考试序次'
|
||||
)
|
||||
)
|
||||
score = Column(Integer,nullable = False)
|
||||
subject = Column(String(100)
|
||||
,nullable = False
|
||||
@@ -21,13 +26,12 @@ class Score(Base):
|
||||
)
|
||||
update_date = Column(DateTime
|
||||
,default = datetime.now
|
||||
,onupdate = datetime.now
|
||||
)
|
||||
)
|
||||
is_deleted = Column(Integer
|
||||
,default = 0
|
||||
,comment='是否删除: 0-正常,1-已删除'
|
||||
)
|
||||
deleted_date=Column(DateTime
|
||||
,default=datetime.now
|
||||
,default=None
|
||||
,comment='删除时间'
|
||||
)
|
||||
|
||||
@@ -1,10 +1,21 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ScoreRequest(BaseModel):
|
||||
cid:int
|
||||
cscore:float
|
||||
score_id:int
|
||||
student_id:int
|
||||
class_id:int
|
||||
score_num:int
|
||||
score:int
|
||||
subject:str
|
||||
|
||||
class ScoreQuery(BaseModel):
|
||||
score_id:int|None = None
|
||||
student_id:int|None = None
|
||||
class_id:int|None = None
|
||||
score_num:int|None = None
|
||||
score:int|None = None
|
||||
subject:str|None = None
|
||||
|
||||
class ScoreResponse(BaseModel):
|
||||
code:int = 200
|
||||
detail:str = 'OK'
|
||||
|
||||
Reference in New Issue
Block a user