1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::fmt;

/// Generate a query string.
///
/// ## Usage
///
/// ```rust
/// # use collaboflow_rs::{Query};
///
/// let query = Query::builder()
///     .app_cd(1)
///     .offset(0)
///     .limit(10);
/// ```
///
/// ## Notes
///
/// Query `fields` is not supported.
#[derive(Debug, Clone, Default)]
pub struct Query {
    pub app_cd: Option<i32>,
    pub offset: Option<i32>,
    pub limit: Option<i32>,
    pub current: Option<bool>,
    pub category_id: Option<i32>,
    pub detail: Option<bool>,
    pub key: Option<String>,
}

impl Query {
    pub fn builder() -> Self {
        Self {
            app_cd: None,
            offset: None,
            limit: None,
            current: None,
            category_id: None,
            detail: None,
            key: None,
        }
    }

    pub fn to_queries(&self) -> Vec<(String, String)> {
        let mut queries: Vec<(String, String)> = Vec::new();
        if let Some(app_cd) = self.app_cd {
            queries.push(("app_cd".to_string(), app_cd.to_string()));
        }
        if let Some(offset) = self.offset {
            queries.push(("offset".to_string(), offset.to_string()));
        }
        if let Some(limit) = self.limit {
            queries.push(("limit".to_string(), limit.to_string()));
        }
        if let Some(current) = self.current {
            queries.push(("current".to_string(), current.to_string()));
        }
        if let Some(category_id) = self.category_id {
            queries.push(("category_id".to_string(), category_id.to_string()));
        }
        if let Some(detail) = self.detail {
            queries.push(("detail".to_string(), detail.to_string()));
        }
        if let Some(key) = &self.key {
            queries.push(("key".to_string(), key.to_string()));
        }

        queries
    }

    pub fn app_cd(mut self, app_cd: i32) -> Self {
        self.app_cd = Some(app_cd);
        self
    }

    pub fn offset(mut self, offset: i32) -> Self {
        self.offset = Some(offset);
        self
    }

    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn current(mut self, current: bool) -> Self {
        self.current = Some(current);
        self
    }

    pub fn category_id(mut self, category_id: i32) -> Self {
        self.category_id = Some(category_id);
        self
    }

    pub fn detail(mut self, detail: bool) -> Self {
        self.detail = Some(detail);
        self
    }

    pub fn key(mut self, key: &str) -> Self {
        self.key = Some(key.to_string());
        self
    }
}

impl fmt::Display for Query {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut query_string = "".to_string();
        if let Some(app_cd) = self.app_cd {
            query_string = format!("app_cd={}", app_cd);
        }

        if let Some(offset) = self.offset {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("offset={}", offset).as_str();
        }

        if let Some(limit) = self.limit {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("limit={}", limit).as_str();
        }

        if let Some(current) = self.current {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("current={}", current).as_str();
        }

        if let Some(category_id) = self.category_id {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("category_id={}", category_id).as_str();
        }

        if let Some(detail) = self.detail {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("detail={}", detail).as_str();
        }

        if let Some(key) = &self.key {
            if !query_string.is_empty() {
                query_string += "&"
            }
            query_string += format!("key={}", key).as_str();
        }

        write!(f, "{}", query_string)
    }
}