78 lines
3.1 KiB
Python
78 lines
3.1 KiB
Python
from __future__ import annotations
|
|||
|
|
|
||
|
|
from fastapi import APIRouter, HTTPException, status
|
||
|
|
from sqlalchemy import select
|
||
|
|
|
||
|
|
from sakura_cloud.api.deps import CurrentUser, DbSession
|
||
|
|
from sakura_cloud.models.device import Device
|
||
|
|
from sakura_cloud.models.mixins import utc_now
|
||
|
|
from sakura_cloud.schemas.common import ApiResponse
|
||
|
|
from sakura_cloud.schemas.device import DeviceOut, DeviceRegister, DeviceUpdate
|
||
|
|
|
||
|
|
|
||
|
|
router = APIRouter()
|
||
|
|
|
||
|
|
|
||
|
|
@router.post("/register", response_model=ApiResponse[DeviceOut], status_code=status.HTTP_201_CREATED)
|
||
|
|
def register_device(payload: DeviceRegister, current_user: CurrentUser, db: DbSession) -> ApiResponse[DeviceOut]:
|
||
|
|
device = Device(
|
||
|
|
user_id=current_user.id,
|
||
|
|
device_name=payload.device_name,
|
||
|
|
device_type=payload.device_type,
|
||
|
|
device_token=payload.device_token,
|
||
|
|
last_active_at=utc_now(),
|
||
|
|
)
|
||
|
|
db.add(device)
|
||
|
|
db.commit()
|
||
|
|
db.refresh(device)
|
||
|
|
return ApiResponse(code=201, message="created", data=DeviceOut.model_validate(device))
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("", response_model=ApiResponse[list[DeviceOut]])
|
||
|
|
def list_devices(current_user: CurrentUser, db: DbSession) -> ApiResponse[list[DeviceOut]]:
|
||
|
|
rows = db.scalars(select(Device).where(Device.user_id == current_user.id).order_by(Device.updated_at.desc())).all()
|
||
|
|
return ApiResponse(data=[DeviceOut.model_validate(row) for row in rows])
|
||
|
|
|
||
|
|
|
||
|
|
@router.get("/sync/status", response_model=ApiResponse[dict[str, str | int | None]])
|
||
|
|
def sync_status(current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, str | int | None]]:
|
||
|
|
latest = db.scalar(
|
||
|
|
select(Device).where(Device.user_id == current_user.id).order_by(Device.last_sync_at.desc().nulls_last())
|
||
|
|
)
|
||
|
|
return ApiResponse(
|
||
|
|
data={
|
||
|
|
"user_id": current_user.id,
|
||
|
|
"storage_used": current_user.storage_used,
|
||
|
|
"last_sync_at": latest.last_sync_at.isoformat() if latest and latest.last_sync_at else None,
|
||
|
|
}
|
||
|
|
)
|
||
|
|
|
||
|
|
|
||
|
|
@router.put("/{device_id}", response_model=ApiResponse[DeviceOut])
|
||
|
|
def update_device(
|
||
|
|
device_id: str,
|
||
|
|
payload: DeviceUpdate,
|
||
|
|
current_user: CurrentUser,
|
||
|
|
db: DbSession,
|
||
|
|
) -> ApiResponse[DeviceOut]:
|
||
|
|
device = db.get(Device, device_id)
|
||
|
|
if device is None or device.user_id != current_user.id:
|
||
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Device not found")
|
||
|
|
if payload.device_name is not None:
|
||
|
|
device.device_name = payload.device_name
|
||
|
|
if payload.device_token is not None:
|
||
|
|
device.device_token = payload.device_token
|
||
|
|
device.last_active_at = utc_now()
|
||
|
|
db.commit()
|
||
|
|
db.refresh(device)
|
||
|
|
return ApiResponse(data=DeviceOut.model_validate(device))
|
||
|
|
|
||
|
|
|
||
|
|
@router.delete("/{device_id}", response_model=ApiResponse[dict[str, bool]])
|
||
|
|
def delete_device(device_id: str, current_user: CurrentUser, db: DbSession) -> ApiResponse[dict[str, bool]]:
|
||
|
|
device = db.get(Device, device_id)
|
||
|
|
if device is None or device.user_id != current_user.id:
|
||
|
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Device not found")
|
||
|
|
db.delete(device)
|
||
|
|
db.commit()
|
||
|
|
return ApiResponse(data={"ok": True})
|