From 32be9b54a4b9a18811a29d59bcff2e2f14d8c871 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 28 Jul 2022 14:18:59 +0300 Subject: [PATCH] feat: get personal library OS-30 --- src/core.rs | 27 +++++++++++++++++++++++++++ src/main.rs | 3 +++ src/routes.rs | 9 +++++++++ 3 files changed, 39 insertions(+) diff --git a/src/core.rs b/src/core.rs index 4667405..c80e1ed 100644 --- a/src/core.rs +++ b/src/core.rs @@ -63,6 +63,33 @@ impl Core { } } + pub async fn get_personal_library(&self, name: &String) -> serde_json::Value { + let response = self + .personal_libraries + .find_one(doc! {"name":&name}, None) + .await; + match response { + Ok(response) => match response { + Some(result) => json!({ + "code":"ok_body", + "body": result.get_array("apps").unwrap() + }), + None => { + json! ({ + "code":"denied", + "msg":"This user does not exist" + }) + } + }, + Err(_) => { + json! ({ + "code":"err", + "msg":"Unknown error" + }) + } + } + } + pub async fn get_rating(&self, app_id: &String) -> serde_json::Value { let result = self .reviews diff --git a/src/main.rs b/src/main.rs index e635c50..067144e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,11 +80,14 @@ async fn main() -> std::io::Result<()> { .service( web::scope("/api") .wrap(HttpAuthentication::bearer(jwt_validator)) + //get .service(routes::apps) .service(routes::app) .service(routes::reviews) .service(routes::rating) .service(routes::versions) + .service(routes::personal_library) + //post .service(routes::update) .service(routes::change_password) .service(routes::write_review) diff --git a/src/routes.rs b/src/routes.rs index b81180a..7e75d1d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -54,6 +54,15 @@ pub async fn app(app_data: web::Data, name: web::Path) response(app_data.core.get_app(&name).await) } +#[get("/personal_library")] +#[has_any_permission("user", "admin")] +pub async fn personal_library( + app_data: web::Data, + req: HttpRequest, +) -> impl Responder { + response(app_data.core.get_personal_library(&username(req)).await) +} + #[post("/change_password")] #[has_any_permission("user", "admin")] pub async fn change_password(