mirror of
https://github.com/OSMA-D/osma-server.git
synced 2024-11-22 05:06:21 +03:00
fix: errors
This commit is contained in:
parent
c9675fa971
commit
039d103fa2
2 changed files with 51 additions and 18 deletions
64
src/core.rs
64
src/core.rs
|
@ -12,8 +12,6 @@ use serde_json::json;
|
||||||
use sha3::{Digest, Sha3_256};
|
use sha3::{Digest, Sha3_256};
|
||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
use mongodb::error::Error;
|
|
||||||
|
|
||||||
use crate::types::*;
|
use crate::types::*;
|
||||||
pub struct Core {
|
pub struct Core {
|
||||||
users: Collection<Document>,
|
users: Collection<Document>,
|
||||||
|
@ -37,7 +35,7 @@ impl Core {
|
||||||
salt: env::var("SALT").expect("Hash salt not found"),
|
salt: env::var("SALT").expect("Hash salt not found"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub async fn get_apps(&self) -> Result<Vec<Document>, Error> {
|
pub async fn get_apps(&self) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
self.get_collection(&self.apps).await
|
self.get_collection(&self.apps).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,17 +154,20 @@ impl Core {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_reviews(&self, app_id: &String) -> Result<Vec<Document>, Error> {
|
pub async fn get_reviews(&self, app_id: &String) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id})
|
self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_apps_by_tag(&self, info: &AppTags) -> Result<Vec<Document>, Error> {
|
pub async fn get_apps_by_tag(
|
||||||
|
&self,
|
||||||
|
info: &AppTags,
|
||||||
|
) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
self.get_collection_with_params(&self.apps, doc! {"tags":{"$in":&info.tags}})
|
self.get_collection_with_params(&self.apps, doc! {"tags":{"$in":&info.tags}})
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_versions(&self, app_id: &String) -> Result<Vec<Document>, Error> {
|
pub async fn get_versions(&self, app_id: &String) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
self.get_collection_with_params_and_sort(
|
self.get_collection_with_params_and_sort(
|
||||||
&self.apps_versions,
|
&self.apps_versions,
|
||||||
doc! {"app_id":app_id},
|
doc! {"app_id":app_id},
|
||||||
|
@ -561,43 +562,76 @@ impl Core {
|
||||||
collection: &Collection<Document>,
|
collection: &Collection<Document>,
|
||||||
params: Document,
|
params: Document,
|
||||||
sort_params: Document,
|
sort_params: Document,
|
||||||
) -> Result<Vec<Document>, Error> {
|
) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
let options = FindOptions::builder()
|
let options = FindOptions::builder()
|
||||||
.projection(doc! {"_id" : 0})
|
.projection(doc! {"_id" : 0})
|
||||||
.sort(sort_params)
|
.sort(sort_params)
|
||||||
.build();
|
.build();
|
||||||
let cursor = match collection.find(params, options).await {
|
let cursor = match collection.find(params, options).await {
|
||||||
Ok(cursor) => cursor,
|
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(
|
async fn get_collection_with_params(
|
||||||
&self,
|
&self,
|
||||||
collection: &Collection<Document>,
|
collection: &Collection<Document>,
|
||||||
params: Document,
|
params: Document,
|
||||||
) -> Result<Vec<Document>, Error> {
|
) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
let options = FindOptions::builder().projection(doc! {"_id" : 0}).build();
|
let options = FindOptions::builder().projection(doc! {"_id" : 0}).build();
|
||||||
let cursor = match collection.find(params, options).await {
|
let cursor = match collection.find(params, options).await {
|
||||||
Ok(cursor) => cursor,
|
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(
|
async fn get_collection(
|
||||||
&self,
|
&self,
|
||||||
collection: &Collection<Document>,
|
collection: &Collection<Document>,
|
||||||
) -> Result<Vec<Document>, Error> {
|
) -> Result<Vec<Document>, serde_json::Value> {
|
||||||
let options = FindOptions::builder().projection(doc! {"_id" : 0}).build();
|
let options = FindOptions::builder().projection(doc! {"_id" : 0}).build();
|
||||||
let cursor = match collection.find(None, options).await {
|
let cursor = match collection.find(None, options).await {
|
||||||
Ok(cursor) => cursor,
|
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"
|
||||||
|
})),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ use crate::types::*;
|
||||||
use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder};
|
use actix_web::{get, post, web, HttpRequest, HttpResponse, Responder};
|
||||||
use actix_web_grants::proc_macro::has_any_permission;
|
use actix_web_grants::proc_macro::has_any_permission;
|
||||||
use bson::Document;
|
use bson::Document;
|
||||||
use mongodb::error::Error;
|
|
||||||
|
|
||||||
#[post("/signup")]
|
#[post("/signup")]
|
||||||
pub async fn signup(app_data: web::Data<crate::AppState>, user: web::Json<User>) -> impl Responder {
|
pub async fn signup(app_data: web::Data<crate::AppState>, user: web::Json<User>) -> impl Responder {
|
||||||
|
@ -168,10 +167,10 @@ fn username(req: HttpRequest) -> String {
|
||||||
.to_string()
|
.to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn resolve_collection(resp: Result<Vec<Document>, Error>) -> HttpResponse {
|
fn resolve_collection(resp: Result<Vec<Document>, serde_json::Value>) -> HttpResponse {
|
||||||
match resp {
|
match resp {
|
||||||
Ok(resp) => HttpResponse::Ok().json(resp),
|
Ok(resp) => HttpResponse::Ok().json(resp),
|
||||||
Err(e) => HttpResponse::InternalServerError().json(e.to_string()),
|
Err(e) => HttpResponse::InternalServerError().json(e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue