medullah_web/helpers/
http.rs

1use chrono::NaiveDate;
2use chrono::NaiveDateTime;
3use ntex::web::types::Query;
4use serde::{Deserialize, Serialize};
5use uuid::Uuid;
6
7use crate::enums::app_message::AppMessage;
8
9pub type TheQueryParams = Query<QueryParams>;
10
11/// Represents common query parameters used for filtering, pagination, and sorting in API requests.
12#[derive(Deserialize, Clone, Default)]
13pub struct QueryParams {
14    /// Search term for filtering results based on relevant text.
15    ///
16    /// Example: `?search=example`
17    pub search: Option<String>,
18
19    /// The maximum number of results to return.
20    ///
21    /// Example: `?limit=50`
22    pub limit: Option<i64>,
23
24    /// The current page number for paginated results.
25    ///
26    /// Example: `?page=2`
27    pub page: Option<i64>,
28
29    /// Number of results per page.
30    ///
31    /// Example: `?per_page=20`
32    pub per_page: Option<i64>,
33
34    /// Filter results based on their status.
35    ///
36    /// Example: `?status=active`
37    pub status: Option<String>,
38
39    /// Filter results based on their stage in a process or workflow.
40    ///
41    /// Example: `?stage=pending`
42    pub stage: Option<String>,
43
44    /// Specifies the column to be used for sorting the results.
45    ///
46    /// Example: `?order_col=created_at`
47    pub order_col: Option<String>,
48
49    /// Specifies the sorting direction: `asc` (ascending) or `desc` (descending).
50    ///
51    /// Example: `?order_dir=desc`
52    pub order_dir: Option<String>,
53
54    /// Filters results by a start date (inclusive). Expected format: `YYYY-MM-DD`.
55    ///
56    /// Example: `?start_date=2024-01-01`
57    pub start_date: Option<NaiveDate>,
58
59    /// Filters results by an end date (inclusive). Expected format: `YYYY-MM-DD`.
60    ///
61    /// Example: `?end_date=2024-12-31`
62    pub end_date: Option<NaiveDate>,
63}
64
65#[derive(Deserialize)]
66pub struct IdPathParam {
67    pub id: String,
68}
69
70#[derive(Deserialize)]
71pub struct IdAsUuid {
72    pub id: Uuid,
73}
74
75#[cfg(feature = "validator")]
76#[derive(Deserialize, validator::Validate)]
77pub struct ReasonPayload {
78    #[validate(length(min = 3, max = 1500))]
79    pub reason: String,
80}
81
82impl QueryParams {
83    pub fn search(&self) -> Option<String> {
84        self.search.clone()
85    }
86
87    pub fn search_query(&self) -> String {
88        self.search.clone().unwrap_or(String::from(""))
89    }
90
91    pub fn search_query_like(&self) -> String {
92        format!("%{}%", self.search_query())
93    }
94
95    pub fn limit(&self) -> i64 {
96        self.limit.unwrap_or(10).min(150)
97    }
98
99    pub fn curr_page(&self) -> i64 {
100        self.page.unwrap_or(1)
101    }
102
103    pub fn per_page(&self) -> i64 {
104        self.per_page.unwrap_or(10).min(150)
105    }
106}
107
108#[allow(dead_code)]
109pub fn date_from_unsafe_input(date: &str, field_name: &str) -> Result<NaiveDateTime, AppMessage> {
110    NaiveDateTime::parse_from_str(format!("{} 00:00:00", date).as_str(), "%Y-%m-%d %H:%M:%S")
111        .map_err(|e| {
112            AppMessage::WarningMessageString(format!(
113                "Invalid {} input value({}), please make sure it's valid date; {}",
114                field_name, date, e
115            ))
116        })
117}
118
119#[derive(Serialize, Deserialize, Clone)]
120pub struct HttpHeaderItem {
121    pub name: String,
122    pub value: String,
123}