medullah_web/helpers/
http.rs1use 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#[derive(Deserialize, Clone, Default)]
13pub struct QueryParams {
14 pub search: Option<String>,
18
19 pub limit: Option<i64>,
23
24 pub page: Option<i64>,
28
29 pub per_page: Option<i64>,
33
34 pub status: Option<String>,
38
39 pub stage: Option<String>,
43
44 pub order_col: Option<String>,
48
49 pub order_dir: Option<String>,
53
54 pub start_date: Option<NaiveDate>,
58
59 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}