Skip to main content

linkrs_server/web/
models.rs

1//! Web Module Data Models
2
3use serde::{Deserialize, Serialize};
4
5pub mod metadata;
6pub mod schema;
7
8/// Standard API response wrapper
9#[derive(Debug, Serialize, utoipa::ToSchema)]
10pub struct ApiResponse<T> {
11    pub success: bool,
12    pub data: Option<T>,
13    pub error: Option<ApiError>,
14}
15
16#[derive(Debug, Serialize, utoipa::ToSchema)]
17pub struct ApiError {
18    pub code: String,
19    pub message: String,
20}
21
22impl<T> ApiResponse<T> {
23    pub fn success(data: T) -> Self {
24        Self {
25            success: true,
26            data: Some(data),
27            error: None,
28        }
29    }
30
31    pub fn error(code: impl Into<String>, message: impl Into<String>) -> Self {
32        Self {
33            success: false,
34            data: None,
35            error: Some(ApiError {
36                code: code.into(),
37                message: message.into(),
38            }),
39        }
40    }
41}
42
43/// Pagination parameters
44#[derive(Debug, Deserialize, utoipa::IntoParams, utoipa::ToSchema)]
45pub struct PaginationParams {
46    #[serde(default = "default_limit")]
47    pub limit: usize,
48    #[serde(default = "default_offset")]
49    pub offset: usize,
50}
51
52pub(crate) fn default_limit() -> usize {
53    20
54}
55
56pub(crate) fn default_offset() -> usize {
57    0
58}
59
60/// Paginated response
61#[derive(Debug, Serialize, utoipa::ToSchema)]
62pub struct PaginatedResponse<T> {
63    pub items: Vec<T>,
64    pub total: i64,
65    pub limit: usize,
66    pub offset: usize,
67}
68
69impl<T> PaginatedResponse<T> {
70    pub fn new(items: Vec<T>, total: i64, limit: usize, offset: usize) -> Self {
71        Self {
72            items,
73            total,
74            limit,
75            offset,
76        }
77    }
78}