1
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from fastapi import APIRouter,Depends,HTTPException
|
||||
from fastapi import APIRouter,HTTPException
|
||||
from dao.student_dao import *
|
||||
from schema.student_schema import *
|
||||
from util.database import get_db
|
||||
@@ -33,10 +33,10 @@ def update_students(s:StudentRequest
|
||||
,db=Depends(get_db)):
|
||||
d = s.model_dump(exclude_unset=True)
|
||||
if not d:
|
||||
raise HTTPException(status_code=400, detail='更新内容不能为空!')
|
||||
raise HTTPException(status_code=400, detail='更新内容不能为空')
|
||||
r = update_student_dao( stu_id=stu_id,update_data=d,db=db )
|
||||
if not r:
|
||||
raise HTTPException(status_code=500, detail='没有更新!')
|
||||
raise HTTPException(status_code=500, detail='没有更新')
|
||||
return {'code':200,'totals':r,'detail':'更新成功'}
|
||||
|
||||
@StudentAPI.delete('/{stu_id}',tags=['学⽣基本信息管理模块'],summary='学生信息删除接口',description='删除学生信息')
|
||||
@@ -50,8 +50,12 @@ def del_students(stu_id:int
|
||||
@StudentAPI.post('/students',tags=['学⽣基本信息管理模块'],response_model=StugetResponse,summary='学生信息新增接口',description='新增学生信息')
|
||||
def add_students(s:StudentRequest
|
||||
,db=Depends(get_db)):
|
||||
d=s.model_dump(exclude_unset=True)
|
||||
stu_new=add_student_dao(o=d,db=db)
|
||||
if not stu_new:
|
||||
raise HTTPException(status_code=404,detail='添加失败')
|
||||
return stu_new
|
||||
d = s.model_dump(exclude_unset=True)
|
||||
if not d:
|
||||
raise HTTPException(status_code=400, detail='添加内容不能为空')
|
||||
r = add_student_dao( o=d, db=db)
|
||||
if r == 'conflict':
|
||||
raise HTTPException(status_code=409, detail='身份证号已存在,请勿重复添加')
|
||||
if r == 'error':
|
||||
raise HTTPException(status_code=500, detail='更添加失败,请稍后重试')
|
||||
return r
|
||||
|
||||
@@ -1,19 +1,28 @@
|
||||
from fastapi import Depends
|
||||
from util.database import get_db
|
||||
from model.all_model import Student_Model
|
||||
from typing import List, Optional, Dict, Any
|
||||
from datetime import datetime
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
def add_student_dao(o,db):
|
||||
if o.get('id_card'):
|
||||
conflict = (db.query(Student_Model)
|
||||
.filter(Student_Model.id_card == o['id_card'],
|
||||
Student_Model.delete_status == 0)
|
||||
.first())
|
||||
if conflict:
|
||||
return 'conflict'
|
||||
try:
|
||||
o1 = Student_Model( **o)
|
||||
db.add(o1)
|
||||
o2 = Student_Model(**o)
|
||||
db.add(o2)
|
||||
db.commit()
|
||||
stu_id=o1.stu_id
|
||||
return db.query(Student_Model).filter(Student_Model.stu_id == stu_id).first()
|
||||
except:
|
||||
db.refresh(o2) # 回填自增的 stu_id
|
||||
return o2
|
||||
except IntegrityError:
|
||||
db.rollback()
|
||||
return None
|
||||
return 'conflict'
|
||||
except Exception:
|
||||
db.rollback()
|
||||
return 'error'
|
||||
|
||||
def delete_student_dao(stu_id,db):
|
||||
try:
|
||||
@@ -28,14 +37,14 @@ def delete_student_dao(stu_id,db):
|
||||
|
||||
def update_student_dao(stu_id,update_data,db):
|
||||
try:
|
||||
rows = (db.query( Student_Model )
|
||||
.filter( Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
|
||||
.update( update_data ))
|
||||
db.commit()
|
||||
rows=(db.query(Student_Model)
|
||||
.fiter(Student_Model.stu_id == stu_id,Student_Model.delete_status == 0)
|
||||
.update(update_data))
|
||||
except:
|
||||
db.rollback()
|
||||
return False
|
||||
else:
|
||||
db.commit()
|
||||
return rows
|
||||
|
||||
def get_student_dao(stu_id:Optional[int]
|
||||
|
||||
@@ -23,6 +23,8 @@ class Student_Model(Base):
|
||||
|
||||
native_place=Column(String(200),comment='籍贯')
|
||||
|
||||
id_card=Column(String(18),unique=True,comment='身份证号')
|
||||
|
||||
birthday=Column(DATE,comment='生日')
|
||||
|
||||
school=Column(String(100),comment='毕业学校')
|
||||
|
||||
@@ -9,6 +9,7 @@ class StudentRequest(BaseModel):
|
||||
stu_name:str
|
||||
age:int |None = None
|
||||
gender:str |None = None
|
||||
id_card:str
|
||||
native_place:str |None = None
|
||||
birthday:date |None = None
|
||||
school:str |None = None
|
||||
@@ -57,6 +58,7 @@ class StugetResponse(BaseModel):
|
||||
stu_name: str | None = None
|
||||
age: int | None = None
|
||||
gender: str | None = None
|
||||
id_card: str
|
||||
native_place: str | None = None
|
||||
birthday: date | None = None
|
||||
school: str | None = None
|
||||
@@ -81,4 +83,6 @@ class StudentPageResponse(BaseModel):
|
||||
page: int
|
||||
page_size: int
|
||||
totals: int
|
||||
data: List[StugetResponse]
|
||||
data: List[StugetResponse]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user