1use std::fmt;
2
3#[derive(Debug, Clone, Default)]
16pub struct Query {
17 pub app_cd: Option<i32>,
18 pub offset: Option<i32>,
19 pub limit: Option<i32>,
20 pub current: Option<bool>,
21 pub category_id: Option<i32>,
22 pub detail: Option<bool>,
23 pub key: Option<String>,
24 pub fields: Option<Vec<String>>,
25}
26
27impl Query {
28 pub fn builder() -> Self {
29 Self {
30 app_cd: None,
31 offset: None,
32 limit: None,
33 current: None,
34 category_id: None,
35 detail: None,
36 key: None,
37 fields: None,
38 }
39 }
40
41 pub fn to_queries(&self) -> Vec<(String, String)> {
42 let mut queries: Vec<(String, String)> = Vec::new();
43 if let Some(app_cd) = self.app_cd {
44 queries.push(("app_cd".to_string(), app_cd.to_string()));
45 }
46 if let Some(offset) = self.offset {
47 queries.push(("offset".to_string(), offset.to_string()));
48 }
49 if let Some(limit) = self.limit {
50 queries.push(("limit".to_string(), limit.to_string()));
51 }
52 if let Some(current) = self.current {
53 queries.push(("current".to_string(), current.to_string()));
54 }
55 if let Some(category_id) = self.category_id {
56 queries.push(("category_id".to_string(), category_id.to_string()));
57 }
58 if let Some(detail) = self.detail {
59 queries.push(("detail".to_string(), detail.to_string()));
60 }
61 if let Some(key) = &self.key {
62 queries.push(("key".to_string(), key.to_string()));
63 }
64 if let Some(fields) = self.fields.clone() {
65 queries.push(("fields".to_string(), fields.join(",")));
66 }
67
68 queries
69 }
70
71 pub fn app_cd(mut self, app_cd: i32) -> Self {
72 self.app_cd = Some(app_cd);
73 self
74 }
75
76 pub fn offset(mut self, offset: i32) -> Self {
77 self.offset = Some(offset);
78 self
79 }
80
81 pub fn limit(mut self, limit: i32) -> Self {
82 self.limit = Some(limit);
83 self
84 }
85
86 pub fn current(mut self, current: bool) -> Self {
87 self.current = Some(current);
88 self
89 }
90
91 pub fn category_id(mut self, category_id: i32) -> Self {
92 self.category_id = Some(category_id);
93 self
94 }
95
96 pub fn detail(mut self, detail: bool) -> Self {
97 self.detail = Some(detail);
98 self
99 }
100
101 pub fn key(mut self, key: &str) -> Self {
102 self.key = Some(key.to_string());
103 self
104 }
105
106 pub fn fields(mut self, fields: Vec<String>) -> Self {
107 self.fields = Some(fields);
108 self
109 }
110}
111
112impl fmt::Display for Query {
113 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 let mut query_string = "".to_string();
115 if let Some(app_cd) = self.app_cd {
116 query_string = format!("app_cd={}", app_cd);
117 }
118
119 if let Some(offset) = self.offset {
120 if !query_string.is_empty() {
121 query_string += "&"
122 }
123 query_string += format!("offset={}", offset).as_str();
124 }
125
126 if let Some(limit) = self.limit {
127 if !query_string.is_empty() {
128 query_string += "&"
129 }
130 query_string += format!("limit={}", limit).as_str();
131 }
132
133 if let Some(current) = self.current {
134 if !query_string.is_empty() {
135 query_string += "&"
136 }
137 query_string += format!("current={}", current).as_str();
138 }
139
140 if let Some(category_id) = self.category_id {
141 if !query_string.is_empty() {
142 query_string += "&"
143 }
144 query_string += format!("category_id={}", category_id).as_str();
145 }
146
147 if let Some(detail) = self.detail {
148 if !query_string.is_empty() {
149 query_string += "&"
150 }
151 query_string += format!("detail={}", detail).as_str();
152 }
153
154 if let Some(key) = &self.key {
155 if !query_string.is_empty() {
156 query_string += "&"
157 }
158 query_string += format!("key={}", key).as_str();
159 }
160
161 if let Some(fields) = &self.fields {
162 let mut fields_string = "".to_string();
163 for field in fields.iter() {
164 if !fields_string.is_empty() {
165 fields_string += ","
166 }
167 fields_string += field.as_str();
168 }
169
170 if !query_string.is_empty() {
171 query_string += "&"
172 }
173 query_string += format!("fields={}", fields_string).as_str();
174 }
175
176 write!(f, "{}", query_string)
177 }
178}