From e7cd705175abb820cd4bfdb28693615ef2171597 Mon Sep 17 00:00:00 2001
From: Yigit Sever
Date: Fri, 16 Apr 2021 16:39:23 +0300
Subject: Add existing user get support

---
 src/handlers.rs | 29 ++++++++++++++++++++---------
 src/routes.rs   |  9 +++++++++
 2 files changed, 29 insertions(+), 9 deletions(-)

(limited to 'src')

diff --git a/src/handlers.rs b/src/handlers.rs
index ee0fbf0..c2c8aca 100644
--- a/src/handlers.rs
+++ b/src/handlers.rs
@@ -712,17 +712,28 @@ fn authorize_proposer(jwt_token: String, user_pem: &str) -> Result<TokenData<Cla
 }
 
 #[derive(Template)]
-#[template(path = "welcome.html")]
-struct WelcomeTemplate<'a> {
-    title: &'a str,
-    body: &'a str,
+#[template(path = "list.html")]
+struct UserTemplate<'a> {
+    users: &'a Vec<DisplayUsers>,
 }
 
-pub async fn welcome_handler() -> Result<impl warp::Reply, warp::Rejection> {
-    let template = WelcomeTemplate {
-        title: "Welcome",
-        body: "To The Bookstore!",
-    };
+struct DisplayUsers {
+    fingerprint: String,
+    balance: i32,
+}
+
+pub async fn user_list_handler(db: Db) -> Result<impl warp::Reply, warp::Rejection> {
+    let users = db.users.read();
+    let mut sane_users = Vec::new();
+
+    for (fingerprint, user) in users.iter() {
+        sane_users.push(DisplayUsers {
+            fingerprint: fingerprint.to_owned(),
+            balance: user.balance,
+        });
+    }
+
+    let template = UserTemplate { users: &sane_users };
     let res = template.render().unwrap();
     Ok(warp::reply::html(res))
 }
diff --git a/src/routes.rs b/src/routes.rs
index 52d357a..211f832 100644
--- a/src/routes.rs
+++ b/src/routes.rs
@@ -18,10 +18,19 @@ pub fn consensus_routes(db: Db) -> impl Filter<Extract = impl Reply, Error = Rej
         .or(register_user(db.clone()))
         .or(auth_transaction_propose(db.clone()))
         .or(auth_block_propose(db.clone()))
+        .or(list_users(db.clone()))
         .or(block_list(db))
         .or(static_route)
 }
 
+/// GET /user warp route
+pub fn list_users(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
+    warp::path!("user")
+        .and(warp::get())
+        .and(custom_filters::with_db(db))
+        .and_then(handlers::user_list_handler)
+}
+
 /// POST /register warp route
 pub fn register_user(db: Db) -> impl Filter<Extract = impl Reply, Error = Rejection> + Clone {
     warp::path!("register")
-- 
cgit v1.2.3-70-g09d2