diff --git a/src/core.rs b/src/core.rs index 1631cf6..ddbd26f 100644 --- a/src/core.rs +++ b/src/core.rs @@ -37,6 +37,33 @@ impl Core { self.get_collection(&self.apps).await } + pub async fn update_user(&self, name: &String, info: &Json) -> 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 { diff --git a/src/main.rs b/src/main.rs index 7fa6433..9b60091 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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") diff --git a/src/routes.rs b/src/routes.rs index 65996f6..175957d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -20,6 +20,30 @@ pub async fn signin( pub async fn apps(app_data: web::Data) -> 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, + update_info: web::Json, + 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) diff --git a/src/types.rs b/src/types.rs index 55d8f6e..aba51e1 100644 --- a/src/types.rs +++ b/src/types.rs @@ -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, +} +