fix(chat): 修复新建聊天删除失败 调整更新sql
This commit is contained in:
@@ -10,9 +10,9 @@ from json.decoder import JSONDecodeError
|
|||||||
|
|
||||||
from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status
|
from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status
|
||||||
from fastapi_limiter.depends import WebSocketRateLimiter
|
from fastapi_limiter.depends import WebSocketRateLimiter
|
||||||
from sqlalchemy import select, func, and_, exists, distinct
|
from sqlalchemy import select, func, and_, exists, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
from sqlalchemy.ext.asyncio import AsyncSession
|
||||||
from sqlalchemy.orm import selectinload, aliased
|
from sqlalchemy.orm import selectinload
|
||||||
|
|
||||||
from core.base import schema as base_schema
|
from core.base import schema as base_schema
|
||||||
from core.db.crud import CRUD
|
from core.db.crud import CRUD
|
||||||
@@ -208,11 +208,23 @@ async def delete_single_chat(
|
|||||||
login_user: LoginUser = Depends(get_current_user_form_http),
|
login_user: LoginUser = Depends(get_current_user_form_http),
|
||||||
db_session: AsyncSession = Depends(get_async_session),
|
db_session: AsyncSession = Depends(get_async_session),
|
||||||
):
|
):
|
||||||
chat = await CRUD(db_session, Chat).update(
|
subquery = (
|
||||||
data={'is_deleted': True},
|
select(chat_participant.c.chat_id)
|
||||||
filters={'chat_guid': chat_guid, 'user_id': login_user.user_id}
|
.where(chat_participant.c.user_id == login_user.user_id)
|
||||||
|
).scalar_subquery()
|
||||||
|
|
||||||
|
query = (
|
||||||
|
update(Chat)
|
||||||
|
.where(and_(
|
||||||
|
Chat.id.in_(subquery),
|
||||||
|
Chat.guid == chat_guid
|
||||||
|
))
|
||||||
|
.values(is_deleted=True)
|
||||||
)
|
)
|
||||||
if chat is None:
|
result = await db_session.execute(query)
|
||||||
|
await db_session.commit()
|
||||||
|
|
||||||
|
if result.rowcount == 0:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='聊天不存在')
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='聊天不存在')
|
||||||
|
|
||||||
return {'message': '聊天已删除'}
|
return {'message': '聊天已删除'}
|
||||||
|
|||||||
Reference in New Issue
Block a user