Add get invites endpoint

This commit is contained in:
ViViDboarder 2019-04-11 16:16:24 -07:00
parent f25ab42ebb
commit 4889415cae
2 changed files with 19 additions and 0 deletions

View File

@ -22,6 +22,7 @@ pub fn routes() -> Vec<Route> {
routes![
admin_login,
get_users,
get_invites,
post_admin_login,
admin_page,
invite_user,
@ -156,6 +157,14 @@ fn invite_user(data: Json<InviteData>, _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<Value> = 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);

View File

@ -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<Self> {
invitations::table.load::<Self>(&**conn).expect("Error loading invitations")
}
}