From 204dbc1f887f42e4a5ea734895ef36c5c434890f 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 --- Cargo.lock | 2 +- src/handlers.rs | 29 ++++++++++++++++++++--------- src/routes.rs | 9 +++++++++ templates/css.html | 27 +++++++++++++++++++++------ templates/header.html | 5 ++--- templates/list.html | 15 +++++++++++++++ 6 files changed, 68 insertions(+), 19 deletions(-) create mode 100644 templates/list.html diff --git a/Cargo.lock b/Cargo.lock index 5538eee..82a3df2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -442,7 +442,7 @@ dependencies = [ [[package]] name = "gradecoin" -version = "0.1.0" +version = "0.2.0" dependencies = [ "aes", "askama", 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 { - title: &'a str, - body: &'a str, +#[template(path = "list.html")] +struct UserTemplate<'a> { + users: &'a Vec, } -pub async fn welcome_handler() -> Result { - let template = WelcomeTemplate { - title: "Welcome", - body: "To The Bookstore!", - }; +struct DisplayUsers { + fingerprint: String, + balance: i32, +} + +pub async fn user_list_handler(db: Db) -> Result { + 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 impl Filter + 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 + Clone { warp::path!("register") diff --git a/templates/css.html b/templates/css.html index c9d54e3..a918a4b 100644 --- a/templates/css.html +++ b/templates/css.html @@ -1,8 +1,23 @@ diff --git a/templates/header.html b/templates/header.html index fffbefe..a142fad 100644 --- a/templates/header.html +++ b/templates/header.html @@ -1,11 +1,10 @@ - Bookstore + Gradecoin {% include "css.html" %}
-

Bookstore

+

Registered Users

- {% include "menu.html" %}
diff --git a/templates/list.html b/templates/list.html new file mode 100644 index 0000000..e63c6be --- /dev/null +++ b/templates/list.html @@ -0,0 +1,15 @@ +{% include "header.html" %} + + + + + {% for user in users %} + + + + + {% endfor %} +
Fingerprint + Balance +
{{ user.fingerprint }}{{ user.balance }}
+{% include "footer.html" %} -- cgit v1.2.3-70-g09d2