wsb 0.227.4750

Workspace - A tool suite for file operations, version management, and development workflow automation
Documentation
//! Gists: the list from the machine's mirror, a gist's contents fetched once
//! and then served from it, and edits written through GitHub.

use axum::{
    extract::{Path, State},
    Json,
};
use serde_json::json;

use crate::web::github::{self, Refusal};
use crate::web::state::WebState;
use wsb_github::client::{CreateGistRequest, UpdateGistRequest};

/// GET /api/v1/gists — the mirrored list, with how current it is.
pub async fn list_gists(State(state): State<WebState>) -> Result<Json<serde_json::Value>, Refusal> {
    let github = github::of(&state)?;
    let listed = github.service.gists().map_err(github::failure)?;
    Ok(Json(json!({ "gists": listed.items, "sync": listed.sync })))
}

/// GET /api/v1/gists/:id — one gist with its file contents.
pub async fn get_gist(
    State(state): State<WebState>,
    Path(gist_id): Path<String>,
) -> Result<Json<wsb_github::service::GistAnswer>, Refusal> {
    let github = github::of(&state)?;
    let client = github::client().ok();
    github
        .service
        .gist(client.as_ref(), &gist_id)
        .await
        .map(Json)
        .map_err(github::failure)
}

pub async fn create_gist(
    State(state): State<WebState>,
    Json(body): Json<CreateGistRequest>,
) -> Result<Json<serde_json::Value>, Refusal> {
    let github = github::of(&state)?;
    let client = github::client()?;
    let gist = github
        .service
        .create_gist(&client, &body)
        .await
        .map_err(github::failure)?;
    Ok(Json(json!({ "gist": gist })))
}

pub async fn update_gist(
    State(state): State<WebState>,
    Path(gist_id): Path<String>,
    Json(body): Json<UpdateGistRequest>,
) -> Result<Json<serde_json::Value>, Refusal> {
    let github = github::of(&state)?;
    let client = github::client()?;
    let gist = github
        .service
        .update_gist(&client, &gist_id, &body)
        .await
        .map_err(github::failure)?;
    Ok(Json(json!({ "gist": gist })))
}