Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1558103dfa | ||
|
|
5c671020d7 | ||
|
|
de85f8ef06 | ||
|
|
1fe2b6bc60 | ||
|
|
8a1022390f | ||
|
|
ed151b55ae | ||
|
|
7cdad9a9fe | ||
|
|
e88306a555 | ||
|
|
6e526d9f6b | ||
|
|
1b9e5b8a57 |
@@ -0,0 +1,46 @@
|
||||
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(tags=['成绩'])
|
||||
|
||||
@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)
|
||||
if aa:
|
||||
return aa
|
||||
raise HTTPException(status_code=404,detail='不存在')
|
||||
|
||||
@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='不可以')
|
||||
|
||||
@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',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:
|
||||
raise HTTPException(status_code=404,detail='记录不存在')
|
||||
return {'message':'更新成功','detail':aaa}
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
from scores.model.score_model import Score
|
||||
from datetime import datetime
|
||||
|
||||
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('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,'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]
|
||||
else:
|
||||
return []
|
||||
|
||||
def add_scores_dao(b,db):
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.sid==b['sid']
|
||||
,Score.t_subject==b['t_subject']).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,t_subject:int,update_data:dict,db):
|
||||
if not update_data:
|
||||
return 0
|
||||
update_data['update_date']=datetime.now()
|
||||
try:
|
||||
aa = db.query(Score).filter(Score.sid == sid
|
||||
,Score.cid == cid
|
||||
,Score.num == num
|
||||
,Score.t_subject == t_subject ).update(update_data)
|
||||
db.commit()
|
||||
return aa
|
||||
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
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
from sqlalchemy import *
|
||||
from sqlalchemy.orm import declarative_base,sessionmaker
|
||||
|
||||
# 导入.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()
|
||||
Session = sessionmaker(bind=engine, autoflush = False, autocommit = False)
|
||||
|
||||
def get_db():
|
||||
db = Session()
|
||||
try:
|
||||
yield db
|
||||
finally:
|
||||
db.close()
|
||||
@@ -0,0 +1,17 @@
|
||||
from fastapi import FastAPI,APIRouter
|
||||
from scores.api.score_api import score_api
|
||||
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)
|
||||
|
||||
if __name__ == '__main__':
|
||||
import uvicorn
|
||||
uvicorn.run('main:app',host='127.0.0.1',port=12346)
|
||||
@@ -0,0 +1,43 @@
|
||||
from scores.database import Base
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime,Column,Integer,String,Float,ForeignKey
|
||||
|
||||
|
||||
|
||||
class Score(Base):
|
||||
__tablename__='scores'
|
||||
id = Column(Integer
|
||||
,primary_key = True
|
||||
,autoincrement = True
|
||||
,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)
|
||||
t_subject = Column(Integer
|
||||
,ForeignKey('subject.id')
|
||||
,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
|
||||
t_subject:int
|
||||
|
||||
class ScoreQuery(BaseModel):
|
||||
id:int|None = None
|
||||
sid:int|None = None
|
||||
cid:int|None = None
|
||||
num:int|None = None
|
||||
score:int|None = None
|
||||
t_subject:int|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