mirror of
https://github.com/OSMA-D/osma-server.git
synced 2024-11-24 05:46:23 +03:00
feat: get apps versions method
OS-22
This commit is contained in:
parent
ba25d9a398
commit
c0cb672750
3 changed files with 41 additions and 2 deletions
33
src/core.rs
33
src/core.rs
|
@ -16,6 +16,7 @@ use crate::types::*;
|
||||||
pub struct Core {
|
pub struct Core {
|
||||||
users: Collection<Document>,
|
users: Collection<Document>,
|
||||||
apps: Collection<Document>,
|
apps: Collection<Document>,
|
||||||
|
apps_versions: Collection<Document>,
|
||||||
reviews: Collection<Document>,
|
reviews: Collection<Document>,
|
||||||
personal_libraries: Collection<Document>,
|
personal_libraries: Collection<Document>,
|
||||||
jwt_secret: String,
|
jwt_secret: String,
|
||||||
|
@ -27,6 +28,7 @@ impl Core {
|
||||||
Core {
|
Core {
|
||||||
users: db.collection("users"),
|
users: db.collection("users"),
|
||||||
apps: db.collection("apps"),
|
apps: db.collection("apps"),
|
||||||
|
apps_versions: db.collection("apps_versions"),
|
||||||
reviews: db.collection("reviews"),
|
reviews: db.collection("reviews"),
|
||||||
personal_libraries: db.collection("personal_libraries"),
|
personal_libraries: db.collection("personal_libraries"),
|
||||||
jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET not found"),
|
jwt_secret: env::var("JWT_SECRET").expect("JWT_SECRET not found"),
|
||||||
|
@ -125,8 +127,17 @@ impl Core {
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_reviews(&self, app_name_id: &String) -> Vec<Document> {
|
pub async fn get_reviews(&self, app_id: &String) -> Vec<Document> {
|
||||||
self.get_collection_with_params(&self.reviews, doc! {"app_name_id":app_name_id})
|
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
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -469,6 +480,24 @@ impl Core {
|
||||||
format!("{:x}", hash)
|
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(
|
async fn get_collection_with_params(
|
||||||
&self,
|
&self,
|
||||||
collection: &Collection<Document>,
|
collection: &Collection<Document>,
|
||||||
|
|
|
@ -84,6 +84,7 @@ async fn main() -> std::io::Result<()> {
|
||||||
.service(routes::app)
|
.service(routes::app)
|
||||||
.service(routes::reviews)
|
.service(routes::reviews)
|
||||||
.service(routes::rating)
|
.service(routes::rating)
|
||||||
|
.service(routes::versions)
|
||||||
.service(routes::update)
|
.service(routes::update)
|
||||||
.service(routes::change_password)
|
.service(routes::change_password)
|
||||||
.service(routes::write_review)
|
.service(routes::write_review)
|
||||||
|
|
|
@ -30,6 +30,15 @@ pub async fn reviews(
|
||||||
HttpResponse::Ok().json(app_data.core.get_reviews(&app_id).await)
|
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}")]
|
#[get("/rating/{app_id}")]
|
||||||
#[has_any_permission("user", "admin")]
|
#[has_any_permission("user", "admin")]
|
||||||
pub async fn rating(
|
pub async fn rating(
|
||||||
|
|
Loading…
Add table
Reference in a new issue