feat: get apps versions method

OS-22
This commit is contained in:
Artemy 2022-07-28 13:01:19 +03:00
parent ba25d9a398
commit c0cb672750
3 changed files with 41 additions and 2 deletions

View file

@ -16,6 +16,7 @@ use crate::types::*;
pub struct Core {
users: Collection<Document>,
apps: Collection<Document>,
apps_versions: Collection<Document>,
reviews: Collection<Document>,
personal_libraries: Collection<Document>,
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<Document> {
self.get_collection_with_params(&self.reviews, doc! {"app_name_id":app_name_id})
pub async fn get_reviews(&self, app_id: &String) -> Vec<Document> {
self.get_collection_with_params(&self.reviews, doc! {"app_id":app_id})
.await
}
pub async fn get_versions(&self, app_id: &String) -> Vec<Document> {
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<ReviewData>) -> 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<Document>,
params: Document,
sort_params: Document,
) -> Vec<Document> {
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<Document>,

View file

@ -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)

View file

@ -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<crate::AppState>,
app_id: web::Path<String>,
) -> 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(