mirror of
https://github.com/AppFlowy-IO/AppFlowy.git
synced 2024-08-30 18:12:39 +00:00
[client]: crud with trash on server & fix some bugs
This commit is contained in:
@ -2,7 +2,7 @@ use std::{net::TcpListener, time::Duration};
|
||||
|
||||
use actix::Actor;
|
||||
use actix_identity::{CookieIdentityPolicy, IdentityService};
|
||||
use actix_web::{dev::Server, web, web::Data, App, HttpServer, Scope};
|
||||
use actix_web::{dev::Server, middleware, web, web::Data, App, HttpServer, Scope};
|
||||
use sqlx::{postgres::PgPoolOptions, PgPool};
|
||||
use tokio::time::interval;
|
||||
|
||||
@ -51,7 +51,7 @@ pub fn run(listener: TcpListener, app_ctx: AppContext) -> Result<Server, std::io
|
||||
|
||||
let server = HttpServer::new(move || {
|
||||
App::new()
|
||||
// .wrap(middleware::Logger::default())
|
||||
.wrap(middleware::Logger::default())
|
||||
.wrap(identify_service(&domain, &secret))
|
||||
.wrap(crate::middleware::default_cors())
|
||||
.wrap(crate::middleware::AuthenticationService)
|
||||
|
@ -15,25 +15,24 @@ use flowy_net::{
|
||||
};
|
||||
use flowy_workspace::{
|
||||
entities::trash::parser::{TrashId, TrashTypeParser},
|
||||
protobuf::{CreateTrashParams, TrashIdentifiers},
|
||||
protobuf::TrashIdentifiers,
|
||||
};
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[tracing::instrument(skip(payload, pool, logged_user), err)]
|
||||
pub async fn create_handler(
|
||||
payload: Payload,
|
||||
pool: Data<PgPool>,
|
||||
logged_user: LoggedUser,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
let params: CreateTrashParams = parse_from_payload(payload).await?;
|
||||
let params: TrashIdentifiers = parse_from_payload(payload).await?;
|
||||
let mut transaction = pool
|
||||
.begin()
|
||||
.await
|
||||
.context("Failed to acquire a Postgres connection to create trash")?;
|
||||
|
||||
let trash_id = check_trash_id(params.id)?;
|
||||
let ty = TrashTypeParser::parse(params.ty.value()).map_err(invalid_params)?;
|
||||
let _ = create_trash(&mut transaction, trash_id, ty, logged_user).await?;
|
||||
log::error!("😁create handler: {:?}", params);
|
||||
let _ = create_trash(&mut transaction, make_records(params)?, logged_user).await?;
|
||||
|
||||
transaction
|
||||
.commit()
|
||||
@ -54,8 +53,7 @@ pub async fn delete_handler(
|
||||
.await
|
||||
.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, &logged_user).await?;
|
||||
let _ = delete_trash(&mut transaction, make_records(params)?, &logged_user).await?;
|
||||
transaction
|
||||
.commit()
|
||||
.await
|
||||
@ -86,10 +84,11 @@ fn check_trash_id(id: String) -> Result<Uuid, ServerError> {
|
||||
Ok(trash_id)
|
||||
}
|
||||
|
||||
fn check_trash_ids(ids: Vec<String>) -> Result<Vec<Uuid>, ServerError> {
|
||||
let mut trash_ids = vec![];
|
||||
for id in ids {
|
||||
trash_ids.push(check_trash_id(id)?)
|
||||
fn make_records(identifiers: TrashIdentifiers) -> Result<Vec<(Uuid, i32)>, ServerError> {
|
||||
let mut records = vec![];
|
||||
for identifier in identifiers.items {
|
||||
let ty = TrashTypeParser::parse(identifier.ty.value()).map_err(invalid_params)?;
|
||||
records.push((check_trash_id(identifier.id.to_owned())?, ty));
|
||||
}
|
||||
Ok(trash_ids)
|
||||
Ok(records)
|
||||
}
|
||||
|
@ -8,36 +8,37 @@ use crate::{
|
||||
};
|
||||
use ::protobuf::ProtobufEnum;
|
||||
use flowy_net::errors::ServerError;
|
||||
use flowy_workspace::protobuf::{RepeatedTrash, Trash, TrashIdentifiers, TrashType};
|
||||
use flowy_workspace::protobuf::{RepeatedTrash, Trash, TrashType};
|
||||
use sqlx::{postgres::PgArguments, Postgres};
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(crate) async fn create_trash(
|
||||
transaction: &mut DBTransaction<'_>,
|
||||
trash_id: Uuid,
|
||||
ty: i32,
|
||||
records: Vec<(Uuid, i32)>,
|
||||
user: LoggedUser,
|
||||
) -> Result<(), ServerError> {
|
||||
let (sql, args) = SqlBuilder::create(TRASH_TABLE)
|
||||
.add_arg("id", trash_id)
|
||||
.add_arg("user_id", &user.user_id)
|
||||
.add_arg("ty", ty)
|
||||
.build()?;
|
||||
for (trash_id, ty) in records {
|
||||
let (sql, args) = SqlBuilder::create(TRASH_TABLE)
|
||||
.add_arg("id", trash_id)
|
||||
.add_arg("user_id", &user.user_id)
|
||||
.add_arg("ty", ty)
|
||||
.build()?;
|
||||
|
||||
let _ = sqlx::query_with(&sql, args)
|
||||
.execute(transaction)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
let _ = sqlx::query_with(&sql, args)
|
||||
.execute(transaction as &mut DBTransaction<'_>)
|
||||
.await
|
||||
.map_err(map_sqlx_error)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn delete_trash(
|
||||
transaction: &mut DBTransaction<'_>,
|
||||
trash_ids: Vec<Uuid>,
|
||||
records: Vec<(Uuid, i32)>,
|
||||
_user: &LoggedUser,
|
||||
) -> Result<(), ServerError> {
|
||||
for trash_id in trash_ids {
|
||||
for (trash_id, _) in records {
|
||||
// Read the trash_table and delete the original table according to the TrashType
|
||||
let (sql, args) = SqlBuilder::select(TRASH_TABLE)
|
||||
.add_field("*")
|
||||
|
@ -1,7 +1,6 @@
|
||||
use crate::helper::*;
|
||||
use flowy_workspace::entities::{
|
||||
app::{AppIdentifier, DeleteAppParams, UpdateAppParams},
|
||||
trash::{CreateTrashParams, TrashIdentifiers, TrashType},
|
||||
view::{UpdateViewParams, ViewIdentifier},
|
||||
workspace::{CreateWorkspaceParams, DeleteWorkspaceParams, QueryWorkspaceParams, UpdateWorkspaceParams},
|
||||
};
|
||||
@ -98,11 +97,7 @@ async fn app_read_with_belongs_in_trash() {
|
||||
let _ = create_test_view(&test.server, &test.app.id).await;
|
||||
let view = create_test_view(&test.server, &test.app.id).await;
|
||||
|
||||
let params = CreateTrashParams {
|
||||
id: view.id.clone(),
|
||||
ty: TrashType::View,
|
||||
};
|
||||
test.server.create_trash(params).await;
|
||||
test.server.create_view_trash(&view.id).await;
|
||||
|
||||
let read_params = AppIdentifier::new(&test.app.id);
|
||||
let app = test.server.read_app(read_params).await.unwrap();
|
||||
@ -159,13 +154,7 @@ async fn view_update() {
|
||||
#[actix_rt::test]
|
||||
async fn view_delete() {
|
||||
let test = ViewTest::new().await;
|
||||
|
||||
// delete
|
||||
let params = CreateTrashParams {
|
||||
id: test.view.id.clone(),
|
||||
ty: TrashType::View,
|
||||
};
|
||||
test.server.create_trash(params).await;
|
||||
test.server.create_view_trash(&test.view.id).await;
|
||||
|
||||
let trash_ids = test
|
||||
.server
|
||||
@ -186,16 +175,8 @@ async fn view_delete() {
|
||||
#[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;
|
||||
test.server.create_view_trash(&test.view.id).await;
|
||||
test.server.delete_view_trash(&test.view.id).await;
|
||||
|
||||
assert_eq!(test.server.read_trash().await.is_empty(), true);
|
||||
}
|
||||
|
@ -122,14 +122,27 @@ impl TestUserServer {
|
||||
delete_view_request(self.user_token(), params, &url).await.unwrap();
|
||||
}
|
||||
|
||||
pub async fn create_trash(&self, params: CreateTrashParams) {
|
||||
pub async fn create_view_trash(&self, view_id: &str) {
|
||||
let identifier = TrashIdentifier {
|
||||
id: view_id.to_string(),
|
||||
ty: TrashType::View,
|
||||
};
|
||||
let url = format!("{}/api/trash", self.http_addr());
|
||||
create_trash_request(self.user_token(), params, &url).await.unwrap();
|
||||
create_trash_request(self.user_token(), vec![identifier].into(), &url)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn delete_trash(&self, params: TrashIdentifiers) {
|
||||
pub async fn delete_view_trash(&self, trash_id: &str) {
|
||||
let url = format!("{}/api/trash", self.http_addr());
|
||||
delete_trash_request(self.user_token(), params, &url).await.unwrap();
|
||||
|
||||
let identifier = TrashIdentifier {
|
||||
id: trash_id.to_string(),
|
||||
ty: TrashType::View,
|
||||
};
|
||||
delete_trash_request(self.user_token(), vec![identifier].into(), &url)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
pub async fn read_trash(&self) -> RepeatedTrash {
|
||||
|
Reference in New Issue
Block a user