Skip to content

Commit 353065d

Browse files
committed
feat: add optional name param for workspace creation
1 parent e2bf59c commit 353065d

File tree

5 files changed

+26
-11
lines changed

5 files changed

+26
-11
lines changed

.sqlx/query-c08e9c86c0877ed54499e47dbf83ffe12c16dfedea5b826e2becb0d65d6c61b1.json renamed to .sqlx/query-5506d75e81326efb146326c929e90442ba1a49ddb811f89faa8ba1459e17ca86.json

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/database/src/workspace.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,17 @@ pub async fn delete_from_workspace(pg_pool: &PgPool, workspace_id: &Uuid) -> Res
3131
pub async fn insert_user_workspace(
3232
pg_pool: &PgPool,
3333
user_uuid: &Uuid,
34+
workspace_name: &str,
3435
) -> Result<AFWorkspaceRow, AppError> {
3536
let workspace = sqlx::query_as!(
3637
AFWorkspaceRow,
3738
r#"
38-
INSERT INTO public.af_workspace (owner_uid)
39-
SELECT uid FROM public.af_user WHERE uuid = $1
39+
INSERT INTO public.af_workspace (owner_uid, workspace_name)
40+
VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)
4041
RETURNING *;
4142
"#,
42-
user_uuid
43+
user_uuid,
44+
workspace_name,
4345
)
4446
.fetch_one(pg_pool)
4547
.await?;

libs/shared-entity/src/dto/workspace_dto.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ impl From<Vec<CreateWorkspaceMember>> for CreateWorkspaceMembers {
2929
}
3030
}
3131

32+
#[derive(Deserialize)]
33+
pub struct CreateWorkspace {
34+
pub name: Option<String>,
35+
}
36+
3237
#[derive(Deserialize, Serialize)]
3338
pub struct CreateWorkspaceMember {
3439
pub email: String,

src/api/workspace.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn workspace_scope() -> Scope {
4545

4646
.service(web::resource("")
4747
.route(web::get().to(list_workspace_handler))
48-
.route(web::post().to(add_workpace_handler))
48+
.route(web::post().to(create_workpace_handler))
4949
)
5050
.service(web::resource("/{workspace_id}")
5151
.route(web::delete().to(delete_workspace_handler))
@@ -116,11 +116,17 @@ pub fn collab_scope() -> Scope {
116116

117117
// Adds a workspace for user, if success, return the workspace id
118118
#[instrument(skip_all, err)]
119-
async fn add_workpace_handler(
119+
async fn create_workpace_handler(
120120
uuid: UserUuid,
121121
state: Data<AppState>,
122+
create_workspace_param: Json<CreateWorkspace>,
122123
) -> Result<Json<AppResponse<AFWorkspace>>> {
123-
let new_workspace = workspace::ops::add_workspace_for_user(&state.pg_pool, &uuid).await?;
124+
let workspace_name = create_workspace_param
125+
.into_inner()
126+
.name
127+
.unwrap_or_else(|| format!("workspace_{}", chrono::Utc::now().timestamp()));
128+
let new_workspace =
129+
workspace::ops::create_workspace_for_user(&state.pg_pool, &uuid, &workspace_name).await?;
124130
Ok(AppResponse::Ok().with_data(new_workspace).into())
125131
}
126132

src/biz/workspace/ops.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@ pub async fn delete_workspace_for_user(
2525
Ok(())
2626
}
2727

28-
pub async fn add_workspace_for_user(
28+
pub async fn create_workspace_for_user(
2929
pg_pool: &PgPool,
3030
user_uuid: &Uuid,
31+
workspace_name: &str,
3132
) -> Result<AFWorkspace, AppResponseError> {
32-
let new_workspace_row = insert_user_workspace(pg_pool, user_uuid).await?;
33+
let new_workspace_row = insert_user_workspace(pg_pool, user_uuid, workspace_name).await?;
3334
let new_workspace = AFWorkspace::try_from(new_workspace_row)?;
3435
Ok(new_workspace)
3536
}

0 commit comments

Comments
 (0)