diff --git a/backend/src/service/trash/router.rs b/backend/src/service/trash/router.rs index 912088c228..d0e3fb4bb1 100644 --- a/backend/src/service/trash/router.rs +++ b/backend/src/service/trash/router.rs @@ -43,7 +43,11 @@ pub async fn create_handler( Ok(FlowyResponse::success().into()) } -pub async fn delete_handler(payload: Payload, pool: Data) -> Result { +pub async fn delete_handler( + payload: Payload, + pool: Data, + logged_user: LoggedUser, +) -> Result { let params: TrashIdentifiers = parse_from_payload(payload).await?; let mut transaction = pool .begin() @@ -51,7 +55,7 @@ pub async fn delete_handler(payload: Payload, pool: Data) -> Result, trash_ids: Vec) -> Result<(), ServerError> { +pub(crate) async fn delete_trash( + transaction: &mut DBTransaction<'_>, + trash_ids: Vec, + _user: &LoggedUser, +) -> Result<(), ServerError> { for trash_id in trash_ids { + // Read the trash_table and delete the original table according to the TrashType + let (sql, args) = SqlBuilder::select(TRASH_TABLE) + .add_field("*") + .and_where_eq("id", trash_id) + .build()?; + + let trash_table = sqlx::query_as_with::(&sql, args) + .fetch_one(transaction as &mut DBTransaction<'_>) + .await + .map_err(map_sqlx_error)?; + + match TrashType::from_i32(trash_table.ty) { + None => log::error!("Parser trash type with value: {} failed", trash_table.ty), + Some(ty) => match ty { + TrashType::Unknown => {}, + TrashType::View => { + let _ = delete_view(transaction as &mut DBTransaction<'_>, vec![trash_table.id]).await; + }, + }, + } + + // Delete the trash table let (sql, args) = SqlBuilder::delete(TRASH_TABLE).and_where_eq("id", &trash_id).build()?; let _ = sqlx::query_with(&sql, args) .execute(transaction as &mut DBTransaction<'_>) diff --git a/backend/tests/api/workspace.rs b/backend/tests/api/workspace.rs index ad778fb81b..2589aae45e 100644 --- a/backend/tests/api/workspace.rs +++ b/backend/tests/api/workspace.rs @@ -1,7 +1,7 @@ use crate::helper::*; use flowy_workspace::entities::{ app::{AppIdentifier, DeleteAppParams, UpdateAppParams}, - trash::{CreateTrashParams, TrashType}, + trash::{CreateTrashParams, TrashIdentifiers, TrashType}, view::{UpdateViewParams, ViewIdentifier}, workspace::{CreateWorkspaceParams, DeleteWorkspaceParams, QueryWorkspaceParams, UpdateWorkspaceParams}, }; @@ -167,9 +167,37 @@ async fn view_delete() { }; test.server.create_trash(params).await; + let trash_ids = test + .server + .read_trash() + .await + .items + .into_iter() + .map(|item| item.id) + .collect::>(); // read let read_params = ViewIdentifier::new(&test.view.id); + + // the view can't read from the server. it should be in the trash assert_eq!(test.server.read_view(read_params).await.is_none(), true); + assert_eq!(trash_ids.contains(&test.view.id), true); +} + +#[actix_rt::test] +async fn view_delete_and_then_delete_the_trash_record() { + let test = ViewTest::new().await; + let params = CreateTrashParams { + id: test.view.id.clone(), + ty: TrashType::View, + }; + test.server.create_trash(params).await; + test.server + .delete_trash(TrashIdentifiers { + ids: vec![test.view.id.clone()], + }) + .await; + + assert_eq!(test.server.read_trash().await.is_empty(), true); } #[actix_rt::test]