nullnet_libdatastore/builders/
get_by_filer_request_builder.rs

1use std::collections::HashMap;
2
3use crate::AdvanceFilter;
4use crate::GetByFilterBody;
5use crate::GetByFilterRequest;
6use crate::Join;
7use crate::MultipleSort;
8use crate::Params;
9
10#[derive(Debug, Default)]
11pub struct GetByFilterRequestBuilder {
12    id: Option<String>,
13    table: Option<String>,
14    pluck: Vec<String>,
15    order_by: Option<String>,
16    limit: Option<i32>,
17    offset: Option<i32>,
18    order_direction: Option<String>,
19    advance_filters: Vec<AdvanceFilter>,
20    joins: Vec<Join>,
21    multiple_sort: Vec<MultipleSort>,
22    pluck_object: HashMap<String, String>,
23    date_format: Option<String>,
24    is_root: bool,
25    is_case_sensitive_sorting: bool,
26}
27
28impl GetByFilterRequestBuilder {
29    pub fn new() -> Self {
30        Self::default()
31    }
32
33    pub fn id(mut self, id: impl Into<String>) -> Self {
34        self.id = Some(id.into());
35        self
36    }
37
38    pub fn table(mut self, table: impl Into<String>) -> Self {
39        self.table = Some(table.into());
40        self
41    }
42
43    pub fn pluck(mut self, field: impl Into<String>) -> Self {
44        self.pluck.push(field.into());
45        self
46    }
47
48    pub fn plucks(mut self, fields: impl IntoIterator<Item = impl Into<String>>) -> Self {
49        self.pluck.extend(fields.into_iter().map(Into::into));
50        self
51    }
52
53    pub fn order_by(mut self, field: impl Into<String>) -> Self {
54        self.order_by = Some(field.into());
55        self
56    }
57
58    pub fn limit(mut self, limit: i32) -> Self {
59        self.limit = Some(limit);
60        self
61    }
62
63    pub fn offset(mut self, offset: i32) -> Self {
64        self.offset = Some(offset);
65        self
66    }
67
68    pub fn order_direction(mut self, direction: impl Into<String>) -> Self {
69        self.order_direction = Some(direction.into());
70        self
71    }
72
73    pub fn advance_filter(mut self, filter: AdvanceFilter) -> Self {
74        self.advance_filters.push(filter);
75        self
76    }
77
78    pub fn advance_filters(mut self, filters: impl IntoIterator<Item = AdvanceFilter>) -> Self {
79        self.advance_filters.extend(filters);
80        self
81    }
82
83    pub fn join(mut self, join: Join) -> Self {
84        self.joins.push(join);
85        self
86    }
87
88    pub fn joins(mut self, joins: impl IntoIterator<Item = Join>) -> Self {
89        self.joins.extend(joins);
90        self
91    }
92
93    pub fn multiple_sort(mut self, sort: MultipleSort) -> Self {
94        self.multiple_sort.push(sort);
95        self
96    }
97
98    pub fn multiple_sorts(mut self, sorts: impl IntoIterator<Item = MultipleSort>) -> Self {
99        self.multiple_sort.extend(sorts);
100        self
101    }
102
103    pub fn pluck_object(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
104        self.pluck_object.insert(key.into(), value.into());
105        self
106    }
107
108    pub fn pluck_objects(
109        mut self,
110        entries: impl IntoIterator<Item = (impl Into<String>, impl Into<String>)>,
111    ) -> Self {
112        for (k, v) in entries {
113            self.pluck_object.insert(k.into(), v.into());
114        }
115        self
116    }
117
118    pub fn date_format(mut self, format: impl Into<String>) -> Self {
119        self.date_format = Some(format.into());
120        self
121    }
122
123    pub fn performed_by_root(mut self, value: bool) -> Self {
124        self.is_root = value;
125        self
126    }
127
128    pub fn case_sensitive_sorting(mut self, value: bool) -> Self {
129        self.is_case_sensitive_sorting = value;
130        self
131    }
132
133    pub fn build(self) -> GetByFilterRequest {
134        GetByFilterRequest {
135            body: Some(GetByFilterBody {
136                pluck: self.pluck,
137                advance_filters: self.advance_filters,
138                order_by: self.order_by.unwrap_or_default(),
139                limit: self.limit.unwrap_or_default(),
140                offset: self.offset.unwrap_or_default(),
141                order_direction: self.order_direction.unwrap_or_default(),
142                joins: self.joins,
143                multiple_sort: self.multiple_sort,
144                pluck_object: self.pluck_object,
145                date_format: self.date_format.unwrap_or_default(),
146                is_case_sensitive_sorting: self.is_case_sensitive_sorting,
147            }),
148            params: Some(Params {
149                id: self.id.unwrap_or_default(),
150                table: self.table.unwrap_or_default(),
151                r#type: if self.is_root {
152                    String::from("root")
153                } else {
154                    String::new()
155                },
156            }),
157        }
158    }
159}