diff --git a/src/api/admin.rs b/src/api/admin.rs index 09f1882..2cc8004 100644 --- a/src/api/admin.rs +++ b/src/api/admin.rs @@ -22,6 +22,7 @@ pub fn routes() -> Vec { routes![ admin_login, get_users, + get_invites, post_admin_login, admin_page, invite_user, @@ -156,6 +157,14 @@ fn invite_user(data: Json, _token: AdminToken, conn: DbConn) -> Empt } } +#[get("/invites")] +fn get_invites(_token: AdminToken, conn: DbConn) ->JsonResult { + let invites = Invitation::get_all(&conn); + let invites_json: Vec = invites.iter().map(|u| u.to_json(&conn)).collect(); + + Ok(Json(Value::Array(invites_json))) +} + #[get("/users")] fn get_users(_token: AdminToken, conn: DbConn) ->JsonResult { let users = User::get_all(&conn); diff --git a/src/db/models/user.rs b/src/db/models/user.rs index 91d018d..500167b 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -239,6 +239,12 @@ impl Invitation { Self { email } } + pub fn to_json(&self, conn: &DbConn) -> Value { + json!({ + "Email": self.email, + }) + } + pub fn save(&self, conn: &DbConn) -> EmptyResult { if self.email.trim().is_empty() { err!("Invitation email can't be empty") @@ -271,4 +277,8 @@ impl Invitation { None => false, } } + + pub fn get_all(conn: &DbConn) -> Vec { + invitations::table.load::(&**conn).expect("Error loading invitations") + } }