fix(chat): 修复新建聊天删除失败 调整更新sql

This commit is contained in:
xingc
2025-05-14 18:37:51 +08:00
parent ee78186508
commit 854f5bcbea

View File

@@ -10,9 +10,9 @@ from json.decoder import JSONDecodeError
from fastapi import APIRouter, Query, Depends, WebSocket, WebSocketDisconnect, HTTPException, status
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.orm import selectinload, aliased
from sqlalchemy.orm import selectinload
from core.base import schema as base_schema
from core.db.crud import CRUD
@@ -208,11 +208,23 @@ async def delete_single_chat(
login_user: LoginUser = Depends(get_current_user_form_http),
db_session: AsyncSession = Depends(get_async_session),
):
chat = await CRUD(db_session, Chat).update(
data={'is_deleted': True},
filters={'chat_guid': chat_guid, 'user_id': login_user.user_id}
subquery = (
select(chat_participant.c.chat_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='聊天不存在')
return {'message': '聊天已删除'}