use serde::{Deserialize, Serialize};
pub mod metadata;
pub mod schema;
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiResponse<T> {
pub success: bool,
pub data: Option<T>,
pub error: Option<ApiError>,
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct ApiError {
pub code: String,
pub message: String,
}
impl<T> ApiResponse<T> {
pub fn success(data: T) -> Self {
Self {
success: true,
data: Some(data),
error: None,
}
}
pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
Self {
success: false,
data: None,
error: Some(ApiError {
code: code.into(),
message: message.into(),
}),
}
}
}
#[derive(Debug, Deserialize, utoipa::IntoParams, utoipa::ToSchema)]
pub struct PaginationParams {
#[serde(default = "default_limit")]
pub limit: usize,
#[serde(default = "default_offset")]
pub offset: usize,
}
pub(crate) fn default_limit() -> usize {
20
}
pub(crate) fn default_offset() -> usize {
0
}
#[derive(Debug, Serialize, utoipa::ToSchema)]
pub struct PaginatedResponse<T> {
pub items: Vec<T>,
pub total: i64,
pub limit: usize,
pub offset: usize,
}
impl<T> PaginatedResponse<T> {
pub fn new(items: Vec<T>, total: i64, limit: usize, offset: usize) -> Self {
Self {
items,
total,
limit,
offset,
}
}
}