Skip to content

Commit

Permalink
feat: return newest data for imported workspace (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit authored Oct 20, 2023
1 parent 22f3e04 commit 12911ae
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
18 changes: 15 additions & 3 deletions apps/keck/src/server/api/blocks/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub async fn get_workspace(Extension(context): Extension<Arc<Context>>, Path(wor
/// Init a `Workspace` by id
/// - Return 200 Ok and `Workspace`'s data if init success.
/// - Return 304 Not Modified if `Workspace` is exists.
/// - Return 404 Not Found if `Workspace` is not exists (failed to import, data
/// may be corrupted).
/// - Return 500 Internal Server Error if init failed.
#[utoipa::path(
post,
Expand All @@ -54,8 +56,9 @@ pub async fn get_workspace(Extension(context): Extension<Arc<Context>>, Path(wor
content_type="application/octet-stream"
),
responses(
(status = 200, description = "Workspace init success"),
(status = 200, description = "Workspace init success", body = Vec<u8>),
(status = 304, description = "Workspace is exists"),
(status = 404, description = "Workspace not found (failed to import, data may be corrupted)"),
(status = 500, description = "Failed to init a workspace")
)
)]
Expand All @@ -79,14 +82,23 @@ pub async fn init_workspace(

if has_error {
StatusCode::INTERNAL_SERVER_ERROR.into_response()
} else if let Err(e) = context.init_workspace(workspace, data).await {
} else if let Err(e) = context.init_workspace(&workspace, data).await {
if matches!(e, JwstStorageError::WorkspaceExists(_)) {
return StatusCode::NOT_MODIFIED.into_response();
}
warn!("failed to init workspace: {}", e.to_string());
StatusCode::INTERNAL_SERVER_ERROR.into_response()
} else {
StatusCode::OK.into_response()
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()
}
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions apps/keck/src/server/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@ impl Context {
self.storage.init_workspace(workspace_id, data).await
}

pub async fn export_workspace<S>(&self, workspace_id: S) -> JwstStorageResult<Vec<u8>>
where
S: AsRef<str>,
{
self.storage.export_workspace(workspace_id).await
}

pub async fn create_workspace<S>(&self, workspace_id: S) -> JwstStorageResult<Workspace>
where
S: AsRef<str>,
Expand Down

1 comment on commit 12911ae

@vercel
Copy link

@vercel vercel bot commented on 12911ae 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.