ordinary-api 0.6.0-pre.13

API server for Ordinary
Documentation
// Copyright (C) 2026 Ordinary Labs, LLC.
//
// SPDX-License-Identifier: AGPL-3.0-only

use crate::server::APPLICATION;
use axum::Json;
use axum::extract::{Query, State};
use axum::http::{HeaderMap, StatusCode};
use axum::response::IntoResponse;
use ordinary_types::ContentObject;
use serde::Deserialize;
use std::sync::Arc;
use tracing::Instrument;
use utoipa::IntoParams;

#[derive(Deserialize, IntoParams)]
pub struct Params {
    /// project domain
    d: String,
}

#[utoipa::path(
    put,
    path = "/content",
    tag = APPLICATION,
    request_body(content = Vec<ContentObject>, content_type = "application/json"),
    params(Params),
    responses(
        (status = 401, description = "unauthorized for operation"),
        (status = 200, description = "update content for an application"),
    ),
    security(
        ("access" = []),
    ),
)]
pub async fn update(
    State(state): State<Arc<crate::server::OrdinaryApiServerState>>,
    Query(Params { d }): Query<Params>,
    headers: HeaderMap,
    Json(objects): Json<Vec<ContentObject>>,
) -> impl IntoResponse {
    let domain = d;

    let span = tracing::info_span!("app", %domain);
    let span = span.in_scope(|| tracing::info_span!("content"));
    let span = span.in_scope(|| tracing::info_span!("update"));

    async {
        let account = match crate::server::check_ordinary_auth(&state, &headers, 3, &domain) {
            Ok(account) => account,
            Err(code) => return code.into_response(),
        };

        tracing::info!(account, "updating");

        let apps = state.apps.read().await;

        if let Some(wrapped_app) = apps.get(&domain) {
            if let Err(err) = wrapped_app.app.update_content(&objects).await {
                tracing::error!(%err, "failed to update content");
            } else {
                tracing::info!("content updated");
                return StatusCode::OK.into_response();
            }
        }

        StatusCode::INTERNAL_SERVER_ERROR.into_response()
    }
    .instrument(span)
    .await
}