From faad9ea93a23dc71b65a9f1c335cc6980ef629e0 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 28 Jul 2022 15:14:10 +0300 Subject: [PATCH] feat: get latest version OS-29 --- src/core.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 1 + src/routes.rs | 11 ++++++++++- 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/core.rs b/src/core.rs index c80e1ed..7949bcb 100644 --- a/src/core.rs +++ b/src/core.rs @@ -253,6 +253,48 @@ impl Core { } } + pub async fn get_latest_version(&self, app_id: &String) -> serde_json::Value { + let options = FindOptions::builder() + .projection(doc! {"_id" : 0}) + .sort(doc! {"timestamp":-1}) + .limit(1) + .build(); + + let result = self + .apps_versions + .find(doc! {"app_id":app_id}, options) + .await; + + match result { + Ok(mut cursor) => match cursor.next().await { + Some(result) => match result { + Ok(result) => { + json! ({ + "code":"ok_body", + "body":result + }) + } + Err(_) => { + json! ({ + "code":"err", + "msg":"Unknown error" + }) + } + }, + None => { + json! ({ + "code":"denied", + "msg":"Versions of this app does not exists" + }) + } + }, + Err(_) => json! ({ + "code":"denied", + "msg":"Unknown error" + }), + } + } + pub async fn delete_app_from_personal_library( &self, name: &String, diff --git a/src/main.rs b/src/main.rs index 067144e..bbaf605 100644 --- a/src/main.rs +++ b/src/main.rs @@ -87,6 +87,7 @@ async fn main() -> std::io::Result<()> { .service(routes::rating) .service(routes::versions) .service(routes::personal_library) + .service(routes::latest_version) //post .service(routes::update) .service(routes::change_password) diff --git a/src/routes.rs b/src/routes.rs index 7e75d1d..30bb4aa 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,6 @@ use crate::types::*; use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder}; -use actix_web_grants::proc_macro::{has_any_permission, has_permissions}; +use actix_web_grants::proc_macro::has_any_permission; #[post("/signup")] pub async fn signup(app_data: web::Data, user: web::Json) -> impl Responder { @@ -54,6 +54,15 @@ pub async fn app(app_data: web::Data, name: web::Path) response(app_data.core.get_app(&name).await) } +#[get("/latest_version/{name}")] +#[has_any_permission("user", "admin")] +pub async fn latest_version( + app_data: web::Data, + name: web::Path, +) -> impl Responder { + response(app_data.core.get_latest_version(&name).await) +} + #[get("/personal_library")] #[has_any_permission("user", "admin")] pub async fn personal_library(