use axum::Json;
use axum::http::StatusCode;
use serde::Serialize;
use serde_json::{Value, json};
pub fn data_response<T: Serialize>(item: T) -> Json<Value> {
Json(json!({ "data": item }))
}
pub fn created_response<T: Serialize>(item: T) -> (StatusCode, Json<Value>) {
(StatusCode::CREATED, Json(json!({ "data": item })))
}
pub fn paginated_response<T: Serialize>(
data: Vec<T>,
total: i64,
limit: i64,
offset: i64,
) -> Json<Value> {
Json(json!({
"data": data,
"total": total,
"limit": limit,
"offset": offset,
}))
}
pub fn paginated_into<T, R, E>(
result: crate::storage::repositories::workflows::PaginatedResult<T>,
map_fn: impl Fn(&T) -> Result<R, E>,
) -> Result<Json<Value>, E>
where
R: Serialize,
{
let data: Vec<R> = result.data.iter().map(map_fn).collect::<Result<_, _>>()?;
Ok(paginated_response(
data,
result.total,
result.limit,
result.offset,
))
}