mirror of
https://github.com/OSMA-D/osma-server.git
synced 2024-12-18 00:53:46 +03:00
feat: get rating of app method (OS-21)
This commit is contained in:
parent
befd6db2e2
commit
f467472fab
3 changed files with 61 additions and 0 deletions
51
src/core.rs
51
src/core.rs
|
@ -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> {
|
||||
self.get_collection_with_params(&self.reviews, doc! {"app_name_id":app_name_id})
|
||||
.await
|
||||
|
|
|
@ -83,6 +83,7 @@ async fn main() -> std::io::Result<()> {
|
|||
.service(routes::apps)
|
||||
.service(routes::app)
|
||||
.service(routes::reviews)
|
||||
.service(routes::rating)
|
||||
.service(routes::update)
|
||||
.service(routes::change_password)
|
||||
.service(routes::write_review),
|
||||
|
|
|
@ -30,6 +30,15 @@ pub async fn reviews(
|
|||
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}")]
|
||||
#[has_any_permission("user", "admin")]
|
||||
pub async fn app(app_data: web::Data<crate::AppState>, name: web::Path<String>) -> impl Responder {
|
||||
|
|
Loading…
Add table
Reference in a new issue