feat: add optional name param for workspace creation

This commit is contained in:
Fu Zi Xiang 2024-01-24 11:35:44 +08:00
parent e2bf59cff4
commit 353065dfbf
No known key found for this signature in database
5 changed files with 26 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "\n INSERT INTO public.af_workspace (owner_uid)\n SELECT uid FROM public.af_user WHERE uuid = $1\n RETURNING *;\n ",
"query": "\n INSERT INTO public.af_workspace (owner_uid, workspace_name)\n VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)\n RETURNING *;\n ",
"describe": {
"columns": [
{
@ -41,7 +41,8 @@
],
"parameters": {
"Left": [
"Uuid"
"Uuid",
"Text"
]
},
"nullable": [
@ -54,5 +55,5 @@
true
]
},
"hash": "c08e9c86c0877ed54499e47dbf83ffe12c16dfedea5b826e2becb0d65d6c61b1"
"hash": "5506d75e81326efb146326c929e90442ba1a49ddb811f89faa8ba1459e17ca86"
}

View File

@ -31,15 +31,17 @@ pub async fn delete_from_workspace(pg_pool: &PgPool, workspace_id: &Uuid) -> Res
pub async fn insert_user_workspace(
pg_pool: &PgPool,
user_uuid: &Uuid,
workspace_name: &str,
) -> Result<AFWorkspaceRow, AppError> {
let workspace = sqlx::query_as!(
AFWorkspaceRow,
r#"
INSERT INTO public.af_workspace (owner_uid)
SELECT uid FROM public.af_user WHERE uuid = $1
INSERT INTO public.af_workspace (owner_uid, workspace_name)
VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)
RETURNING *;
"#,
user_uuid
user_uuid,
workspace_name,
)
.fetch_one(pg_pool)
.await?;

View File

@ -29,6 +29,11 @@ impl From<Vec<CreateWorkspaceMember>> for CreateWorkspaceMembers {
}
}
#[derive(Deserialize)]
pub struct CreateWorkspace {
pub name: Option<String>,
}
#[derive(Deserialize, Serialize)]
pub struct CreateWorkspaceMember {
pub email: String,

View File

@ -45,7 +45,7 @@ pub fn workspace_scope() -> Scope {
.service(web::resource("")
.route(web::get().to(list_workspace_handler))
.route(web::post().to(add_workpace_handler))
.route(web::post().to(create_workpace_handler))
)
.service(web::resource("/{workspace_id}")
.route(web::delete().to(delete_workspace_handler))
@ -116,11 +116,17 @@ pub fn collab_scope() -> Scope {
// Adds a workspace for user, if success, return the workspace id
#[instrument(skip_all, err)]
async fn add_workpace_handler(
async fn create_workpace_handler(
uuid: UserUuid,
state: Data<AppState>,
create_workspace_param: Json<CreateWorkspace>,
) -> Result<Json<AppResponse<AFWorkspace>>> {
let new_workspace = workspace::ops::add_workspace_for_user(&state.pg_pool, &uuid).await?;
let workspace_name = create_workspace_param
.into_inner()
.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?;
Ok(AppResponse::Ok().with_data(new_workspace).into())
}

View File

@ -25,11 +25,12 @@ pub async fn delete_workspace_for_user(
Ok(())
}
pub async fn add_workspace_for_user(
pub async fn create_workspace_for_user(
pg_pool: &PgPool,
user_uuid: &Uuid,
workspace_name: &str,
) -> Result<AFWorkspace, AppResponseError> {
let new_workspace_row = insert_user_workspace(pg_pool, user_uuid).await?;
let new_workspace_row = insert_user_workspace(pg_pool, user_uuid, workspace_name).await?;
let new_workspace = AFWorkspace::try_from(new_workspace_row)?;
Ok(new_workspace)
}