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