main
use api::http::{Json, Path, Query, State};
use api::pagination::{Page, PageQuery};
use api::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,
}
#[utoipa::path(
get,
path = "/licenses",
tag = "licenses",
description = "分页列出系统登记的许可证。",
params(PageQuery),
responses(
(status = 200, description = "list licenses", body = AppResponseData<Page<LicenseDto>>)
)
)]
pub async fn list_licenses(
State(service): State<GitService>,
Query(q): Query<PageQuery>,
) -> Result<AppResponseData<Page<LicenseDto>>, ApiError> {
let (limit, offset) = q.bounds()?;
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<LicenseDto>)
)
)]
pub async fn get_license(
State(service): State<GitService>,
Path(p): Path<LicenseNamePath>,
) -> Result<AppResponseData<LicenseDto>, 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<LicenseDto>)
)
)]
pub async fn upsert_license(
State(service): State<GitService>,
Path(p): Path<LicenseNamePath>,
Json(cmd): Json<UpsertLicenseCmd>,
) -> Result<AppResponseData<LicenseDto>, ApiError> {
let out = service
.upsert_license(&p.name, cmd)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}