feat: user info update api (OS-6)

This commit is contained in:
Artemy 2022-07-26 20:35:08 +03:00
parent 57735ede3f
commit 4340c6d212
4 changed files with 60 additions and 0 deletions

View file

@ -37,6 +37,33 @@ impl Core {
self.get_collection(&self.apps).await
}
pub async fn update_user(&self, name: &String, info: &Json<UserData>) -> serde_json::Value {
let response = self
.users
.update_one(
doc! {"name": name},
doc! {"$set": {
"email":&info.email,
"img":&info.img
}},
None,
)
.await;
match response {
Ok(_) => {
json! ({
"code":"ok",
"msg":"User information updated"
})
}
Err(_) => {
json! ({
"code":"err",
"msg":"Unknown error"
})
}
}
}
pub async fn signin(&self, name: &String, password: &String) -> serde_json::Value {
let response = self.users.find_one(doc! {"name":name}, None).await;
match response {

View file

@ -81,11 +81,13 @@ async fn main() -> std::io::Result<()> {
web::scope("/api")
.wrap(HttpAuthentication::bearer(jwt_validator))
.service(routes::apps)
.service(routes::update)
)
.service(
web::scope("/auth")
.service(routes::signup)
.service(routes::signin),
)
})
.bind(("0.0.0.0", port))
.expect("Can not bind to port")

View file

@ -20,6 +20,30 @@ pub async fn signin(
pub async fn apps(app_data: web::Data<crate::AppState>) -> impl Responder {
HttpResponse::Ok().json(app_data.core.get_apps().await)
}
#[post("/update")]
#[has_any_permission("user", "admin")]
pub async fn update(
app_data: web::Data<crate::AppState>,
update_info: web::Json<UserData>,
req: HttpRequest,
) -> impl Responder {
response(
app_data
.core
.update_user(&username(req), &update_info)
.await,
)
}
fn username(req: HttpRequest) -> String {
req.headers()
.get("osma-username")
.unwrap()
.to_str()
.ok()
.unwrap()
.to_string()
}
fn response(result: serde_json::Value) -> impl Responder {
if result["code"] == "ok" {
HttpResponse::Ok().json(result)

View file

@ -28,3 +28,10 @@ pub struct JwtInfo {
pub role: String,
pub exp: i64,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UserData {
pub email: String,
pub img: String,
}