成绩建表
This commit is contained in:
@@ -2,12 +2,12 @@ from fastapi import APIRouter, Depends,HTTPException
|
||||
from scores.database import get_db
|
||||
from scores.dao.score_dao import get_scores_dao,add_scores_dao,update_scores_dao,delete_scores_dao
|
||||
from scores.model.score_model import Score
|
||||
from scores.schema.score_request import ScoreRequest, ScoreResponse
|
||||
from scores.schema.score_request import ScoreRequest, ScoreResponse,ScoreQuery
|
||||
|
||||
score_api = APIRouter()
|
||||
|
||||
@score_api.get('/Score')
|
||||
def get_scores(score:ScoreRequest=Depends(),db=Depends(get_db)):
|
||||
def get_scores(score:ScoreQuery=Depends(),db=Depends(get_db)):
|
||||
a = score.model_dump()
|
||||
aa = get_scores_dao(s=a,db=db)
|
||||
if aa:
|
||||
@@ -24,9 +24,9 @@ def add_scores(score:ScoreRequest=Depends(),db=Depends(get_db)):
|
||||
raise HTTPException(status_code=500,detail='没有')
|
||||
|
||||
@score_api.put('/Score')
|
||||
def update_scores(score:ScoreRequest,student_id:int,class_id:int,db=Depends(get_db)):
|
||||
def update_scores(score:ScoreRequest,sid:int,cid: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)
|
||||
aa = update_scores_dao(sid = sid,cid = cid,update_date = a,db=db)
|
||||
if not aa:
|
||||
raise HTTPException(status_code=404,detail='没有更新')
|
||||
return {'message':'更新成功','data':a}
|
||||
|
||||
+26
-14
@@ -1,22 +1,34 @@
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_scores_dao(s,db):
|
||||
ab = db.query(Score)
|
||||
if s.get('score_id'):
|
||||
ab = ab.filter(Score.score_id==s.get('score_id'))
|
||||
ab = db.query(Score).filter(Score.is_deleted ==0)
|
||||
if s.get('id'):
|
||||
ab = ab.filter(Score.id==s.get('id'))
|
||||
if s.get('sid'):
|
||||
ab = ab.filter(Score.sid==s.get('sid'))
|
||||
if s.get('cid'):
|
||||
ab = ab.filter(Score.cid==s.get('cid'))
|
||||
if s.get('num'):
|
||||
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'))
|
||||
aa = ab.all()
|
||||
if 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
|
||||
return [{'id':i.id,'sid':i.sid
|
||||
,'cid':i.cid,'num':i.num
|
||||
,'score': i.score,'tsub':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]
|
||||
else:
|
||||
return []
|
||||
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.student_id==b['student_id']
|
||||
,Score.subject==b['subject']).all()
|
||||
aa = db.query(Score).filter(Score.sid==b['sid']
|
||||
,Score.tsub==b['tsub']).all()
|
||||
if aa:
|
||||
raise ValueError
|
||||
else:
|
||||
@@ -30,9 +42,9 @@ def add_scores_dao(b,db):
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def update_scores_dao(student_id:int,class_id:int,update_date,db):
|
||||
def update_scores_dao(sid:int,cid:int,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.student_id == student_id , Score.class_id == class_id,update_date=a).update()
|
||||
aa = db.query(Score).filter(Score.sid == sid , Score.cid == cid,update_date=a).update()
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
@@ -40,9 +52,9 @@ def update_scores_dao(student_id:int,class_id:int,update_date,db):
|
||||
db.commit()
|
||||
return aa
|
||||
|
||||
def delete_scores_dao(student_id,class_id,db):
|
||||
def delete_scores_dao(sid,cid,db):
|
||||
try:
|
||||
aaa = db.query(Score).filter(Score.student_id ==student_id,class_id = class_id).delete()
|
||||
aaa = db.query(Score).filter(Score.sid ==sid,cid = cid).delete()
|
||||
except:
|
||||
db.rollback()
|
||||
aaa = 0
|
||||
|
||||
+1
-1
@@ -1,7 +1,7 @@
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
|
||||
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/a0824?charset=utf8mb4'
|
||||
db_url='mysql+pymysql://root:123456@127.0.0.1:3306/student_manage_system?charset=utf8mb4'
|
||||
engine = create_engine(db_url)
|
||||
|
||||
Base = declarative_base()
|
||||
|
||||
@@ -2,23 +2,23 @@ from scores.database import Base
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float
|
||||
class Score(Base):
|
||||
__tablename__='score'
|
||||
score_id = Column(Integer
|
||||
__tablename__='scores'
|
||||
id = Column(Integer
|
||||
,primary_key = True
|
||||
,autoincrement = True
|
||||
,comment ='编号'
|
||||
)
|
||||
student_id = Column(Integer
|
||||
sid = Column(Integer
|
||||
,comment ='学生ID'
|
||||
)
|
||||
class_id = Column(Integer
|
||||
cid = Column(Integer
|
||||
,comment ='班级ID'
|
||||
)
|
||||
score_num = Column(Integer
|
||||
num = Column(Integer
|
||||
,comment ='考试序次'
|
||||
)
|
||||
score = Column(Integer,nullable = False)
|
||||
subject = Column(String(100)
|
||||
tsub = Column(String(100)
|
||||
,nullable = False
|
||||
)
|
||||
create_date = Column(DateTime
|
||||
|
||||
@@ -1,20 +1,20 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ScoreRequest(BaseModel):
|
||||
score_id:int
|
||||
student_id:int
|
||||
class_id:int
|
||||
score_num:int
|
||||
# id:int
|
||||
cid:int
|
||||
sid:int
|
||||
num:int
|
||||
score:int
|
||||
subject:str
|
||||
tsub:str
|
||||
|
||||
class ScoreQuery(BaseModel):
|
||||
score_id:int|None = None
|
||||
student_id:int|None = None
|
||||
class_id:int|None = None
|
||||
score_num:int|None = None
|
||||
id:int|None = None
|
||||
sid:int|None = None
|
||||
cid:int|None = None
|
||||
num:int|None = None
|
||||
score:int|None = None
|
||||
subject:str|None = None
|
||||
tsub:str|None = None
|
||||
|
||||
class ScoreResponse(BaseModel):
|
||||
code:int = 200
|
||||
|
||||
Reference in New Issue
Block a user