feat: add workspace icon field

This commit is contained in:
Zack Fu Zi Xiang 2024-02-27 11:26:31 +08:00
parent 375318774a
commit 7abba9d7ef
No known key found for this signature in database
11 changed files with 98 additions and 7 deletions

View File

@ -37,6 +37,11 @@
"ordinal": 6,
"name": "workspace_name",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "icon",
"type_info": "Text"
}
],
"parameters": {
@ -51,7 +56,8 @@
true,
false,
true,
true
true,
false
]
},
"hash": "03b8ab1c98353b442f9c143c29a905442985f7cfa19faaaa970a410708adc773"

View File

@ -37,6 +37,11 @@
"ordinal": 6,
"name": "workspace_name",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "icon",
"type_info": "Text"
}
],
"parameters": {
@ -51,7 +56,8 @@
true,
false,
true,
true
true,
false
]
},
"hash": "0517066279a74e9af59645b8bb152273aa959b610ca3d5e4f8eaecf4f7785dc2"

View File

@ -37,6 +37,11 @@
"ordinal": 6,
"name": "workspace_name",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "icon",
"type_info": "Text"
}
],
"parameters": {
@ -52,7 +57,8 @@
true,
false,
true,
true
true,
false
]
},
"hash": "5506d75e81326efb146326c929e90442ba1a49ddb811f89faa8ba1459e17ca86"

View File

@ -37,6 +37,11 @@
"ordinal": 6,
"name": "workspace_name",
"type_info": "Text"
},
{
"ordinal": 7,
"name": "icon",
"type_info": "Text"
}
],
"parameters": {
@ -51,7 +56,8 @@
true,
false,
true,
true
true,
false
]
},
"hash": "af5c5c1fbf22870171f644e70b0abe5a46d40b9bff68df3896adc5348539d27b"

View File

@ -444,6 +444,7 @@ pub struct AFWorkspace {
pub workspace_type: i32,
pub workspace_name: String,
pub created_at: DateTime<Utc>,
pub icon: String,
}
#[derive(Serialize, Deserialize)]

View File

@ -16,6 +16,7 @@ pub struct AFWorkspaceRow {
pub workspace_type: i32,
pub deleted_at: Option<DateTime<Utc>>,
pub workspace_name: Option<String>,
pub icon: Option<String>,
}
impl TryFrom<AFWorkspaceRow> for AFWorkspace {
@ -31,6 +32,7 @@ impl TryFrom<AFWorkspaceRow> for AFWorkspace {
let workspace_name = value.workspace_name.unwrap_or_default();
let created_at = value.created_at.unwrap_or_else(Utc::now);
let icon = value.icon.unwrap_or_default();
Ok(Self {
workspace_id: value.workspace_id,
@ -39,6 +41,7 @@ impl TryFrom<AFWorkspaceRow> for AFWorkspace {
workspace_type: value.workspace_type,
workspace_name,
created_at,
icon,
})
}
}

View File

@ -73,6 +73,33 @@ pub async fn rename_workspace(
Ok(())
}
#[inline]
pub async fn change_workspace_icon(
tx: &mut Transaction<'_, sqlx::Postgres>,
workspace_id: &Uuid,
icon: &str,
) -> Result<(), AppError> {
let res = sqlx::query!(
r#"
UPDATE public.af_workspace
SET icon = $1
WHERE workspace_id = $2
"#,
icon,
workspace_id,
)
.execute(tx.deref_mut())
.await?;
if res.rows_affected() != 1 {
tracing::error!(
"Failed to change workspace icon, workspace_id: {}",
workspace_id
);
}
Ok(())
}
/// Checks whether a user, identified by a UUID, is an 'Owner' of a workspace, identified by its
/// workspace_id.
#[inline]

View File

@ -83,8 +83,9 @@ pub struct CreateWorkspaceParam {
pub workspace_name: Option<String>,
}
#[derive(Serialize, Deserialize)]
#[derive(Serialize, Deserialize, Default)]
pub struct PatchWorkspaceParam {
pub workspace_id: Uuid,
pub workspace_name: Option<String>,
pub workspace_icon: Option<String>,
}

View File

@ -153,6 +153,7 @@ async fn patch_workspace_handler(
&state.pg_pool,
&params.workspace_id,
params.workspace_name.as_deref(),
params.workspace_icon.as_deref(),
)
.await?;
Ok(AppResponse::Ok().into())

View File

@ -4,7 +4,7 @@ use database::collab::upsert_collab_member_with_txn;
use database::pg_row::{AFWorkspaceMemberRow, AFWorkspaceRow};
use database::user::select_uid_from_email;
use database::workspace::{
delete_from_workspace, delete_workspace_members, insert_user_workspace,
change_workspace_icon, delete_from_workspace, delete_workspace_members, insert_user_workspace,
insert_workspace_member_with_txn, rename_workspace, select_all_user_workspaces, select_workspace,
select_workspace_member_list, update_updated_at_of_workspace, upsert_workspace_member,
};
@ -39,11 +39,15 @@ pub async fn patch_workspace(
pg_pool: &PgPool,
workspace_id: &Uuid,
workspace_name: Option<&str>,
workspace_icon: Option<&str>,
) -> Result<(), AppResponseError> {
let mut tx = pg_pool.begin().await?;
if let Some(workspace_name) = workspace_name {
rename_workspace(&mut tx, workspace_id, workspace_name).await?;
}
if let Some(workspace_icon) = workspace_icon {
change_workspace_icon(&mut tx, workspace_id, workspace_icon).await?;
}
tx.commit().await?;
Ok(())
}

View File

@ -75,7 +75,7 @@ async fn add_and_delete_workspace_for_non_owner_user() {
}
#[tokio::test]
async fn test_workspace_rename() {
async fn test_workspace_rename_and_icon_change() {
let (c, _user) = generate_unique_registered_user_client().await;
let workspace_id = c
.get_workspaces()
@ -91,6 +91,7 @@ async fn test_workspace_rename() {
c.patch_workspace(PatchWorkspaceParam {
workspace_id,
workspace_name: Some(desired_new_name.to_string()),
..Default::default()
})
.await
.expect("Failed to rename workspace");
@ -108,6 +109,7 @@ async fn test_workspace_rename() {
c.patch_workspace(PatchWorkspaceParam {
workspace_id,
workspace_name: None,
..Default::default()
})
.await
.expect("Failed to rename workspace");
@ -119,4 +121,32 @@ async fn test_workspace_rename() {
.workspace_name;
assert_eq!(actual_new_name, desired_new_name);
}
{
c.patch_workspace(PatchWorkspaceParam {
workspace_id,
workspace_icon: Some("icon123".to_string()),
..Default::default()
})
.await
.expect("Failed to change icon");
let workspaces = c.get_workspaces().await.expect("Failed to get workspaces");
let icon = &workspaces.0.first().expect("No workspace found").icon;
assert_eq!(icon, "icon123");
}
{
c.patch_workspace(PatchWorkspaceParam {
workspace_id,
workspace_name: Some("new_name456".to_string()),
workspace_icon: Some("new_icon456".to_string()),
})
.await
.expect("Failed to change icon");
let workspaces = c.get_workspaces().await.expect("Failed to get workspaces");
let workspace = workspaces.0.first().expect("No workspace found");
let icon = workspace.icon.as_str();
let name = workspace.workspace_name.as_str();
assert_eq!(icon, "new_icon456");
assert_eq!(name, "new_name456");
}
}