Skip to content

Commit

Permalink
feat: add optional name param for workspace creation
Browse files Browse the repository at this point in the history
  • Loading branch information
speed2exe committed Jan 24, 2024
1 parent 353065d commit d04eae9
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 11 deletions.
8 changes: 6 additions & 2 deletions libs/client-api/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::notify::{ClientToken, TokenStateReceiver};
use anyhow::Context;
use brotli::CompressorReader;
use gotrue_entity::dto::AuthProvider;
use shared_entity::dto::workspace_dto::CreateWorkspaceParam;
use std::io::Read;

use app_error::AppError;
Expand Down Expand Up @@ -503,12 +504,15 @@ impl Client {
}

#[instrument(level = "debug", skip_all, err)]
pub async fn add_workspace(&self) -> Result<AFWorkspace, AppResponseError> {
pub async fn create_workspace(
&self,
params: CreateWorkspaceParam,
) -> Result<AFWorkspace, AppResponseError> {
let url = format!("{}/api/workspace", self.base_url);
let resp = self
.http_client_with_auth(Method::POST, &url)
.await?
.json(&())
.json(&params)
.send()
.await?;
log_request_id(&resp);
Expand Down
2 changes: 1 addition & 1 deletion libs/database-entity/src/dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ pub struct AFUserProfile {
pub updated_at: i64,
}

#[derive(Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct AFWorkspace {
pub workspace_id: Uuid,
pub database_storage_id: Uuid,
Expand Down
10 changes: 5 additions & 5 deletions libs/shared-entity/src/dto/workspace_dto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ impl From<Vec<CreateWorkspaceMember>> for CreateWorkspaceMembers {
}
}

#[derive(Deserialize)]
pub struct CreateWorkspace {
pub name: Option<String>,
}

#[derive(Deserialize, Serialize)]
pub struct CreateWorkspaceMember {
pub email: String,
Expand Down Expand Up @@ -82,3 +77,8 @@ pub struct BlobMetadata {
pub file_size: i64,
pub modified_at: DateTime<Utc>,
}

#[derive(Serialize, Deserialize)]
pub struct CreateWorkspaceParam {
pub workspace_name: Option<String>,
}
5 changes: 3 additions & 2 deletions src/api/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ pub fn collab_scope() -> Scope {
async fn create_workpace_handler(
uuid: UserUuid,
state: Data<AppState>,
create_workspace_param: Json<CreateWorkspace>,
create_workspace_param: Json<CreateWorkspaceParam>,
) -> Result<Json<AppResponse<AFWorkspace>>> {
let workspace_name = create_workspace_param
.into_inner()
.name
.workspace_name
.unwrap_or_else(|| format!("workspace_{}", chrono::Utc::now().timestamp()));
let new_workspace =
workspace::ops::create_workspace_for_user(&state.pg_pool, &uuid, &workspace_name).await?;
Expand All @@ -140,6 +140,7 @@ async fn delete_workspace_handler(
Ok(AppResponse::Ok().into())
}

// TODO: also get shared workspaces
#[instrument(skip_all, err)]
async fn list_workspace_handler(
uuid: UserUuid,
Expand Down
16 changes: 15 additions & 1 deletion tests/workspace/workspace_crud.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
use client_api_test_util::generate_unique_registered_user_client;
use shared_entity::dto::workspace_dto::CreateWorkspaceParam;

#[tokio::test]
async fn add_and_delete_workspace_for_user() {
let (c, _user) = generate_unique_registered_user_client().await;
let workspaces = c.get_workspaces().await.unwrap();
assert_eq!(workspaces.0.len(), 1);
let newly_addad_workspace = c.add_workspace().await.unwrap();
let newly_addad_workspace = c
.create_workspace(CreateWorkspaceParam {
workspace_name: Some("my_workspace".to_string()),
})
.await
.unwrap();
let workspaces = c.get_workspaces().await.unwrap();
assert_eq!(workspaces.0.len(), 2);

let _ = workspaces
.0
.iter()
.find(|w| {
w.workspace_name == "my_workspace" && w.workspace_id == newly_addad_workspace.workspace_id
})
.unwrap();

c.delete_workspace(&newly_addad_workspace.workspace_id.to_string())
.await
.unwrap();
Expand Down

0 comments on commit d04eae9

Please sign in to comment.