orign 0.2.3

A globally distributed container orchestrator
Documentation
use crate::models::{Datasets, V1UserProfile};
use crate::org::get_organization_names;
use crate::state::AppState;
use axum::{
    extract::Extension, extract::Json, extract::State, http::StatusCode, response::IntoResponse,
};

#[axum::debug_handler]
pub async fn list_datasets(
    State(state): State<AppState>,
    Extension(user_profile): Extension<V1UserProfile>,
) -> impl IntoResponse {
    // Collect all namespaces the user has access to
    let mut namespaces = Vec::new();

    // Add the user's handle if it exists
    if let Some(ref handle) = user_profile.handle {
        namespaces.push(handle.clone());
    } else {
        return Err((
            StatusCode::BAD_REQUEST,
            "User handle is required".to_string(),
        ));
    }

    // Add organization names
    if let Some(ref organizations) = user_profile.organizations {
        let org_names = get_organization_names(organizations);
        namespaces.extend(org_names);
    }

    // Initialize a vector to hold the dataset names
    let mut datasets = Vec::new();

    // Iterate over all namespaces and collect datasets
    for namespace in namespaces {
        // Construct the path to the datasets directory for this namespace
        let datasets_path = format!("/orign/{}/datasets", namespace);

        // Read the directories in the datasets path
        match tokio::fs::read_dir(&datasets_path).await {
            Ok(mut dir) => {
                while let Ok(Some(entry)) = dir.next_entry().await {
                    let file_type = match entry.file_type().await {
                        Ok(ft) => ft,
                        Err(err) => {
                            // Handle the error (e.g., skip this entry or return an error)
                            return Err((
                                StatusCode::INTERNAL_SERVER_ERROR,
                                format!("Failed to get file type: {}", err),
                            ));
                        }
                    };
                    if file_type.is_dir() {
                        if let Some(name) = entry.file_name().to_str() {
                            datasets.push(name.to_string());
                        }
                    }
                }
            }
            Err(_) => {
                // If the directory doesn't exist, skip this namespace
                continue;
            }
        }
    }

    // Return the list of datasets
    Ok(Json(Datasets { datasets }))
}