use export::http::{Json, Path, Query, State}; use export::rest::AppResponseData; use serde::Deserialize; use utoipa::IntoParams; use crate::errors::{ApiError, GitApiError}; use crate::service::GitService; use crate::service::licenses::{LicenseDto, UpsertLicenseCmd}; #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct LicenseNamePath { pub name: String, } #[derive(Debug, Clone, Deserialize, IntoParams)] pub struct LicenseListQuery { pub limit: Option, pub offset: Option, } #[utoipa::path( get, path = "/licenses", tag = "licenses", description = "分页列出系统登记的许可证。", params(LicenseListQuery), responses( (status = 200, description = "list licenses", body = AppResponseData>) ) )] pub async fn list_licenses( State(service): State, Query(q): Query, ) -> Result>, ApiError> { let limit = q .limit .unwrap_or(export::pagination::DEFAULT_PAGE_LIMIT) .clamp(1, export::pagination::MAX_PAGE_LIMIT); let offset = q .offset .unwrap_or(0) .clamp(0, export::pagination::MAX_OFFSET); let out = service .list_licenses(limit, offset) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( get, path = "/licenses/{name}", tag = "licenses", description = "按名称读取许可证全文与登记信息。", params(LicenseNamePath), responses( (status = 200, description = "get license", body = AppResponseData) ) )] pub async fn get_license( State(service): State, Path(p): Path, ) -> Result, ApiError> { let out = service .get_license(&p.name) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) } #[utoipa::path( put, path = "/licenses/{name}", tag = "licenses", description = "由内部服务登记或更新许可证定义。", params(LicenseNamePath), request_body = UpsertLicenseCmd, responses( (status = 200, description = "upsert license", body = AppResponseData) ) )] pub async fn upsert_license( State(service): State, Path(p): Path, Json(cmd): Json, ) -> Result, ApiError> { let out = service .upsert_license(&p.name, cmd) .await .map_err(GitApiError::from)?; Ok(AppResponseData::ok(out)) }