Compare commits

...
Author SHA1 Message Date
geyuan f697a8b634 加更改时提交空值不覆盖 2026-09-23 14:05:49 +08:00
geyuan 8b26d3b239 添加一些注释 2026-09-23 12:23:41 +08:00
2 changed files with 6 additions and 1 deletions
+3
View File
@@ -15,6 +15,7 @@ def add_tea(tea: NewTeaRequest=Depends(),db=Depends(get_db)):
if not r:
raise HTTPException(status_code=500,detail='添加失败!')
return TeaResponse(totals=1,data=tea.model_dump())
@tea_api.get('/tea',response_model=TeaResponse,summary='查询老师',description='任意选择输入的信息数量,不输入则查询全部老师')
def get_tea(t:TeaRequest=Depends(),db=Depends(get_db)): #开启会话窗口
r = get_tea_dao(t.model_dump(),db=db)
@@ -31,6 +32,7 @@ def delete_tea(id:int|None,db=Depends(get_db)):
@tea_api.put('/tea/{id}', summary='更新老师信息', description='输入更改的信息,可以为空')
def update_tea(id: int, tea: NewTeaRequest = Depends(), db=Depends(get_db)):
t = tea.model_dump(exclude_unset=True)
print(t)
r = update_tea_dao(id=id, t=t, db=db)
if r == 'NOT_FOUND':
raise HTTPException(status_code=404, detail='老师不存在')
@@ -38,6 +40,7 @@ def update_tea(id: int, tea: NewTeaRequest = Depends(), db=Depends(get_db)):
raise HTTPException(status_code=400, detail='没有要更新的内容')
if isinstance(r, str) and r.startswith('ERROR'):
raise HTTPException(status_code=400, detail=r)
#先确认是字符串再调用字符串方法判断是不是以错误开头,是就报错给前端
return {'detail': '更新成功'}
#根据任意信息删除
+3 -1
View File
@@ -14,7 +14,9 @@ def add_tea_dao(t,db):
if existing:
for key,value in t.items():
if hasattr(existing,key) and value is not None and value != '':
#hasattr(existing,key)统计有没有key,返回值是True或False
setattr(existing,key,value)
#将value的值赋值到existing的key中,配合循环,让所有输入的信息都覆盖原信息
existing.is_delete = False
db.commit()
return '恢复修改成功'
@@ -37,7 +39,7 @@ def update_tea_dao(id, t, db):
if not obj:
return 'NOT_FOUND'
for k, v in t.items():
if hasattr(obj, k):
if hasattr(obj, k) and v is not None and v != '':
setattr(obj, k, v)
db.commit()
return 'OK'