From 1cebe8122dfc0f1221f434224ae5716461678c9c Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 27 Jul 2022 13:28:58 +0300 Subject: [PATCH] feat: add app to personal library OS-7 --- src/core.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 3 ++- src/routes.rs | 16 +++++++++++++++- src/types.rs | 5 +++++ 4 files changed, 75 insertions(+), 2 deletions(-) diff --git a/src/core.rs b/src/core.rs index 0fea2b5..bfa1d43 100644 --- a/src/core.rs +++ b/src/core.rs @@ -214,6 +214,59 @@ impl Core { } } + pub async fn add_app_to_personal_library( + &self, + name: &String, + app: &String, + ) -> serde_json::Value { + let options = FindOneOptions::builder() + .projection(doc! {"_id" : 1}) + .build(); + let response = self.apps.find_one(doc! {"name_id":&app}, options).await; + match response { + Ok(response) => match response { + Some(_) => { + let response = self + .personal_libraries + .update_one( + doc! {"name": name}, + doc! {"$push": { + "apps":&app, + }}, + None, + ) + .await; + match response { + Ok(_) => { + json! ({ + "code":"ok", + "msg":"App added to personal library" + }) + } + Err(_) => { + json! ({ + "code":"err", + "msg":"App already in the library | Unknown error" + }) + } + } + } + None => { + json! ({ + "code":"denied", + "msg":"This app does not exist" + }) + } + }, + Err(_) => { + json! ({ + "code":"err", + "msg":"Unknown error" + }) + } + } + } + pub async fn change_password( &self, name: &String, diff --git a/src/main.rs b/src/main.rs index ba8be10..1b25182 100644 --- a/src/main.rs +++ b/src/main.rs @@ -86,7 +86,8 @@ async fn main() -> std::io::Result<()> { .service(routes::rating) .service(routes::update) .service(routes::change_password) - .service(routes::write_review), + .service(routes::write_review) + .service(routes::add_app_to_personal_library), ) .service( web::scope("/auth") diff --git a/src/routes.rs b/src/routes.rs index 9b9d22c..c49f6b3 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -75,6 +75,21 @@ pub async fn update( ) } +#[post("/add_app_to_personal_library")] +#[has_any_permission("user", "admin")] +pub async fn add_app_to_personal_library( + app_data: web::Data, + app_info: web::Json, + req: HttpRequest, +) -> impl Responder { + response( + app_data + .core + .add_app_to_personal_library(&username(req), &app_info.name) + .await, + ) +} + #[post("/write_review")] #[has_any_permission("user", "admin")] pub async fn write_review( @@ -108,7 +123,6 @@ fn response(result: serde_json::Value) -> impl Responder { } else if result["code"] == "denied" { HttpResponse::Forbidden().json(result) } else { - println!("{}", result["code"]); HttpResponse::InternalServerError().json(result) } } diff --git a/src/types.rs b/src/types.rs index c99ab88..a783961 100644 --- a/src/types.rs +++ b/src/types.rs @@ -47,3 +47,8 @@ pub struct PasswordsInf { pub old_password: String, pub new_password: String, } + +#[derive(Debug, Serialize, Deserialize)] +pub struct AppToAdd { + pub name: String, +}