summaryrefslogblamecommitdiff
path: root/src/handlers/colonies.rs
blob: 37ec65aff665455204ae2c77e1c474a1141c2eae (plain) (tree)













































                                                                                     
use std::sync::Arc;

use axum::{Extension, Json};
use sqlx::query_as;

use crate::{
    errors::{JsonResult, ServiceError, StringResult},
    State,
};

use solarlib::colony::{User, UserData};

#[derive(Debug, Clone)]
pub struct DbUser {
    pub id: i32,
    pub name: String,
    pub groups: Vec<String>,
    pub ssh_authorized_keys: Vec<String>,
}

impl From<DbUser> for User {
    fn from(o: DbUser) -> Self {
        Self {
            name: o.name,
            groups: o.groups,
            ssh_authorized_keys: o.ssh_authorized_keys,
        }
    }
}

pub async fn list_users(state: Extension<Arc<State>>) -> JsonResult<Json<UserData>> {
    let mut conn = state.conn.acquire().await?;

    let db_users = query_as!(DbUser, "SELECT * FROM seed_users")
        .fetch_all(&mut conn)
        .await?;

    let users = db_users
        .into_iter()
        .map(|u| u.into())
        .collect::<Vec<User>>();

    let data = UserData { users };

    Ok(Json(data))
}