feat: 增加了用户音频的增删查改
This commit is contained in:
1 parent
4322b03418
commit
4a256fa506
6 files changed
+132
-184
No files matched your search
@@ -106,7 +106,6 @@ async def create_chat_handler(chat: ChatCreateRequest, db, redis):
|
||||
try:
|
||||
db.add(new_chat)
|
||||
db.commit()
|
||||
db.refresh(new_chat)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
@@ -154,7 +153,6 @@ async def create_chat_handler(chat: ChatCreateRequest, db, redis):
|
||||
# 将Session记录存入
|
||||
db.add(new_session)
|
||||
db.commit()
|
||||
db.refresh(new_session)
|
||||
redis.set(session_id, json.dumps(content, ensure_ascii=False))
|
||||
|
||||
chat_create_data = ChatCreateData(user_character_id=new_chat.id, session_id=session_id, createdAt=datetime.now().isoformat())
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from ..schemas.user_schema import *
|
||||
from ..dependencies.logger import get_logger
|
||||
from ..models import User, Hardware
|
||||
from ..models import User, Hardware, Audio
|
||||
from datetime import datetime
|
||||
from sqlalchemy.orm import Session
|
||||
from fastapi import HTTPException, status
|
||||
@@ -36,7 +36,6 @@ async def update_user_handler(user_id:int, user:UserUpdateRequest, db: Session):
|
||||
existing_user.persona = user.persona
|
||||
try:
|
||||
db.commit()
|
||||
db.refresh(existing_user)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
@@ -117,7 +116,6 @@ async def change_bind_hardware_handler(hardware_id, user, db):
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
||||
existing_hardware.user_id = user.user_id
|
||||
db.commit()
|
||||
db.refresh(existing_hardware)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
@@ -135,7 +133,6 @@ async def update_hardware_handler(hardware_id, hardware, db):
|
||||
existing_hardware.firmware = hardware.firmware
|
||||
existing_hardware.model = hardware.model
|
||||
db.commit()
|
||||
db.refresh(existing_hardware)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
@@ -153,4 +150,67 @@ async def get_hardware_handler(hardware_id, db):
|
||||
if existing_hardware is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
||||
hardware_query_data = HardwareQueryData(mac=existing_hardware.mac, user_id=existing_hardware.user_id, firmware=existing_hardware.firmware, model=existing_hardware.model)
|
||||
return HardwareQueryResponse(status="success", message="查询硬件信息成功", data=hardware_query_data)
|
||||
return HardwareQueryResponse(status="success", message="查询硬件信息成功", data=hardware_query_data)
|
||||
|
||||
|
||||
#用户上传音频
|
||||
async def upload_audio_handler(user_id, audio, db):
|
||||
try:
|
||||
audio_data = audio.file.read()
|
||||
new_audio = Audio(user_id=user_id, audio_data=audio_data)
|
||||
db.add(new_audio)
|
||||
db.commit()
|
||||
db.refresh(new_audio)
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
audio_upload_data = AudioUploadData(audio_id=new_audio.id, uploadedAt=datetime.now().isoformat())
|
||||
return AudioUploadResponse(status="success", message="用户上传音频成功", data=audio_upload_data)
|
||||
|
||||
|
||||
#用户更新音频
|
||||
async def update_audio_handler(audio_id, audio_file, db):
|
||||
try:
|
||||
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
||||
if existing_audio is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
||||
audio_data = audio_file.file.read()
|
||||
existing_audio.audio_data = audio_data
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
audio_update_data = AudioUpdateData(updatedAt=datetime.now().isoformat())
|
||||
return AudioUpdateResponse(status="success", message="用户更新音频成功", data=audio_update_data)
|
||||
|
||||
|
||||
#用户查询音频
|
||||
async def download_audio_handler(audio_id, db):
|
||||
try:
|
||||
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
if existing_audio is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
||||
audio_data = existing_audio.audio_data
|
||||
return audio_data
|
||||
|
||||
|
||||
#用户删除音频
|
||||
async def delete_audio_handler(audio_id, db):
|
||||
try:
|
||||
existing_audio = db.query(Audio).filter(Audio.id == audio_id).first()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
if existing_audio is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="音频不存在")
|
||||
try:
|
||||
db.delete(existing_audio)
|
||||
db.commit()
|
||||
except Exception as e:
|
||||
db.rollback()
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
||||
audio_delete_data = AudioDeleteData(deletedAt=datetime.now().isoformat())
|
||||
return AudioDeleteResponse(status="success", message="用户删除音频成功", data=audio_delete_data)
|
||||
Reference in new issue
Block a user