修更新的bud

This commit is contained in:
2026-09-23 00:50:59 +08:00
parent 079a7cf351
commit b67e3cd547
3 changed files with 26 additions and 15 deletions
+6 -2
View File
@@ -29,11 +29,15 @@ def delete_tea(id:int|None,db=Depends(get_db)):
return {'detail':'删除成功!'} return {'detail':'删除成功!'}
@tea_api.put('/tea/{id}', summary='更新老师信息', description='输入更改的信息,可以为空') @tea_api.put('/tea/{id}', summary='更新老师信息', description='输入更改的信息,可以为空')
def update_tea(id:int,tea:NewTeaRequest,db=Depends(get_db)): def update_tea(id: int, tea: NewTeaRequest = Depends(), db=Depends(get_db)):
t = tea.model_dump(exclude_unset=True) t = tea.model_dump(exclude_unset=True)
r = update_tea_dao(id=id, t=t, db=db) r = update_tea_dao(id=id, t=t, db=db)
if not r: if r == 'NOT_FOUND':
raise HTTPException(status_code=404, detail='老师不存在') raise HTTPException(status_code=404, detail='老师不存在')
if r == 'NO_CHANGE':
raise HTTPException(status_code=400, detail='没有要更新的内容')
if isinstance(r, str) and r.startswith('ERROR'):
raise HTTPException(status_code=400, detail=r)
return {'detail': '更新成功'} return {'detail': '更新成功'}
#根据任意信息删除 #根据任意信息删除
+13 -6
View File
@@ -31,13 +31,20 @@ def add_tea_dao(t,db):
#更新 #更新
def update_tea_dao(id, t, db): def update_tea_dao(id, t, db):
try: try:
row = db.query(Tea).filter(Tea.id == id,Tea.is_delete ==False ).update(t) if not t:
except: return 'NO_CHANGE'
db.rollback() obj = db.query(Tea).filter(Tea.id == id, Tea.is_delete == False).first()
return False if not obj:
else: return 'NOT_FOUND'
for k, v in t.items():
if hasattr(obj, k):
setattr(obj, k, v)
db.commit() db.commit()
return row return 'OK'
except Exception as e:
db.rollback()
print('更新失败原因', repr(e))
return f'ERROR: {e}'
#查询 #查询
def get_tea_dao(t,db): def get_tea_dao(t,db):
+1 -1
View File
@@ -15,7 +15,7 @@ if __name__ == "__main__":
Base.metadata.create_all(engine1) Base.metadata.create_all(engine1)
import uvicorn import uvicorn
uvicorn.run('main:app', host='0.0.0.0', port=57000) uvicorn.run('main:app', host='0.0.0.0', port=56000)
""" """
IPv4 地址 . . . . . . . . . . . . : 192.168.1.51 IPv4 地址 . . . . . . . . . . . . : 192.168.1.51
子网掩码 . . . . . . . . . . . . : 255.255.255.0 子网掩码 . . . . . . . . . . . . : 255.255.255.0