2024-05-06 11:39:45 +08:00
|
|
|
from ..schemas.user_schema import *
|
2024-05-01 17:18:30 +08:00
|
|
|
from ..dependencies.logger import get_logger
|
|
|
|
from ..models import User, Hardware
|
|
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
from fastapi import HTTPException, status
|
|
|
|
|
|
|
|
|
|
|
|
#依赖注入获取logger
|
|
|
|
logger = get_logger()
|
|
|
|
|
|
|
|
|
|
|
|
#创建用户
|
|
|
|
async def create_user_handler(user:UserCrateRequest, db: Session):
|
|
|
|
new_user = User(created_at=datetime.now(), open_id=user.open_id, username=user.username, avatar_id=user.avatar_id, tags=user.tags, persona=user.persona)
|
|
|
|
try:
|
|
|
|
db.add(new_user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_user)
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
user_create_data = UserCrateData(user_id=new_user.id, createdAt=new_user.created_at.isoformat())
|
|
|
|
return UserCrateResponse(status="success", message="创建用户成功", data=user_create_data)
|
|
|
|
|
|
|
|
|
|
|
|
#更新用户信息
|
|
|
|
async def update_user_handler(user_id:int, user:UserUpdateRequest, db: Session):
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
existing_user.open_id = user.open_id
|
|
|
|
existing_user.username = user.username
|
|
|
|
existing_user.avatar_id = user.avatar_id
|
|
|
|
existing_user.tags = user.tags
|
|
|
|
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))
|
|
|
|
user_update_data = UserUpdateData(updatedAt=datetime.now().isoformat())
|
|
|
|
return UserUpdateResponse(status="success", message="更新用户信息成功", data=user_update_data)
|
|
|
|
|
|
|
|
|
|
|
|
#查询用户信息
|
|
|
|
async def get_user_handler(user_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
user_query_data = UserQueryData(open_id=existing_user.open_id, username=existing_user.username, avatar_id=existing_user.avatar_id, tags=existing_user.tags, persona=existing_user.persona)
|
|
|
|
return UserQueryResponse(status="success", message="查询用户信息成功", data=user_query_data)
|
|
|
|
|
|
|
|
|
|
|
|
#删除用户
|
|
|
|
async def delete_user_handler(user_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_user = db.query(User).filter(User.id == user_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_user is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="用户不存在")
|
|
|
|
try:
|
|
|
|
db.delete(existing_user)
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
user_delete_data = UserDeleteData(deletedAt=datetime.now().isoformat())
|
|
|
|
return UserDeleteResponse(status="success", message="删除用户成功", data=user_delete_data)
|
|
|
|
|
|
|
|
|
|
|
|
#绑定硬件
|
|
|
|
async def bind_hardware_handler(hardware, db: Session):
|
|
|
|
new_hardware = Hardware(mac=hardware.mac, user_id=hardware.user_id, firmware=hardware.firmware, model=hardware.model)
|
|
|
|
try:
|
|
|
|
db.add(new_hardware)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_hardware)
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_bind_data = HardwareBindData(hardware_id=new_hardware.id, bindedAt=datetime.now().isoformat())
|
|
|
|
return HardwareBindResponse(status="success", message="绑定硬件成功", data=hardware_bind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#解绑硬件
|
|
|
|
async def unbind_hardware_handler(hardware_id:int, db: Session):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
try:
|
|
|
|
db.delete(existing_hardware)
|
|
|
|
db.commit()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
hardware_unbind_data = HardwareUnbindData(unbindedAt=datetime.now().isoformat())
|
|
|
|
return HardwareUnbindResponse(status="success", message="解绑硬件成功", data=hardware_unbind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#硬件换绑
|
|
|
|
async def change_bind_hardware_handler(hardware_id, user, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
if existing_hardware is None:
|
|
|
|
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))
|
|
|
|
hardware_change_bind_data = HardwareChangeBindData(bindChangedAt=datetime.now().isoformat())
|
|
|
|
return HardwareChangeBindResponse(status="success", message="硬件换绑成功", data=hardware_change_bind_data)
|
|
|
|
|
|
|
|
|
|
|
|
#硬件信息更新
|
|
|
|
async def update_hardware_handler(hardware_id, hardware, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
if existing_hardware is None:
|
|
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="硬件不存在")
|
|
|
|
existing_hardware.mac = hardware.mac
|
|
|
|
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))
|
|
|
|
hardware_update_data = HardwareUpdateData(updatedAt=datetime.now().isoformat())
|
|
|
|
return HardwareUpdateResponse(status="success", message="硬件信息更新成功", data=hardware_update_data)
|
|
|
|
|
|
|
|
|
|
|
|
#查询硬件
|
|
|
|
async def get_hardware_handler(hardware_id, db):
|
|
|
|
try:
|
|
|
|
existing_hardware = db.query(Hardware).filter(Hardware.id == hardware_id).first()
|
|
|
|
except Exception as e:
|
|
|
|
db.rollback()
|
|
|
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=str(e))
|
|
|
|
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)
|