diff --git a/api/student.py b/api/student.py index 9299659..ac3edb9 100644 --- a/api/student.py +++ b/api/student.py @@ -1,14 +1,14 @@ from fastapi import APIRouter,Depends,HTTPException -from sqlalchemy.orm import Session from dao.student import * from schemas.student import StudentCreate - +from sqlalchemy.orm import Session student_router = APIRouter() @student_router.post("/student") -def create_student(request:StudentCreate,db:Session,create = Depends(create_students)): +def create_student(request:StudentCreate,db:Session = Depends(get_db)): d = request.model_dump() r = create_students(d,db) if not r: raise HTTPException(status_code=500,detail='服务器繁忙,请稍后添加!') - return {'code':200,'detail':"添加学生成功"} \ No newline at end of file + return {'code':200,'detail':"添加学生成功"} + diff --git a/dao/student.py b/dao/student.py index 9014fe5..49991d7 100644 --- a/dao/student.py +++ b/dao/student.py @@ -1,16 +1,17 @@ from schemas.student import * +from fastapi import Depends from core.database import get_db from models.student import * from utils.code_generator import generate_no +from sqlalchemy.orm import Session - -def create_students(d,db): +def create_students(d,db:Session = Depends(get_db)): stu = Student(**d) try: db.add(stu) - generate_no("student_no", "STU", stu, db) db.commit() - except:return False + except: + return False else: return True diff --git a/database.py b/database.py index ecff48f..7ee23b9 100644 --- a/database.py +++ b/database.py @@ -10,4 +10,4 @@ engine = create_engine(db_url Base.metadata.create_all(engine) db = SessionLocal() -db.commit() \ No newline at end of file +db.commit() #创建全部表 \ No newline at end of file diff --git a/main.py b/main.py index a0cfb07..d8e7658 100644 --- a/main.py +++ b/main.py @@ -1,7 +1,7 @@ from fastapi import FastAPI from api.example import example_router from middleware import log_middleware -from api.statistics import statistics_router +# from api.statistics import statistics_router import uvicorn from api.student import student_router app = FastAPI() @@ -12,14 +12,14 @@ from api.class_api import classes_router app.include_router(classes_router,tags=['班级接口'],prefix="/classes") #-------------李玉杰-------------- -app.include_router(student_router) +app.include_router(student_router,tags=['学生接口']) #-------------张义--------------- #-------------薄鑫--------------- #-------------张昕浩--------------- -app.include_router(example_router) -app.include_router(statistics_router, prefix="/statistics") +# app.include_router(example_router) +# app.include_router(statistics_router, prefix="/statistics") #-------------曾凯--------------- diff --git a/schemas/student.py b/schemas/student.py index d4a044a..64c42bb 100644 --- a/schemas/student.py +++ b/schemas/student.py @@ -7,7 +7,7 @@ class StudentCreate(BaseModel): class_id: int consultant_id: int | None = None native_place: str | None = None - graduate_school: str | None = None + graduation_school: str | None = None major: str | None = None enrollment_date: date graduation_date: date | None = None diff --git a/utils/code_generator.py b/utils/code_generator.py index f4b48ce..3320511 100644 --- a/utils/code_generator.py +++ b/utils/code_generator.py @@ -33,7 +33,7 @@ def add_student( ''' - +from sqlalchemy.orm import Session def generate_no(field_name: str, prefix: str, db_object, db: Session): if getattr(db_object, field_name): return