feat: get rating of app method (OS-21)

This commit is contained in:
Artemy 2022-07-26 20:42:16 +03:00
parent befd6db2e2
commit f467472fab
3 changed files with 61 additions and 0 deletions

View file

@ -61,6 +61,57 @@ impl Core {
} }
} }
pub async fn get_rating(&self, app_name_id: &String) -> serde_json::Value {
let result = self
.reviews
.aggregate(
[
doc! {"$match":{"app_name_id":app_name_id}},
doc! {
"$group": {
"_id": "$app_name_id",
"rating": {
"$avg": "$score"
},
},
},
],
None,
)
.await;
match result {
Ok(mut result) => match result.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":"This app does not exist"
})
}
},
Err(_) => {
json! ({
"code":"err",
"msg":"Unknown error"
})
}
}
}
pub async fn get_reviews(&self, app_name_id: &String) -> Vec<Document> { 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}) self.get_collection_with_params(&self.reviews, doc! {"app_name_id":app_name_id})
.await .await

View file

@ -83,6 +83,7 @@ async fn main() -> std::io::Result<()> {
.service(routes::apps) .service(routes::apps)
.service(routes::app) .service(routes::app)
.service(routes::reviews) .service(routes::reviews)
.service(routes::rating)
.service(routes::update) .service(routes::update)
.service(routes::change_password) .service(routes::change_password)
.service(routes::write_review), .service(routes::write_review),

View file

@ -30,6 +30,15 @@ pub async fn reviews(
HttpResponse::Ok().json(app_data.core.get_reviews(&app_name_id).await) HttpResponse::Ok().json(app_data.core.get_reviews(&app_name_id).await)
} }
#[get("/rating/{app_name_id}")]
#[has_any_permission("user", "admin")]
pub async fn rating(
app_data: web::Data<crate::AppState>,
app_name_id: web::Path<String>,
) -> impl Responder {
response(app_data.core.get_rating(&app_name_id).await)
}
#[get("/app/{name}")] #[get("/app/{name}")]
#[has_any_permission("user", "admin")] #[has_any_permission("user", "admin")]
pub async fn app(app_data: web::Data<crate::AppState>, name: web::Path<String>) -> impl Responder { pub async fn app(app_data: web::Data<crate::AppState>, name: web::Path<String>) -> impl Responder {