| 1 | package Rapi::Blog::DB::Component::ResultSet::ListAPI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use parent 'DBIx::Class::ResultSet'; | ||
| 7 | |||
| 8 | use RapidApp::Util ':all'; | ||
| 9 | use URI::Escape qw/uri_escape uri_unescape/; | ||
| 10 | |||
| 11 | |||
| 12 | sub _api_param_arg_order { undef; } | ||
| 13 | sub _get_api_param_arg_order { (shift)->_api_param_arg_order(@_) // [qw/search/] } | ||
| 14 | |||
| 15 | sub _api_default_params { undef; } | ||
| 16 | sub _get_api_default_params { | ||
| 17 | my $params = (shift)->_api_default_params(@_) || {}; | ||
| 18 | { page => 1, limit => 500, %$params } | ||
| 19 | } | ||
| 20 | |||
| 21 | # This setting allows for identifying a specific string value as 'undef' since | ||
| 22 | # strings are all we have to work with in URLs | ||
| 23 | sub _api_params_undef_map {{}}; | ||
| 24 | |||
| 25 | |||
| 26 | # get/set accessor: | ||
| 27 | sub _list_api_params { | ||
| 28 | my ($self, @args) = @_; | ||
| 29 | |||
| 30 | if(scalar(@args) > 0) { | ||
| 31 | my %P = (); | ||
| 32 | if((ref($args[0])||'') eq 'HASH') { | ||
| 33 | # Our caller has supplied arguments already as name/values: | ||
| 34 | %P = %{ $args[0] }; | ||
| 35 | } | ||
| 36 | else { | ||
| 37 | # Also support ordered list arguments: | ||
| 38 | my @order = @{$self->_get_api_param_arg_order}; | ||
| 39 | for my $value (@args) { | ||
| 40 | my $param = shift(@order) or last; | ||
| 41 | $P{$param} = $value; | ||
| 42 | } | ||
| 43 | } | ||
| 44 | |||
| 45 | for my $key (grep { $_ =~ /^new\_/ } keys %P) { | ||
| 46 | my $new_val = delete $P{$key}; | ||
| 47 | $key =~ s/^new_//; | ||
| 48 | $P{$key} = $new_val; | ||
| 49 | } | ||
| 50 | |||
| 51 | my $defaults = $self->_get_api_default_params; | ||
| 52 | exists $P{$_} or $P{$_} = $defaults->{$_} for (keys %$defaults); | ||
| 53 | |||
| 54 | # Extra checks to make sure limit and page are not only defined but positive integers, | ||
| 55 | # and if the consumer class has set | ||
| 56 | $P{limit} = $defaults->{limit} unless ($P{limit} && $P{limit} =~ /^\d+$/); | ||
| 57 | $P{page} = $defaults->{page} unless ($P{page} && $P{page} =~ /^\d+$/); | ||
| 58 | |||
| 59 | # Finally, fallback defaults in case the consumer class _api_default_params doesn't | ||
| 60 | # return correct/valid limit and page params (abundance of caution) | ||
| 61 | $P{limit} = 500 unless ($P{limit} && $P{limit} =~ /^\d+$/); | ||
| 62 | $P{page} = 1 unless ($P{page} && $P{page} =~ /^\d+$/); | ||
| 63 | |||
| 64 | # Translate placeholder values meant to be 'undef' into undef: | ||
| 65 | my $uMap = $self->_api_params_undef_map; | ||
| 66 | ($P{$_} && $P{$_} eq $uMap->{$_}) and $P{$_} = undef for (keys %$uMap); | ||
| 67 | |||
| 68 | $self->{attrs}{___list_api_params} = \%P; | ||
| 69 | } | ||
| 70 | |||
| 71 | $self->{attrs}{___list_api_params} || {} | ||
| 72 | } | ||
| 73 | |||
| 74 | |||
| 75 | sub _list_api { | ||
| 76 | my ($self, @args) = @_; | ||
| 77 | |||
| 78 | my $P = $self->_list_api_params(@args); | ||
| 79 | |||
| 80 | my $Rs = $self; | ||
| 81 | |||
| 82 | my @rows = (); | ||
| 83 | my $pages = 1; | ||
| 84 | |||
| 85 | my $total = $Rs->_safe_count; | ||
| 86 | if($total > 0) { | ||
| 87 | $pages = int($total/$P->{limit}); | ||
| 88 | $pages++ if ($total % $P->{limit}); | ||
| 89 | } | ||
| 90 | |||
| 91 | $P->{page} = $pages if ($P->{page} > $pages); | ||
| 92 | |||
| 93 | @rows = $Rs | ||
| 94 | ->search_rs(undef,{ page => $P->{page}, rows => $P->{limit} }) | ||
| 95 | ->all if ($total > 0); | ||
| 96 | |||
| 97 | my $count = (scalar @rows); | ||
| 98 | |||
| 99 | my $thru = $P->{page} == 1 ? $count : ($P->{page}-1) * $P->{limit} + $count; | ||
| 100 | my $remaining = $total - $thru; | ||
| 101 | |||
| 102 | my $last_page = $P->{page} == $pages ? 1 : 0; | ||
| 103 | |||
| 104 | my $this_qs = $self->_to_query_string(%$P); | ||
| 105 | my $prev_qs = $P->{page} > 1 ? $self->_to_query_string(%$P, page => $P->{page}-1 ) : undef; | ||
| 106 | my $next_qs = !$last_page ? $self->_to_query_string(%$P, page => $P->{page}+1 ) : undef; | ||
| 107 | my $first_qs = $P->{page} > 2 ? $self->_to_query_string(%$P, page => 1 ) : undef; | ||
| 108 | my $last_qs = $P->{page} < ($pages-1) ? $self->_to_query_string(%$P, page => $pages ) : undef; | ||
| 109 | |||
| 110 | my %meta = ( | ||
| 111 | # Number of items returned (this page) | ||
| 112 | count => $count, | ||
| 113 | |||
| 114 | # Total number of items (all pages) | ||
| 115 | total => $total, | ||
| 116 | |||
| 117 | # Page number of current page | ||
| 118 | page => $P->{page}, | ||
| 119 | |||
| 120 | # Total number of pages | ||
| 121 | pages => $pages, | ||
| 122 | |||
| 123 | # True is the current page is the last page | ||
| 124 | last_page => $P->{page} == $pages ? 1 : 0, | ||
| 125 | |||
| 126 | # True if this page already contains all items | ||
| 127 | complete => $total == $count ? 1 : 0, | ||
| 128 | |||
| 129 | # The number (out of total items) this page starts at | ||
| 130 | start => $thru - $count + 1, | ||
| 131 | |||
| 132 | # The number (out of total items) this page ends at | ||
| 133 | end => $thru, | ||
| 134 | |||
| 135 | # The number of items remaining after this page | ||
| 136 | remaining => $remaining, | ||
| 137 | |||
| 138 | # The number of items in all the pages before this one | ||
| 139 | before => $thru - $count, | ||
| 140 | |||
| 141 | # The limit of items per page | ||
| 142 | limit => $P->{limit}, | ||
| 143 | |||
| 144 | # Expressed as a query string, the params that would return the first page (undef if N/A) | ||
| 145 | first_qs => $first_qs, | ||
| 146 | |||
| 147 | # Expressed as a query string, the params that would return the last page (undef if N/A) | ||
| 148 | last_qs => $last_qs, | ||
| 149 | |||
| 150 | # Expressed as a query string, the params that would return the previous page (undef if N/A) | ||
| 151 | prev_qs => $prev_qs, | ||
| 152 | |||
| 153 | # Expressed as a query string, the params that would return the next page (undef if N/A) | ||
| 154 | next_qs => $next_qs, | ||
| 155 | |||
| 156 | # Expressed as a query string, the params that would return this same page | ||
| 157 | this_qs => $this_qs, | ||
| 158 | |||
| 159 | # The current params for this page as a HashRef | ||
| 160 | params => $P | ||
| 161 | ); | ||
| 162 | |||
| 163 | return { %meta, rows => \@rows } | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | sub _to_query_string { | ||
| 168 | my $self = shift; | ||
| 169 | my %params = (ref($_[0]) eq 'HASH') ? %{ $_[0] } : @_; # <-- arg as hash or hashref | ||
| 170 | |||
| 171 | # Translate undef values back to their placeholder string values: | ||
| 172 | my $uMap = $self->_api_params_undef_map; | ||
| 173 | (exists $params{$_} && ! defined $params{$_}) and $params{$_} = $uMap->{$_} for (keys %$uMap); | ||
| 174 | |||
| 175 | # remove params already at their default values in order to provide a cleaner url: | ||
| 176 | my $defaults = $self->_get_api_default_params; | ||
| 177 | for my $key (keys %$defaults) { | ||
| 178 | delete $params{$key} if (defined $params{$key} && "$params{$key}" eq "$defaults->{$key}"); | ||
| 179 | } | ||
| 180 | |||
| 181 | # strip empty string values (false values are expected to use '0') | ||
| 182 | $params{$_} eq '' and delete $params{$_} for (keys %params); | ||
| 183 | |||
| 184 | # Put the page back in - even if its already at its default value - if there | ||
| 185 | # are no other params to ensure we return a "true" value | ||
| 186 | $params{page} = $defaults->{page} || 1 unless (scalar(keys %params) > 0); | ||
| 187 | |||
| 188 | my %encP = map { $_ => uri_escape($params{$_}) } keys %params; | ||
| 189 | |||
| 190 | join('&',map { join('=',$_,$encP{$_}) } keys %encP) | ||
| 191 | } | ||
| 192 | |||
| 193 | |||
| 194 | sub _select_only_primary_columns { | ||
| 195 | my $self = shift; | ||
| 196 | |||
| 197 | my @pri_cols = $self->result_source->primary_columns; | ||
| 198 | die "No primary columns!" unless (scalar(@pri_cols) > 0); | ||
| 199 | |||
| 200 | my $select = [ map { join('.',$self->current_source_alias,$_) } @pri_cols ]; | ||
| 201 | my $as = \@pri_cols; | ||
| 202 | |||
| 203 | $self->search_rs(undef,{ columns => [], select => $select, as => $as }) | ||
| 204 | } | ||
| 205 | |||
| 206 | sub _safe_count { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | # There seems to be a DBIC bug in count - | ||
| 210 | # Get errors like: | ||
| 211 | # Single parameters to new() must be a HASH ref data => DBIx::Class::ResultSource::Table=HASH(0xc8ce240) | ||
| 212 | # When we try to call ->count when we have { join => 'post_tags', group_by => 'me.id' } in the attrs. | ||
| 213 | # The line which is barfing is: | ||
| 214 | # https://metacpan.org/source/RIBASUSHI/DBIx-Class-0.082840/lib/DBIx/Class/ResultSet.pm#L3389 | ||
| 215 | # Which is DBIC trying to create a fresh_rs where it passes $self->result_source to ->new() so | ||
| 216 | # it seems to be an internal inconsistency. I don't have time to deal with it now so I created this | ||
| 217 | # ugly fallback for now. FIXME | ||
| 218 | |||
| 219 | |||
| 220 | my $count = try{ $self->count }; | ||
| 221 | return $count if (defined $count); | ||
| 222 | |||
| 223 | # Do a manual query and count the results, but try to make it least expensive as possible | ||
| 224 | my @rows = $self | ||
| 225 | ->_select_only_primary_columns | ||
| 226 | ->search_rs(undef, { result_class => 'DBIx::Class::ResultClass::HashRefInflator'}) | ||
| 227 | ->all; | ||
| 228 | |||
| 229 | return scalar(@rows); | ||
| 230 | } | ||
| 231 | |||
| 232 | 1; | ||
| 233 | |||
| 234 | __END__ |