feat: add app to personal library

OS-7
This commit is contained in:
Artemy 2022-07-27 13:28:58 +03:00
parent 136ad52e7d
commit 1cebe8122d
4 changed files with 75 additions and 2 deletions

View file

@ -214,6 +214,59 @@ impl Core {
} }
} }
pub async fn add_app_to_personal_library(
&self,
name: &String,
app: &String,
) -> serde_json::Value {
let options = FindOneOptions::builder()
.projection(doc! {"_id" : 1})
.build();
let response = self.apps.find_one(doc! {"name_id":&app}, options).await;
match response {
Ok(response) => match response {
Some(_) => {
let response = self
.personal_libraries
.update_one(
doc! {"name": name},
doc! {"$push": {
"apps":&app,
}},
None,
)
.await;
match response {
Ok(_) => {
json! ({
"code":"ok",
"msg":"App added to personal library"
})
}
Err(_) => {
json! ({
"code":"err",
"msg":"App already in the library | Unknown error"
})
}
}
}
None => {
json! ({
"code":"denied",
"msg":"This app does not exist"
})
}
},
Err(_) => {
json! ({
"code":"err",
"msg":"Unknown error"
})
}
}
}
pub async fn change_password( pub async fn change_password(
&self, &self,
name: &String, name: &String,

View file

@ -86,7 +86,8 @@ async fn main() -> std::io::Result<()> {
.service(routes::rating) .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)
.service(routes::add_app_to_personal_library),
) )
.service( .service(
web::scope("/auth") web::scope("/auth")

View file

@ -75,6 +75,21 @@ pub async fn update(
) )
} }
#[post("/add_app_to_personal_library")]
#[has_any_permission("user", "admin")]
pub async fn add_app_to_personal_library(
app_data: web::Data<crate::AppState>,
app_info: web::Json<AppToAdd>,
req: HttpRequest,
) -> impl Responder {
response(
app_data
.core
.add_app_to_personal_library(&username(req), &app_info.name)
.await,
)
}
#[post("/write_review")] #[post("/write_review")]
#[has_any_permission("user", "admin")] #[has_any_permission("user", "admin")]
pub async fn write_review( pub async fn write_review(
@ -108,7 +123,6 @@ fn response(result: serde_json::Value) -> impl Responder {
} else if result["code"] == "denied" { } else if result["code"] == "denied" {
HttpResponse::Forbidden().json(result) HttpResponse::Forbidden().json(result)
} else { } else {
println!("{}", result["code"]);
HttpResponse::InternalServerError().json(result) HttpResponse::InternalServerError().json(result)
} }
} }

View file

@ -47,3 +47,8 @@ pub struct PasswordsInf {
pub old_password: String, pub old_password: String,
pub new_password: String, pub new_password: String,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct AppToAdd {
pub name: String,
}