上传文件至「api」
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy.orm import Session
|
||||
from database import get_db
|
||||
from dao.stu_dao import create_student, get_student_by_id, get_student_list, update_student, delete_student_logic
|
||||
from schema.stu_schema import (
|
||||
StudentCreateRequest,
|
||||
StudentUpdateRequest,
|
||||
StudentQuery,
|
||||
StudentResponse,
|
||||
StudentPageResponse
|
||||
)
|
||||
|
||||
# 创建路由对象 API Router1
|
||||
stu_router = APIRouter()
|
||||
@stu_router.post("", response_model=StudentResponse, summary="新增学生")
|
||||
def add_student(
|
||||
student_req: StudentCreateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
db_stu = create_student(db, student_req)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="新增学生失败,数据库异常")
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
|
||||
@stu_router.get("", response_model=StudentPageResponse, summary="根据学号姓名班级查询")
|
||||
def get_student_page(
|
||||
query: StudentQuery = Depends(),
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
total, db_stu_list = get_student_list(
|
||||
db,
|
||||
stu_id=query.stu_id,
|
||||
stu_name=query.stu_name,
|
||||
class_id=query.class_id,
|
||||
page=query.page,
|
||||
page_size=query.page_size
|
||||
)
|
||||
if total == 0:
|
||||
raise HTTPException(status_code=404, detail="没有找到符合条件的学生")
|
||||
|
||||
item_list = [StudentResponse.model_validate(s) for s in db_stu_list]
|
||||
|
||||
return StudentPageResponse(
|
||||
total=total,
|
||||
page=query.page,
|
||||
page_size=query.page_size,
|
||||
items=item_list
|
||||
)
|
||||
|
||||
|
||||
@stu_router.get("/{stu_id}", response_model=StudentResponse, summary="根据学号查询")
|
||||
def get_one_student(
|
||||
stu_id: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
db_stu = get_student_by_id(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
@stu_router.put("/{stu_id}", response_model=StudentResponse, summary="修改学生信息")
|
||||
def edit_student(
|
||||
stu_id: str,
|
||||
update_req: StudentUpdateRequest,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
exist = get_student_by_id(db, stu_id)
|
||||
if exist is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
db_stu = update_student(db, stu_id, update_req)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="修改学生失败,数据库异常")
|
||||
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
|
||||
|
||||
@stu_router.delete("/{stu_id}", response_model=StudentResponse, summary="删除学生信息")
|
||||
def remove_student(
|
||||
stu_id: str,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
exist = get_student_by_id(db, stu_id)
|
||||
if exist is None:
|
||||
raise HTTPException(status_code=404, detail="该学生不存在")
|
||||
|
||||
db_stu = delete_student_logic(db, stu_id)
|
||||
if db_stu is None:
|
||||
raise HTTPException(status_code=500, detail="删除学生失败,数据库异常")
|
||||
|
||||
return StudentResponse.model_validate(db_stu)
|
||||
Reference in New Issue
Block a user