use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use crate::models::workspace::Workspace;
use crate::rpc::error::RpcError;
use crate::state::AppState;
#[derive(Debug, Serialize)]
pub struct ListResult {
pub workspaces: Vec<Workspace>,
}
pub async fn list(state: &AppState) -> Result<ListResult, RpcError> {
let workspaces = state.workspace_store.list().await?;
Ok(ListResult { workspaces })
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GetParams {
pub id: String,
}
pub async fn get(state: &AppState, params: GetParams) -> Result<Workspace, RpcError> {
state
.workspace_store
.get(¶ms.id)
.await?
.ok_or_else(|| RpcError::NotFound(format!("Workspace {} not found", params.id)))
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateParams {
pub title: String,
pub metadata: Option<HashMap<String, String>>,
}
#[derive(Debug, Serialize)]
pub struct CreateResult {
pub workspace: Workspace,
}
pub async fn create(state: &AppState, params: CreateParams) -> Result<CreateResult, RpcError> {
let ws = Workspace::new(
uuid::Uuid::new_v4().to_string(),
params.title,
params.metadata,
);
state.workspace_store.save(&ws).await?;
Ok(CreateResult { workspace: ws })
}
#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteParams {
pub id: String,
}
#[derive(Debug, Serialize)]
pub struct DeleteResult {
pub deleted: bool,
}
pub async fn delete(state: &AppState, params: DeleteParams) -> Result<DeleteResult, RpcError> {
state.workspace_store.delete(¶ms.id).await?;
Ok(DeleteResult { deleted: true })
}