成绩建表
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
|
||||
|
||||
|
||||
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)
|
||||
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)):
|
||||
a = score.model_dump()
|
||||
aa = add_scores_dao(b=a,db=db)
|
||||
if not aa:
|
||||
raise HTTPException(status_code=500,detail='没有')
|
||||
return ScoreResponse(data=a)
|
||||
|
||||
@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)
|
||||
if not aa:
|
||||
raise HTTPException(status_code=500,detail='没有更新')
|
||||
return {'code':200,'totals':a,'detail':'更新成功'}
|
||||
|
||||
@score_api.delete('/Score,{cid}')
|
||||
def get_scores(cid:int,db=Depends(get_db)):
|
||||
aaa = delete_scores_dao(cid,db)
|
||||
if not aaa:
|
||||
raise HTTPException(status_code=500,detail='没有删除')
|
||||
return {'code':200,'totals':aaa,'detail':'更新成功'}
|
||||
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
from scores.model.score_model import Score
|
||||
|
||||
def get_scores_dao(cid,score,subject,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)
|
||||
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]
|
||||
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.cid==b['cid']).all()
|
||||
if aa:
|
||||
z = Score(**b)
|
||||
db.add(z)
|
||||
else:
|
||||
raise ValueError
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
def update_scores_dao(cid,update_date,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.cid ==cid).update(update_date)
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return aa
|
||||
|
||||
def delete_scores_dao(cid,db):
|
||||
try:
|
||||
aaa = db.query(Score).filter(Score.cid ==cid).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/a0824?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,29 @@
|
||||
from scores.database import Base
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float
|
||||
class Score(Base):
|
||||
__tablename__='score'
|
||||
cid = Column(Integer
|
||||
,primary_key = True
|
||||
,autoincrement = True
|
||||
,comment ='考试序次'
|
||||
)
|
||||
score = Column(Float,nullable = False)
|
||||
subject = Column(String(100)
|
||||
,nullable = False
|
||||
)
|
||||
create_date = Column(DateTime
|
||||
,default = datetime.now
|
||||
)
|
||||
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
|
||||
,comment='删除时间'
|
||||
)
|
||||
@@ -0,0 +1,12 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
class ScoreRequest(BaseModel):
|
||||
cid:int
|
||||
cscore:float
|
||||
subject:str
|
||||
|
||||
class ScoreResponse(BaseModel):
|
||||
code:int = 200
|
||||
detail:str = 'OK'
|
||||
totals:int = 0
|
||||
data:str|dict|tuple|list
|
||||
Reference in New Issue
Block a user