Merge remote-tracking branch 'origin/xiaoyang_score' into lon_students_merge_code_test
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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,ScoreQuery
|
||||
|
||||
score_api = APIRouter()
|
||||
|
||||
@score_api.get('/Score')
|
||||
def get_scores(score:ScoreQuery=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=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='没有')
|
||||
|
||||
@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)
|
||||
if not aa:
|
||||
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)
|
||||
if not aaa:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
return {'message':'更新成功','detail':aaa}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_scores_dao(s,db):
|
||||
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 [{'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.sid==b['sid']
|
||||
,Score.tsub==b['tsub']).all()
|
||||
if aa:
|
||||
raise ValueError
|
||||
else:
|
||||
z = Score(**b)
|
||||
db.add(z)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
print(e)
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def update_scores_dao(sid:int,cid:int,num:int,tsub:str,update_data:dict,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.sid == sid
|
||||
,Score.cid == cid
|
||||
,Score.num == num
|
||||
,Score.tsub == tsub ).update(update_data)
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return aa
|
||||
|
||||
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).delete()
|
||||
except:
|
||||
db.rollback()
|
||||
aaa = 0
|
||||
else:
|
||||
db.commit()
|
||||
finally:
|
||||
return aaa
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
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'
|
||||
engine = create_engine(db_url)
|
||||
|
||||
Base = declarative_base()
|
||||
Session = sessionmaker(bind=engine, autoflush = False, autocommit = False)
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1,12 @@
|
||||
from fastapi import FastAPI
|
||||
from scores.api.score_api import score_api
|
||||
from scores.database import *
|
||||
app = FastAPI()
|
||||
|
||||
app.include_router(score_api)
|
||||
|
||||
Base.metadata.create_all(engine)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run('main:app',host='127.0.0.1',port=12346)
|
||||
@@ -0,0 +1,37 @@
|
||||
from scores.database import Base
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float
|
||||
class Score(Base):
|
||||
__tablename__='scores'
|
||||
id = Column(Integer
|
||||
,primary_key = True
|
||||
,autoincrement = True
|
||||
,comment ='编号'
|
||||
)
|
||||
sid = Column(Integer
|
||||
,comment ='学生ID'
|
||||
)
|
||||
cid = Column(Integer
|
||||
,comment ='班级ID'
|
||||
)
|
||||
num = Column(Integer
|
||||
,comment ='考试序次'
|
||||
)
|
||||
score = Column(Integer,nullable = False)
|
||||
tsub = Column(String(100)
|
||||
,nullable = False
|
||||
)
|
||||
create_date = Column(DateTime
|
||||
,default = datetime.now
|
||||
)
|
||||
update_date = Column(DateTime
|
||||
,default = datetime.now
|
||||
)
|
||||
is_deleted = Column(Integer
|
||||
,default = 0
|
||||
,comment='是否删除: 0-正常,1-已删除'
|
||||
)
|
||||
deleted_date=Column(DateTime
|
||||
,default=None
|
||||
,comment='删除时间'
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ScoreRequest(BaseModel):
|
||||
# id:int
|
||||
sid:int
|
||||
cid:int
|
||||
num:int
|
||||
score:int
|
||||
tsub:str
|
||||
|
||||
class ScoreQuery(BaseModel):
|
||||
id:int|None = None
|
||||
sid:int|None = None
|
||||
cid:int|None = None
|
||||
num:int|None = None
|
||||
score:int|None = None
|
||||
tsub:str|None = None
|
||||
|
||||
class ScoreResponse(BaseModel):
|
||||
code:int = 200
|
||||
detail:str = 'OK'
|
||||
totals:int = 0
|
||||
data:str|dict|tuple|list
|
||||
Reference in New Issue
Block a user