diff options
-rw-r--r-- | src/controllers/users_controller.rs | 31 | ||||
-rw-r--r-- | src/models/user.rs | 5 | ||||
-rw-r--r-- | templates/users.html | 16 |
3 files changed, 41 insertions, 11 deletions
diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs index 9708a1b..9bfb5e1 100644 --- a/src/controllers/users_controller.rs +++ b/src/controllers/users_controller.rs @@ -4,23 +4,32 @@ use rocket::response::{Flash, Redirect}; use utils; #[derive(BartDisplay)] -#[template = "templates/new_user.html"] -pub struct NewUserTemplate { - user: ::models::NewUser +#[template = "templates/users.html"] +pub struct UsersTemplate { + users: Vec<::models::User> } -implement_responder_for!(NewUserTemplate); +implement_responder_for!(UsersTemplate); -impl NewUserTemplate { - pub fn realname(&self) -> String { - self.user.realname.as_ref().unwrap_or(&String::new()).clone() +#[get("/", format = "text/html")] +fn index(conn: utils::DbConn) -> utils::Page<UsersTemplate> { + utils::Page { + title: String::from("Users"), + flash: None, + content: UsersTemplate { + users: ::models::User::all(conn).unwrap() + } } +} - pub fn email(&self) -> String { - self.user.email.as_ref().unwrap_or(&String::new()).clone() - } +#[derive(BartDisplay)] +#[template = "templates/new_user.html"] +pub struct NewUserTemplate { + user: ::models::NewUser } +implement_responder_for!(NewUserTemplate); + #[get("/new", format = "text/html")] fn new() -> utils::Page<NewUserTemplate> { utils::Page { @@ -41,5 +50,5 @@ fn create(user: Form<::models::NewUser>, conn: utils::DbConn) -> Flash<Redirect> } pub fn routes() -> Vec<rocket::Route> { - routes![new, create] + routes![index, new, create] } diff --git a/src/models/user.rs b/src/models/user.rs index 537dfe6..7f6fe88 100644 --- a/src/models/user.rs +++ b/src/models/user.rs @@ -13,6 +13,11 @@ pub struct User { } impl User { + pub fn all(conn: utils::DbConn) -> QueryResult<Vec<User>> { + use schema::users::dsl::*; + users.load::<User>(&*conn) + } + pub fn by_id(user_id: i32, conn: utils::DbConn) -> QueryResult<User> { use schema::users::dsl::*; users.filter(id.eq(user_id)) diff --git a/templates/users.html b/templates/users.html new file mode 100644 index 0000000..9eeb533 --- /dev/null +++ b/templates/users.html @@ -0,0 +1,16 @@ +<table> + <hr> + <td>id</td> + <td>username</td> + <td>real name</td> + <td>email</td> + </hr> + <tr> + {{# users }} + <td>{{ .id }}</td> + <td>{{ .username }}</td> + <td>{{ .realname() }}</td> + <td>{{ .email() }}</td> + {{/ users }} + </tr> +</table> |