Skip to content

Commit

Permalink
feat: add workspace export & update history field (#554)
Browse files Browse the repository at this point in the history
* feat: add export api

* feat: add history action

* feat: add field name
  • Loading branch information
darkskygit authored Oct 20, 2023
1 parent 12911ae commit 63c6e83
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
6 changes: 4 additions & 2 deletions apps/keck/src/server/api/blocks/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ pub struct BlockHistoryQuery {
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct BlockHistory {
pub workspace_id: String,
pub block_id: String,
pub field_name: Option<String>,
pub parent: Vec<String>,
pub content: String,
pub action: String,
}

impl From<(&str, &History)> for BlockHistory {
fn from((workspace_id, history): (&str, &History)) -> Self {
Self {
workspace_id: workspace_id.into(),
block_id: history.id.clone(),
field_name: history.field_name.clone(),
parent: history.parent.iter().map(|id| id.to_string()).collect::<Vec<_>>(),
content: history.content.clone(),
action: history.action.to_string(),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions apps/keck/src/server/api/blocks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fn block_apis(router: Router) -> Router {
fn workspace_apis(router: Router) -> Router {
router
.route("/block/:workspace/init", post(workspace::init_workspace))
.route("/block/:workspace/export", get(workspace::export_workspace))
.route("/block/:workspace/client", get(clients::workspace_client))
.route("/block/:workspace/clients", get(clients::workspace_clients))
.route("/block/:workspace/history", get(history::history_workspace))
Expand Down
33 changes: 33 additions & 0 deletions apps/keck/src/server/api/blocks/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,39 @@ pub async fn init_workspace(
}
}

/// Export a `Workspace` by id
/// - Return 200 Ok and `Workspace`'s data if export success.
/// - Return 404 Not Found if `Workspace` is not exists.
/// - Return 500 Internal Server Error if export failed.
#[utoipa::path(
get,
tag = "Workspace",
context_path = "/api/block",
path = "/{workspace}/export",
params(
("workspace", description = "workspace id"),
),
responses(
(status = 200, description = "Workspace export success", body = Vec<u8>),
(status = 404, description = "Workspace is not exists"),
(status = 500, description = "Failed to export a workspace")
)
)]
pub async fn export_workspace(Extension(context): Extension<Arc<Context>>, Path(workspace): Path<String>) -> Response {
info!("export_workspace: {}", workspace);

match context.export_workspace(workspace).await {
Ok(data) => data.into_response(),
Err(e) => {
if matches!(e, JwstStorageError::WorkspaceNotFound(_)) {
return StatusCode::NOT_FOUND.into_response();
}
warn!("failed to init workspace: {}", e.to_string());
StatusCode::INTERNAL_SERVER_ERROR.into_response()
}
}
}

/// Create a `Workspace` by id
/// - Return 200 Ok and `Workspace`'s data if init success or `Workspace` is
/// exists.
Expand Down
1 change: 1 addition & 0 deletions apps/keck/src/server/api/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use super::{
subscribe::subscribe_workspace,
subscribe::subscribe_test_hook,
workspace::init_workspace,
workspace::export_workspace,
workspace::get_workspace,
workspace::set_workspace,
workspace::delete_workspace,
Expand Down
13 changes: 13 additions & 0 deletions libs/jwst-codec/src/doc/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ impl StoreHistory {

histories.push(History {
id: item.id.to_string(),
field_name: item.parent_sub.clone(),
parent: Self::parse_path(item, &parents),
content: Value::from(&item.content).to_string(),
action: HistoryAction::Update,
Expand All @@ -163,6 +164,7 @@ impl StoreHistory {
for item in deleted_items {
histories.push(History {
id: item.id.to_string(),
field_name: item.parent_sub.clone(),
parent: Self::parse_path(&item, &parents),
content: Value::from(&item.content).to_string(),
action: HistoryAction::Delete,
Expand Down Expand Up @@ -245,9 +247,20 @@ pub enum HistoryAction {
Delete,
}

impl ToString for HistoryAction {
fn to_string(&self) -> String {
match self {
Self::Insert => "insert".to_string(),
Self::Update => "update".to_string(),
Self::Delete => "delete".to_string(),
}
}
}

#[derive(Debug, PartialEq)]
pub struct History {
pub id: String,
pub field_name: Option<String>,
pub parent: Vec<String>,
pub content: String,
pub action: HistoryAction,
Expand Down

1 comment on commit 63c6e83

@vercel
Copy link

@vercel vercel bot commented on 63c6e83 Oct 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.