Browse Source

[server]: add delete trash test

appflowy 3 years ago
parent
commit
560ee376f9

+ 6 - 2
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<PgPool>) -> Result<HttpResponse, ServerError> {
+pub async fn delete_handler(
+    payload: Payload,
+    pool: Data<PgPool>,
+    logged_user: LoggedUser,
+) -> Result<HttpResponse, ServerError> {
     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<PgPool>) -> Result<Http
         .context("Failed to acquire a Postgres connection to delete trash")?;
 
     let trash_ids = check_trash_ids(params.ids.into_vec())?;
-    let _ = delete_trash(&mut transaction, trash_ids).await?;
+    let _ = delete_trash(&mut transaction, trash_ids, &logged_user).await?;
     transaction
         .commit()
         .await

+ 31 - 2
backend/src/service/trash/trash.rs

@@ -1,6 +1,9 @@
 use crate::{
     entities::workspace::{TrashTable, TRASH_TABLE},
-    service::{user::LoggedUser, view::read_view_table},
+    service::{
+        user::LoggedUser,
+        view::{delete_view, read_view_table},
+    },
     sqlx_ext::{map_sqlx_error, DBTransaction, SqlBuilder},
 };
 use ::protobuf::ProtobufEnum;
@@ -29,8 +32,34 @@ pub(crate) async fn create_trash(
     Ok(())
 }
 
-pub(crate) async fn delete_trash(transaction: &mut DBTransaction<'_>, trash_ids: Vec<Uuid>) -> Result<(), ServerError> {
+pub(crate) async fn delete_trash(
+    transaction: &mut DBTransaction<'_>,
+    trash_ids: Vec<Uuid>,
+    _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::<Postgres, TrashTable, PgArguments>(&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<'_>)

+ 29 - 1
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::<Vec<String>>();
     // 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]