feat: write review method (OS-12)

This commit is contained in:
Artemy 2022-07-26 20:38:06 +03:00
parent 38589ae0c3
commit 699f84e440
4 changed files with 74 additions and 0 deletions

View file

@ -37,6 +37,62 @@ impl Core {
self.get_collection(&self.apps).await self.get_collection(&self.apps).await
} }
pub async fn write_review(&self, name: &String, info: &Json<ReviewData>) -> serde_json::Value {
let options = FindOneOptions::builder()
.projection(doc! {"_id" : 1})
.build();
let response = self
.apps
.find_one(doc! {"name_id":&info.app_name_id}, options)
.await;
match response {
Ok(response) => match response {
Some(_) => {
let options = UpdateOptions::builder().upsert(Some(true)).build();
let response = self
.reviews
.update_one(
doc! {"user_name": name},
doc! {"$set": {
"app_name_id":&info.app_name_id,
"text":&info.text,
"score":&info.score,
}},
options,
)
.await;
match response {
Ok(_) => {
json! ({
"code":"ok",
"msg":"The review is written"
})
}
Err(_) => {
json! ({
"code":"err",
"msg":"Validation error"
})
}
}
}
None => {
json! ({
"code":"denied",
"msg":"This app does not exist"
})
}
},
Err(_) => {
json! ({
"code":"err",
"msg":"Unknown error"
})
}
}
}
pub async fn update_user(&self, name: &String, info: &Json<UserData>) -> serde_json::Value { pub async fn update_user(&self, name: &String, info: &Json<UserData>) -> serde_json::Value {
let response = self let response = self
.users .users

View file

@ -83,6 +83,7 @@ async fn main() -> std::io::Result<()> {
.service(routes::apps) .service(routes::apps)
.service(routes::update) .service(routes::update)
.service(routes::change_password) .service(routes::change_password)
.service(routes::write_review),
) )
.service( .service(
web::scope("/auth") web::scope("/auth")

View file

@ -20,6 +20,16 @@ pub async fn signin(
pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder { pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder {
HttpResponse::Ok().json(app_data.core.get_apps().await) HttpResponse::Ok().json(app_data.core.get_apps().await)
} }
#[get("/reviews/{app_name_id}")]
#[has_any_permission("user", "admin")]
pub async fn reviews(
app_data: web::Data<crate::AppState>,
app_name_id: web::Path<String>,
) -> impl Responder {
HttpResponse::Ok().json(app_data.core.get_reviews(&app_name_id).await)
}
#[post("/change_password")] #[post("/change_password")]
#[has_any_permission("user", "admin")] #[has_any_permission("user", "admin")]
pub async fn change_password( pub async fn change_password(

View file

@ -35,6 +35,13 @@ pub struct UserData {
pub img: String, pub img: String,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct ReviewData {
pub app_name_id: String,
pub score: i32,
pub text: String,
}
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct PasswordsInf { pub struct PasswordsInf {
pub old_password: String, pub old_password: String,