router.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. use crate::{
  2. context::FlowyPersistence,
  3. entities::logged_user::LoggedUser,
  4. services::core::trash::{create_trash, delete_all_trash, delete_trash, read_trash},
  5. util::serde_ext::parse_from_payload,
  6. };
  7. use ::protobuf::ProtobufEnum;
  8. use actix_web::{
  9. web::{Data, Payload},
  10. HttpResponse,
  11. };
  12. use anyhow::Context;
  13. use backend_service::{
  14. errors::{invalid_params, ServerError},
  15. response::FlowyResponse,
  16. };
  17. use flowy_core_data_model::{parser::trash::TrashIdentify, protobuf::RepeatedTrashId};
  18. use sqlx::PgPool;
  19. use std::sync::Arc;
  20. use uuid::Uuid;
  21. #[tracing::instrument(skip(payload, pool, logged_user), err)]
  22. pub async fn create_handler(
  23. payload: Payload,
  24. pool: Data<PgPool>,
  25. logged_user: LoggedUser,
  26. ) -> Result<HttpResponse, ServerError> {
  27. let params: RepeatedTrashId = parse_from_payload(payload).await?;
  28. let mut transaction = pool
  29. .begin()
  30. .await
  31. .context("Failed to acquire a Postgres connection to create trash")?;
  32. let _ = create_trash(&mut transaction, make_records(params)?, logged_user).await?;
  33. transaction
  34. .commit()
  35. .await
  36. .context("Failed to commit SQL transaction to create trash.")?;
  37. Ok(FlowyResponse::success().into())
  38. }
  39. #[tracing::instrument(skip(payload, persistence, logged_user), fields(delete_trash), err)]
  40. pub async fn delete_handler(
  41. payload: Payload,
  42. persistence: Data<Arc<FlowyPersistence>>,
  43. logged_user: LoggedUser,
  44. ) -> Result<HttpResponse, ServerError> {
  45. let pool = persistence.pg_pool();
  46. let kv_store = persistence.kv_store();
  47. let params: RepeatedTrashId = parse_from_payload(payload).await?;
  48. let mut transaction = pool
  49. .begin()
  50. .await
  51. .context("Failed to acquire a Postgres connection to delete trash")?;
  52. if params.delete_all {
  53. tracing::Span::current().record("delete_trash", &"all");
  54. let _ = delete_all_trash(&mut transaction, &kv_store, &logged_user).await?;
  55. } else {
  56. let records = make_records(params)?;
  57. let _ = delete_trash(&mut transaction, &kv_store, records).await?;
  58. }
  59. transaction
  60. .commit()
  61. .await
  62. .context("Failed to commit SQL transaction to delete trash.")?;
  63. Ok(FlowyResponse::success().into())
  64. }
  65. pub async fn read_handler(pool: Data<PgPool>, logged_user: LoggedUser) -> Result<HttpResponse, ServerError> {
  66. let mut transaction = pool
  67. .begin()
  68. .await
  69. .context("Failed to acquire a Postgres connection to read trash")?;
  70. let repeated_trash = read_trash(&mut transaction, &logged_user).await?;
  71. transaction
  72. .commit()
  73. .await
  74. .context("Failed to commit SQL transaction to read view.")?;
  75. Ok(FlowyResponse::success().pb(repeated_trash)?.into())
  76. }
  77. fn check_trash_id(id: String) -> Result<Uuid, ServerError> {
  78. let trash_id = TrashIdentify::parse(id).map_err(invalid_params)?;
  79. let trash_id = Uuid::parse_str(trash_id.as_ref())?;
  80. Ok(trash_id)
  81. }
  82. fn make_records(identifiers: RepeatedTrashId) -> Result<Vec<(Uuid, i32)>, ServerError> {
  83. let mut records = vec![];
  84. for identifier in identifiers.items {
  85. // match TrashType::from_i32(identifier.ty.value()) {
  86. // None => {}
  87. // Some(ty) => {}
  88. // }
  89. records.push((check_trash_id(identifier.id.to_owned())?, identifier.ty.value()));
  90. }
  91. Ok(records)
  92. }