From 045daf1dcfa9eec2014212954ef24f9fbea5cb5b Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 27 Jul 2022 13:46:55 +0300 Subject: [PATCH] feat: delete app from personal library method OS-24 --- src/core.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 3 ++- src/routes.rs | 17 ++++++++++++++++- src/types.rs | 2 +- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/core.rs b/src/core.rs index bfa1d43..1928a77 100644 --- a/src/core.rs +++ b/src/core.rs @@ -214,6 +214,37 @@ impl Core { } } + pub async fn delete_app_from_personal_library( + &self, + name: &String, + app: &String, + ) -> serde_json::Value { + let response = self + .personal_libraries + .update_one( + doc! {"name": name}, + doc! {"$pull": { + "apps":&app, + }}, + None, + ) + .await; + match response { + Ok(_) => { + json! ({ + "code":"ok", + "msg":"App deleted from personal library" + }) + } + Err(_) => { + json! ({ + "code":"err", + "msg":"Unknown error" + }) + } + } + } + pub async fn add_app_to_personal_library( &self, name: &String, diff --git a/src/main.rs b/src/main.rs index 1b25182..7d5c561 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,7 +87,8 @@ async fn main() -> std::io::Result<()> { .service(routes::update) .service(routes::change_password) .service(routes::write_review) - .service(routes::add_app_to_personal_library), + .service(routes::add_app_to_personal_library) + .service(routes::delete_app_from_personal_library), ) .service( web::scope("/auth") diff --git a/src/routes.rs b/src/routes.rs index c49f6b3..7841b67 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -79,7 +79,7 @@ pub async fn update( #[has_any_permission("user", "admin")] pub async fn add_app_to_personal_library( app_data: web::Data, - app_info: web::Json, + app_info: web::Json, req: HttpRequest, ) -> impl Responder { response( @@ -90,6 +90,21 @@ pub async fn add_app_to_personal_library( ) } +#[post("/delete_app_from_personal_library")] +#[has_any_permission("user", "admin")] +pub async fn delete_app_from_personal_library( + app_data: web::Data, + app_info: web::Json, + req: HttpRequest, +) -> impl Responder { + response( + app_data + .core + .delete_app_from_personal_library(&username(req), &app_info.name) + .await, + ) +} + #[post("/write_review")] #[has_any_permission("user", "admin")] pub async fn write_review( diff --git a/src/types.rs b/src/types.rs index a783961..0716bf0 100644 --- a/src/types.rs +++ b/src/types.rs @@ -49,6 +49,6 @@ pub struct PasswordsInf { } #[derive(Debug, Serialize, Deserialize)] -pub struct AppToAdd { +pub struct AppInfo { pub name: String, }