feat: add optional name param for workspace creation
This commit is contained in:
parent
e2bf59cff4
commit
353065dfbf
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"db_name": "PostgreSQL",
|
"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": {
|
"describe": {
|
||||||
"columns": [
|
"columns": [
|
||||||
{
|
{
|
||||||
|
|
@ -41,7 +41,8 @@
|
||||||
],
|
],
|
||||||
"parameters": {
|
"parameters": {
|
||||||
"Left": [
|
"Left": [
|
||||||
"Uuid"
|
"Uuid",
|
||||||
|
"Text"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"nullable": [
|
"nullable": [
|
||||||
|
|
@ -54,5 +55,5 @@
|
||||||
true
|
true
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"hash": "c08e9c86c0877ed54499e47dbf83ffe12c16dfedea5b826e2becb0d65d6c61b1"
|
"hash": "5506d75e81326efb146326c929e90442ba1a49ddb811f89faa8ba1459e17ca86"
|
||||||
}
|
}
|
||||||
|
|
@ -31,15 +31,17 @@ pub async fn delete_from_workspace(pg_pool: &PgPool, workspace_id: &Uuid) -> Res
|
||||||
pub async fn insert_user_workspace(
|
pub async fn insert_user_workspace(
|
||||||
pg_pool: &PgPool,
|
pg_pool: &PgPool,
|
||||||
user_uuid: &Uuid,
|
user_uuid: &Uuid,
|
||||||
|
workspace_name: &str,
|
||||||
) -> Result<AFWorkspaceRow, AppError> {
|
) -> Result<AFWorkspaceRow, AppError> {
|
||||||
let workspace = sqlx::query_as!(
|
let workspace = sqlx::query_as!(
|
||||||
AFWorkspaceRow,
|
AFWorkspaceRow,
|
||||||
r#"
|
r#"
|
||||||
INSERT INTO public.af_workspace (owner_uid)
|
INSERT INTO public.af_workspace (owner_uid, workspace_name)
|
||||||
SELECT uid FROM public.af_user WHERE uuid = $1
|
VALUES ((SELECT uid FROM public.af_user WHERE uuid = $1), $2)
|
||||||
RETURNING *;
|
RETURNING *;
|
||||||
"#,
|
"#,
|
||||||
user_uuid
|
user_uuid,
|
||||||
|
workspace_name,
|
||||||
)
|
)
|
||||||
.fetch_one(pg_pool)
|
.fetch_one(pg_pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,11 @@ impl From<Vec<CreateWorkspaceMember>> for CreateWorkspaceMembers {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize)]
|
||||||
|
pub struct CreateWorkspace {
|
||||||
|
pub name: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize)]
|
||||||
pub struct CreateWorkspaceMember {
|
pub struct CreateWorkspaceMember {
|
||||||
pub email: String,
|
pub email: String,
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ pub fn workspace_scope() -> Scope {
|
||||||
|
|
||||||
.service(web::resource("")
|
.service(web::resource("")
|
||||||
.route(web::get().to(list_workspace_handler))
|
.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}")
|
.service(web::resource("/{workspace_id}")
|
||||||
.route(web::delete().to(delete_workspace_handler))
|
.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
|
// Adds a workspace for user, if success, return the workspace id
|
||||||
#[instrument(skip_all, err)]
|
#[instrument(skip_all, err)]
|
||||||
async fn add_workpace_handler(
|
async fn create_workpace_handler(
|
||||||
uuid: UserUuid,
|
uuid: UserUuid,
|
||||||
state: Data<AppState>,
|
state: Data<AppState>,
|
||||||
|
create_workspace_param: Json<CreateWorkspace>,
|
||||||
) -> Result<Json<AppResponse<AFWorkspace>>> {
|
) -> 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())
|
Ok(AppResponse::Ok().with_data(new_workspace).into())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,12 @@ pub async fn delete_workspace_for_user(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn add_workspace_for_user(
|
pub async fn create_workspace_for_user(
|
||||||
pg_pool: &PgPool,
|
pg_pool: &PgPool,
|
||||||
user_uuid: &Uuid,
|
user_uuid: &Uuid,
|
||||||
|
workspace_name: &str,
|
||||||
) -> Result<AFWorkspace, AppResponseError> {
|
) -> 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)?;
|
let new_workspace = AFWorkspace::try_from(new_workspace_row)?;
|
||||||
Ok(new_workspace)
|
Ok(new_workspace)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue