test三轮完成的版本,并入了国伟的班级模块
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from dao.wl_score_dao import get_score, create_score, update_score, delete_score, list_score_by_stu_id
|
||||
from database import get_db
|
||||
|
||||
router = APIRouter(tags=["成绩CRUD"])
|
||||
|
||||
# 新增成绩
|
||||
@router.post("/score/add")
|
||||
def api_add_score(stu_id: int, exam_order: int, score: float, db: Session = Depends(get_db)):
|
||||
res = create_score(db, stu_id, exam_order, score)
|
||||
if res is None:
|
||||
raise HTTPException(status_code=400, detail="新增失败:学生不存在或成绩重复")
|
||||
return res
|
||||
|
||||
# 查询单条成绩
|
||||
@router.get("/score/one")
|
||||
def api_get_score(stu_id: int, exam_order: int, db: Session = Depends(get_db)):
|
||||
s = get_score(db, stu_id, exam_order)
|
||||
if not s:
|
||||
raise HTTPException(status_code=404, detail="成绩记录不存在")
|
||||
return s
|
||||
|
||||
# 查询某个学生全部成绩
|
||||
@router.get("/score/list/{stu_id}")
|
||||
def api_list_score(stu_id: int, db: Session = Depends(get_db)):
|
||||
return list_score_by_stu_id(db, stu_id)
|
||||
|
||||
# 更新成绩
|
||||
@router.put("/score/update")
|
||||
def api_update_score(stu_id: int, exam_order: int, new_score: float, db: Session = Depends(get_db)):
|
||||
obj = update_score(db, stu_id, exam_order, new_score)
|
||||
if obj is None:
|
||||
raise HTTPException(status_code=404, detail="更新失败,记录不存在")
|
||||
return obj
|
||||
|
||||
# 删除成绩
|
||||
@router.delete("/score/del")
|
||||
def api_del_score(stu_id: int, exam_order: int, db: Session = Depends(get_db)):
|
||||
ok = delete_score(db, stu_id, exam_order)
|
||||
if not ok:
|
||||
raise HTTPException(status_code=404, detail="删除失败,记录不存在")
|
||||
return {"msg": "删除成功"}
|
||||
@@ -0,0 +1,105 @@
|
||||
from sqlalchemy.orm import Session
|
||||
from sqlalchemy import select, update, delete
|
||||
from model.wl_score_model import Score
|
||||
from model.wl_student_model import Student
|
||||
|
||||
|
||||
def get_score(db: Session, stu_id: int, exam_order: int):
|
||||
"""根据学生id+考试批次查询单条成绩"""
|
||||
stmt = select(Score).where(
|
||||
Score.stu_id == stu_id,
|
||||
Score.exam_order == exam_order
|
||||
)
|
||||
return db.scalar(stmt)
|
||||
|
||||
|
||||
def get_student_by_id(db: Session, stu_id: int):
|
||||
"""检测学生是否存在:通过学生id查学生表"""
|
||||
return db.scalar(select(Student).where(Student.stu_id == stu_id))
|
||||
|
||||
|
||||
def get_student_by_name(db: Session, stu_name: str):
|
||||
"""通过学生姓名查询学生"""
|
||||
return db.scalar(select(Student).where(Student.stu_name == stu_name))
|
||||
|
||||
|
||||
def list_score_by_stu_id(db: Session, stu_id: int):
|
||||
"""查询一个学生全部成绩,返回列表"""
|
||||
stmt = select(Score).where(Score.stu_id == stu_id)
|
||||
return db.scalars(stmt).all()
|
||||
|
||||
|
||||
|
||||
def create_score(db: Session, stu_id: int, exam_order: int, score: float):
|
||||
"""
|
||||
新增成绩
|
||||
返回:成功返回StudentScore对象;检测不通过返回None
|
||||
"""
|
||||
# 1.检测:学生是否真实存在学生表
|
||||
stu = get_student_by_id(db, stu_id)
|
||||
if not stu:
|
||||
print("[检测] 学生表无此学生,stu_id=", stu_id)
|
||||
return None
|
||||
|
||||
# 2.检测:该学生该批次成绩是否已经存在(重复新增)
|
||||
exist_score = get_score(db, stu_id, exam_order)
|
||||
if exist_score:
|
||||
print("[检测] 成绩已存在 stu_id=", stu_id, "exam_order=", exam_order)
|
||||
return None
|
||||
|
||||
# 3.构建ORM对象插入数据库
|
||||
new_score = Score(
|
||||
stu_id=stu_id,
|
||||
exam_order=exam_order,
|
||||
score=score
|
||||
)
|
||||
db.add(new_score)
|
||||
db.commit()
|
||||
db.refresh(new_score)
|
||||
return new_score
|
||||
|
||||
|
||||
|
||||
def update_score(db: Session, stu_id: int, exam_order: int, new_score: float):
|
||||
"""
|
||||
更新指定学生指定考试批次的分数
|
||||
"""
|
||||
# 1.检测学生是否存在
|
||||
stu = get_student_by_id(db, stu_id)
|
||||
if not stu:
|
||||
print("[检测] 更新失败:学生不存在")
|
||||
return None
|
||||
|
||||
# 2.检测这条成绩记录本身是否存在
|
||||
score_obj = get_score(db, stu_id, exam_order)
|
||||
if not score_obj:
|
||||
print("[检测] 更新失败:该学生该批次成绩记录不存在")
|
||||
return None
|
||||
|
||||
# 方案A:直接修改对象属性(推荐)
|
||||
score_obj.score = new_score
|
||||
db.commit()
|
||||
db.refresh(score_obj)
|
||||
return score_obj
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def delete_score(db: Session, stu_id: int, exam_order: int):
|
||||
"""删除某个学生某一次考核成绩"""
|
||||
# 检测记录是否存在
|
||||
score_obj = get_score(db, stu_id, exam_order)
|
||||
if not score_obj:
|
||||
print("[检测] 删除失败,记录不存在")
|
||||
return False
|
||||
|
||||
stmt = delete(Score).where(
|
||||
Score.stu_id == stu_id,
|
||||
Score.exam_order == exam_order
|
||||
)
|
||||
db.execute(stmt)
|
||||
db.commit()
|
||||
return True
|
||||
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
#项目初始化入口
|
||||
from fastapi import FastAPI
|
||||
from api import wl_student_api,wl_class_api # 周学灵、熊浩钦的路由
|
||||
from api import wl_student_api,wl_class_api,wl_score_api # 周学灵、熊浩钦、圣国伟的路由
|
||||
from database import Base, engine
|
||||
from model import wl_student_model as student_model
|
||||
from model import wl_class_model,wl_advisor_model
|
||||
from model import wl_class_model,wl_advisor_model,wl_score_model
|
||||
|
||||
|
||||
Base.metadata.create_all(bind=engine)
|
||||
|
||||
app = FastAPI(title="沃林学生管理系统")
|
||||
app.include_router(wl_student_api.router)
|
||||
app.include_router(wl_class_api.router)
|
||||
app.include_router(wl_score_api.router)
|
||||
|
||||
if __name__ == "__main__":
|
||||
import uvicorn
|
||||
|
||||
@@ -2,11 +2,10 @@ from sqlalchemy import Column, Integer, Float
|
||||
from database import Base
|
||||
|
||||
class Score(Base):
|
||||
__tablename__ = "student_score"
|
||||
__tablename__ = "wl_score"
|
||||
|
||||
stu_id = Column(Integer, primary_key=True, comment="学生编号")
|
||||
exam_order = Column(Integer, primary_key=True, comment="考核序次")
|
||||
score = Column(Float, nullable=False, comment="成绩")
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
from pydantic import BaseModel
|
||||
from datetime import date
|
||||
from typing import Optional, List
|
||||
|
||||
# ----------------学生----------------
|
||||
class StudentCreate(BaseModel):
|
||||
stu_name: str
|
||||
native_place: Optional[str] = None
|
||||
graduate_school: Optional[str] = None
|
||||
major: Optional[str] = None
|
||||
in_time: Optional[date] = None
|
||||
out_time: Optional[date] = None
|
||||
edu: Optional[str] = None
|
||||
advisor_id: Optional[int] = None
|
||||
stu_age: Optional[int] = None
|
||||
stu_gender: Optional[str] = None
|
||||
|
||||
class StudentResp(StudentCreate):
|
||||
stu_id: int
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# ----------------成绩----------------
|
||||
class ScoreCreate(BaseModel):
|
||||
stu_id: int
|
||||
exam_order: int
|
||||
score: float
|
||||
|
||||
class ScoreResp(ScoreCreate):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# ----------------就业----------------
|
||||
class EmploymentCreate(BaseModel):
|
||||
stu_id: int
|
||||
emp_open_time: date
|
||||
offer_time: Optional[date] = None
|
||||
company_name: Optional[str] = None
|
||||
salary: Optional[float] = None
|
||||
|
||||
class EmploymentResp(EmploymentCreate):
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# ----------------班级----------------
|
||||
class ClassCreate(BaseModel):
|
||||
start_time: Optional[date] = None
|
||||
|
||||
class ClassResp(ClassCreate):
|
||||
class_id: int
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# ----------------老师----------------
|
||||
class TeacherCreate(BaseModel):
|
||||
t_name: str
|
||||
t_gender: Optional[str] = None
|
||||
t_email: Optional[str] = None
|
||||
class_id: Optional[int] = None
|
||||
role: Optional[str] = None
|
||||
|
||||
class TeacherResp(TeacherCreate):
|
||||
t_id: int
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
# ----------------顾问----------------
|
||||
class AdvisorCreate(BaseModel):
|
||||
t_name: str
|
||||
gender: Optional[str] = None
|
||||
phone: Optional[str] = None
|
||||
|
||||
class AdvisorResp(AdvisorCreate):
|
||||
advisor_id: int
|
||||
class Config:
|
||||
from_attributes = True
|
||||
Reference in New Issue
Block a user