From c9675fa9716fdb560c53f5750249cf6922fa5bdd Mon Sep 17 00:00:00 2001 From: Artemy Date: Sat, 8 Jul 2023 19:11:13 +0300 Subject: [PATCH 1/2] refactor: get_collection --- src/core.rs | 31 ++++++++++++++++++------------- src/routes.rs | 17 +++++++++++++---- 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/src/core.rs b/src/core.rs index 9abcb4a..4d80f1f 100644 --- a/src/core.rs +++ b/src/core.rs @@ -12,6 +12,8 @@ use serde_json::json; use sha3::{Digest, Sha3_256}; use std::env; +use mongodb::error::Error; + use crate::types::*; pub struct Core { users: Collection, @@ -35,7 +37,7 @@ impl Core { salt: env::var("SALT").expect("Hash salt not found"), } } - pub async fn get_apps(&self) -> Vec { + pub async fn get_apps(&self) -> Result, Error> { self.get_collection(&self.apps).await } @@ -154,17 +156,17 @@ impl Core { .unwrap(); } - pub async fn get_reviews(&self, app_id: &String) -> Vec { + pub async fn get_reviews(&self, app_id: &String) -> Result, Error> { self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id}) .await } - pub async fn get_apps_by_tag(&self, info: &AppTags) -> Vec { + pub async fn get_apps_by_tag(&self, info: &AppTags) -> Result, Error> { self.get_collection_with_params(&self.apps, doc! {"tags":{"$in":&info.tags}}) .await } - pub async fn get_versions(&self, app_id: &String) -> Vec { + pub async fn get_versions(&self, app_id: &String) -> Result, Error> { self.get_collection_with_params_and_sort( &self.apps_versions, doc! {"app_id":app_id}, @@ -559,40 +561,43 @@ impl Core { collection: &Collection, params: Document, sort_params: Document, - ) -> Vec { + ) -> Result, Error> { 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![], + Err(e) => return Err(e), }; - cursor.try_collect().await.unwrap_or_else(|_| vec![]) + Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) } async fn get_collection_with_params( &self, collection: &Collection, params: Document, - ) -> Vec { + ) -> Result, Error> { let options = FindOptions::builder().projection(doc! {"_id" : 0}).build(); let cursor = match collection.find(params, options).await { Ok(cursor) => cursor, - Err(_) => return vec![], + Err(e) => return Err(e), }; - cursor.try_collect().await.unwrap_or_else(|_| vec![]) + Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) } - async fn get_collection(&self, collection: &Collection) -> Vec { + async fn get_collection( + &self, + collection: &Collection, + ) -> Result, Error> { let options = FindOptions::builder().projection(doc! {"_id" : 0}).build(); let cursor = match collection.find(None, options).await { Ok(cursor) => cursor, - Err(_) => return vec![], + Err(e) => return Err(e), }; - cursor.try_collect().await.unwrap_or_else(|_| vec![]) + Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) } } diff --git a/src/routes.rs b/src/routes.rs index 6df8d30..740697c 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,6 +1,8 @@ use crate::types::*; use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder}; use actix_web_grants::proc_macro::has_any_permission; +use bson::Document; +use mongodb::error::Error; #[post("/signup")] pub async fn signup(app_data: web::Data, user: web::Json) -> impl Responder { @@ -18,7 +20,7 @@ pub async fn signin( #[get("/apps")] #[has_any_permission("user", "admin")] pub async fn apps(app_data: web::Data) -> impl Responder { - HttpResponse::Ok().json(app_data.core.get_apps().await) + resolve_collection(app_data.core.get_apps().await) } #[post("/apps_by_tag")] @@ -27,7 +29,7 @@ pub async fn apps_by_tags( app_data: web::Data, info: web::Json, ) -> impl Responder { - HttpResponse::Ok().json(app_data.core.get_apps_by_tag(&info).await) + resolve_collection(app_data.core.get_apps_by_tag(&info).await) } #[get("/reviews/{app_id}")] @@ -36,7 +38,7 @@ pub async fn reviews( app_data: web::Data, app_id: web::Path, ) -> impl Responder { - HttpResponse::Ok().json(app_data.core.get_reviews(&app_id).await) + resolve_collection(app_data.core.get_reviews(&app_id).await) } #[get("/versions/{app_id}")] @@ -45,7 +47,7 @@ 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) + resolve_collection(app_data.core.get_versions(&app_id).await) } #[get("/rating/{app_id}")] @@ -166,6 +168,13 @@ fn username(req: HttpRequest) -> String { .to_string() } +fn resolve_collection(resp: Result, Error>) -> HttpResponse { + match resp { + Ok(resp) => HttpResponse::Ok().json(resp), + Err(e) => HttpResponse::InternalServerError().json(e.to_string()), + } +} + fn response(result: serde_json::Value) -> impl Responder { if result["code"] == "ok" { HttpResponse::Ok().json(result) From 039d103fa23941c6c91271d75cd4bd3d09bb11ee Mon Sep 17 00:00:00 2001 From: Artemy Date: Wed, 12 Jul 2023 09:39:37 +0300 Subject: [PATCH 2/2] fix: errors --- src/core.rs | 64 +++++++++++++++++++++++++++++++++++++++------------ src/routes.rs | 5 ++-- 2 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/core.rs b/src/core.rs index 4d80f1f..53255ad 100644 --- a/src/core.rs +++ b/src/core.rs @@ -12,8 +12,6 @@ use serde_json::json; use sha3::{Digest, Sha3_256}; use std::env; -use mongodb::error::Error; - use crate::types::*; pub struct Core { users: Collection, @@ -37,7 +35,7 @@ impl Core { salt: env::var("SALT").expect("Hash salt not found"), } } - pub async fn get_apps(&self) -> Result, Error> { + pub async fn get_apps(&self) -> Result, serde_json::Value> { self.get_collection(&self.apps).await } @@ -156,17 +154,20 @@ impl Core { .unwrap(); } - pub async fn get_reviews(&self, app_id: &String) -> Result, Error> { + pub async fn get_reviews(&self, app_id: &String) -> Result, serde_json::Value> { self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id}) .await } - pub async fn get_apps_by_tag(&self, info: &AppTags) -> Result, Error> { + pub async fn get_apps_by_tag( + &self, + info: &AppTags, + ) -> Result, serde_json::Value> { self.get_collection_with_params(&self.apps, doc! {"tags":{"$in":&info.tags}}) .await } - pub async fn get_versions(&self, app_id: &String) -> Result, Error> { + pub async fn get_versions(&self, app_id: &String) -> Result, serde_json::Value> { self.get_collection_with_params_and_sort( &self.apps_versions, doc! {"app_id":app_id}, @@ -561,43 +562,76 @@ impl Core { collection: &Collection, params: Document, sort_params: Document, - ) -> Result, Error> { + ) -> Result, serde_json::Value> { let options = FindOptions::builder() .projection(doc! {"_id" : 0}) .sort(sort_params) .build(); let cursor = match collection.find(params, options).await { Ok(cursor) => cursor, - Err(e) => return Err(e), + Err(_) => { + return Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })) + } }; - Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) + match cursor.try_collect().await { + Ok(collection) => Ok(collection), + Err(_) => Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })), + } } async fn get_collection_with_params( &self, collection: &Collection, params: Document, - ) -> Result, Error> { + ) -> Result, serde_json::Value> { let options = FindOptions::builder().projection(doc! {"_id" : 0}).build(); let cursor = match collection.find(params, options).await { Ok(cursor) => cursor, - Err(e) => return Err(e), + Err(_) => { + return Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })) + } }; - Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) + match cursor.try_collect().await { + Ok(collection) => Ok(collection), + Err(_) => Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })), + } } async fn get_collection( &self, collection: &Collection, - ) -> Result, Error> { + ) -> Result, serde_json::Value> { let options = FindOptions::builder().projection(doc! {"_id" : 0}).build(); let cursor = match collection.find(None, options).await { Ok(cursor) => cursor, - Err(e) => return Err(e), + Err(_) => { + return Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })) + } }; - Ok(cursor.try_collect().await.unwrap_or_else(|_| vec![])) + match cursor.try_collect().await { + Ok(collection) => Ok(collection), + Err(_) => Err(json! ({ + "code":"err", + "msg":"Error connecting to the database" + })), + } } } diff --git a/src/routes.rs b/src/routes.rs index 740697c..e860de6 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -2,7 +2,6 @@ use crate::types::*; use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder}; use actix_web_grants::proc_macro::has_any_permission; use bson::Document; -use mongodb::error::Error; #[post("/signup")] pub async fn signup(app_data: web::Data, user: web::Json) -> impl Responder { @@ -168,10 +167,10 @@ fn username(req: HttpRequest) -> String { .to_string() } -fn resolve_collection(resp: Result, Error>) -> HttpResponse { +fn resolve_collection(resp: Result, serde_json::Value>) -> HttpResponse { match resp { Ok(resp) => HttpResponse::Ok().json(resp), - Err(e) => HttpResponse::InternalServerError().json(e.to_string()), + Err(e) => HttpResponse::InternalServerError().json(e), } }