Service/Search.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Service/Profiles/Search.pm ↑ Summary Service/Profiles.pm »
1 package XAS::Service::Search;
2  
3 our $VERSION = '0.01';
4  
5 0 ▶use XAS::Service::Profiles;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use XAS::Service::Profiles::Search;
7 use XAS::Constants 'HASHREF ARRAYREF';
8  
9 use XAS::Class
10 version => $VERSION,
11 base => 'XAS::Base',
12 mixin => 'XAS::Service::CheckParameters',
13 accessors => 'profile',
14 utils => ':validation',
15 codec => 'JSON',
16 vars => {
17 PARAMS => {
18 -columns => { type => ARRAYREF },
19 }
20 }
21 ;
22  
23 use Data::Dumper;
24  
25 # ----------------------------------------------------------------------
26 # Public Methods
27 # ----------------------------------------------------------------------
28  
29 sub build {
30 my $self = shift;
31 my ($multi) = validate_params(\@_, [
32 { isa => 'Hash::MultiValue' },
33 ]);
34  
35 my $opts;
36 my $options = {};
37 my $criteria = {};
38 my $params = $multi->as_hashref;
39  
40 # internal routines
41  
42 my $comparison = sub {
43 my $filter = shift;
44  
45 if ($filter->{'comparison'} eq 'lt') {
46  
47 $criteria->{$filter->{'field'}} = {'<', $filter->{'value'}};
48  
49 } elsif ($filter->{'comparison'} eq 'le') {
50  
51 $criteria->{$filter->{'field'}} = {'<=', $filter->{'value'}};
52  
53 } elsif ($filter->{'comparison'} eq 'gt') {
54  
55 $criteria->{$filter->{'field'}} = {'>', $filter->{'value'}};
56  
57 } elsif ($filter->{'comparison'} eq 'ge') {
58  
59 $criteria->{$filter->{'field'}} = {'>=', $filter->{'value'}};
60  
61 } elsif ($filter->{'comparison'} eq 'lk') {
62  
63 $criteria->{$filter->{'field'}} = {-like => [$filter->{'value'} . '%']};
64  
65 } elsif ($filter->{'comparison'} eq 'eq') {
66  
67 $criteria->{$filter->{'field'}} = $filter->{'value'};
68  
69 } elsif ($filter->{'comparison'} eq 'be') {
70  
71 my $begin = shift(@{$filter->{'value'}});
72 my $end = shift(@{$filter->{'value'}});
73  
74 $criteria->{$filter->{'field'}} = {-between => [$begin, $end]};
75  
76 }
77  
78 };
79  
80 # build the options
81  
82 if (defined($params->{'start'}) && defined($params->{'limit'})) {
83  
84 my $pager = $self->_dbpage($params->{'start'}, $params->{'limit'});
85  
86 $options->{'page'} = $pager->{'page'};
87 $options->{'rows'} = $pager->{'rows'};
88  
89 delete $params->{'start'};
90 delete $params->{'limit'};
91  
92 }
93  
94 if (defined($params->{'sort'})) {
95  
96 $params->{'sort'} = decode($params->{'sort'});
97 $options->{'order_by'} = $self->_order_by($params->{'sort'});
98  
99 delete $params->{'sort'};
100  
101 }
102  
103 if (defined($params->{'group'})) {
104  
105 $params->{'group'} = decode($params->{'group'});
106 $options->{'group_by'} = $self->_group_by($params->{'group'});
107  
108 delete $params->{'group'};
109  
110 }
111  
112 # user defined options
113  
114 if (defined($params->{'options'})) {
115  
116 my $hash = $params->{'options'};
117  
118 while (my ($key, $value) = each(%$hash)) {
119  
120 $options->{$key} = $value;
121  
122 }
123  
124 }
125  
126 # build the criteria
127  
128 if (defined($params->{'filter'})) {
129  
130 my $filters = decode($params->{'filter'});
131  
132 while (my $filter = shift(@$filters)) {
133  
134 $self->check_parameters($filter, 'filter');
135  
136 if ($filter->{'type'} eq 'string') {
137  
138 if (defined($filter->{'comparison'})) {
139  
140 $comparison->($filter);
141  
142 } else {
143  
144 $criteria->{$filter->{'field'}} = $filter->{'value'};
145  
146 }
147  
148 } elsif ($filter->{'type'} eq 'number') {
149  
150 if (defined($filter->{'comparison'})) {
151  
152 $comparison->($filter);
153  
154 } else {
155  
156 $criteria->{$filter->{'field'}} = $filter->{'value'};
157  
158 }
159  
160 } elsif ($filter->{'type'} eq 'list') {
161  
162 while (my $x = shift(@{$filter->{'value'}})) {
163  
164 push(@{$criteria->{$filter->{'field'}}}, $x);
165  
166 }
167  
168 } elsif ($filter->{'type'} eq 'boolean') {
169  
170 my $b = sprintf("%s", $filter->{'value'});
171  
172 $criteria->{$filter->{'field'}} = 'f' if ($b eq '0');
173 $criteria->{$filter->{'field'}} = 't' if ($b eq '1');
174  
175 } elsif ($filter->{'type'} eq 'date') {
176  
177 if (defined($filter->{'comparison'})) {
178  
179 $comparison->($filter);
180  
181 } else {
182  
183 $criteria->{$filter->{'field'}} = $filter->{'value'};
184  
185 }
186  
187 }
188  
189 }
190  
191 }
192  
193 return $criteria, $options;
194  
195 }
196  
197 # ----------------------------------------------------------------------
198 # Private Methods
199 # ----------------------------------------------------------------------
200  
201 sub init {
202 my $class = shift;
203  
204 my $self = $class->SUPER::init(@_);
205  
206 my $fields = $self->columns->[0];
207 my $search = XAS::Service::Profiles::Search->new($fields);
208  
209 $self->{'profile'} = XAS::Service::Profiles->new($search);
210  
211 return $self;
212  
213 }
214  
215 #
216 # General purpose rounding function.
217 #
218 # To round a number to the nearest integer, use round($x). To round it to
219 # the nearest 10, use round($x, 1). Nearest 100 is round($x, 2) and so on.
220 # You can also round to the nearest tenth, hundredth, etc., by using a
221 # negative second argument: round(5.28, -1) == 5.3.
222 #
223 sub _roundup {
224 my $self = shift;
225 my $n = shift;
226 my $places = shift;
227  
228 my $factor = 10 ** ($places || 0);
229 return (int(($n * $factor) + ($n < 0 ? -1 : 1) * 0.5) / $factor);
230  
231 }
232  
233 #
234 # Find the "page" in a "paged" db retrieval
235 #
236 sub _dbpage {
237 my $self = shift;
238 my $start = shift;
239 my $limit = shift;
240  
241 my $params = {
242 start => $start,
243 limit => $limit
244 };
245  
246 $self->check_parameters($params, 'pager');
247  
248 return {
249 page => $self->_roundup(($start / $limit) + 1),
250 rows => $limit
251 }
252  
253 }
254  
255 sub _order_by {
256 my $self = shift;
257 my $ob = shift;
258  
259 my @d;
260  
261 foreach my $x (@$ob) {
262  
263 my $y;
264 my $z = $self->check_parameters($x, 'sort');
265  
266 $y->{'-asc'} = $z->{'field'} if ($z->{'direction'} eq 'ASC');
267 $y->{'-desc'} = $z->{'field'} if ($z->{'direction'} eq 'DESC');
268  
269 push(@d, $y);
270  
271 }
272  
273 return \@d;
274  
275 }
276  
277 sub _group_by {
278 my ($self, $ob) = @_;
279  
280 my @d;
281 my $columns = $self->columns->[0];
282  
283 foreach my $x (@$ob) {
284  
285 my $y = $self->check_parameters($x, 'group');
286 my $z = $y->{'field'};
287  
288 push(@d, $z);
289  
290 }
291  
292 foreach my $x (@$columns) {
293  
294 push(@d, 'me.' . $x);
295  
296 };
297  
298 return \@d;
299  
300 }
301  
302 1;
303  
304 __END__
« Service/Profiles/Search.pm ↑ Summary Service/Profiles.pm »