From c0cb672750017f097a76e47d44f9ce4bd1b54659 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 28 Jul 2022 13:01:19 +0300 Subject: [PATCH] feat: get apps versions method OS-22 --- src/core.rs | 33 +++++++++++++++++++++++++++++++-- src/main.rs | 1 + src/routes.rs | 9 +++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/core.rs b/src/core.rs index 24fef40..4667405 100644 --- a/src/core.rs +++ b/src/core.rs @@ -16,6 +16,7 @@ use crate::types::*; pub struct Core { users: Collection, apps: Collection, + apps_versions: Collection, reviews: Collection, personal_libraries: Collection, jwt_secret: String, @@ -27,6 +28,7 @@ impl Core { Core { users: db.collection("users"), apps: db.collection("apps"), + apps_versions: db.collection("apps_versions"), reviews: db.collection("reviews"), personal_libraries: db.collection("personal_libraries"), jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET not found"), @@ -125,11 +127,20 @@ impl Core { .unwrap(); } - pub async fn get_reviews(&self, app_name_id: &String) -> Vec { - self.get_collection_with_params(&self.reviews, doc! {"app_name_id":app_name_id}) + pub async fn get_reviews(&self, app_id: &String) -> Vec { + self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id}) .await } + pub async fn get_versions(&self, app_id: &String) -> Vec { + self.get_collection_with_params_and_sort( + &self.apps_versions, + doc! {"app_id":app_id}, + doc! {"timestamp": -1}, + ) + .await + } + pub async fn write_review(&self, name: &String, info: &Json) -> serde_json::Value { let options = FindOneOptions::builder() .projection(doc! {"_id" : 1}) @@ -469,6 +480,24 @@ impl Core { format!("{:x}", hash) } + async fn get_collection_with_params_and_sort( + &self, + collection: &Collection, + params: Document, + sort_params: Document, + ) -> Vec { + let options = FindOptions::builder() + .projection(doc! {"_id" : 0}) + .sort(sort_params) + .build(); + let cursor = match collection.find(params, options).await { + Ok(cursor) => cursor, + Err(_) => return vec![], + }; + + cursor.try_collect().await.unwrap_or_else(|_| vec![]) + } + async fn get_collection_with_params( &self, collection: &Collection, diff --git a/src/main.rs b/src/main.rs index 7d5c561..e635c50 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,7 @@ async fn main() -> std::io::Result<()> { .service(routes::app) .service(routes::reviews) .service(routes::rating) + .service(routes::versions) .service(routes::update) .service(routes::change_password) .service(routes::write_review) diff --git a/src/routes.rs b/src/routes.rs index e18f81a..b81180a 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -30,6 +30,15 @@ pub async fn reviews( HttpResponse::Ok().json(app_data.core.get_reviews(&app_id).await) } +#[get("/versions/{app_id}")] +#[has_any_permission("user", "admin")] +pub async fn versions( + app_data: web::Data, + app_id: web::Path, +) -> impl Responder { + HttpResponse::Ok().json(app_data.core.get_versions(&app_id).await) +} + #[get("/rating/{app_id}")] #[has_any_permission("user", "admin")] pub async fn rating(