Bugzilla – Attachment 66753 Details for
Bug 19234
Add query parameters handling helpers
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 19234: Add query parameters handling helpers
Bug-19234-Add-query-parameters-handling-helpers.patch (text/plain), 3.79 KB, created by
Tomás Cohen Arazi (tcohen)
on 2017-09-01 15:02:38 UTC
(
hide
)
Description:
Bug 19234: Add query parameters handling helpers
Filename:
MIME Type:
Creator:
Tomás Cohen Arazi (tcohen)
Created:
2017-09-01 15:02:38 UTC
Size:
3.79 KB
patch
obsolete
>From 88a6a2fd6aebaccb07e189149c5cd2b32c0a05c2 Mon Sep 17 00:00:00 2001 >From: Tomas Cohen Arazi <tomascohen@theke.io> >Date: Fri, 1 Sep 2017 11:53:35 -0300 >Subject: [PATCH] Bug 19234: Add query parameters handling helpers > >This patch introduces a Mojolicious plugin to be used on the REST api. >It adds a helper method: > >generate_dbic_query >=================== > >When used, it generates what's needed to perform a search on >DBIC/Koha::Objects like this: > > my $params = $c->validation->output; > my ($filter, $attributes) = $c->generate_dbic_query($params); > my $patrons = Koha::Patrons->search( $filter, $attributes ); > >It introduces a reserved word _exact_match that controls the generated >$filter: >- Default behaviour: right truncation on the query params >- _exact_match=1: no truncation added. > >All the plugin's behaviour is tested. > >To test: >- Run: > $ sudo koha-shell kohadev > k$ cd kohaclone > k$ prove t/Koha/REST/Plugin/Query.t >=> SUCCESS: Tests pass! >- Sign off :-D > >Sponsored-by: ByWater solutions >Sponsored-by: Camden County >--- > Koha/REST/Plugin/Query.pm | 108 ++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 108 insertions(+) > create mode 100644 Koha/REST/Plugin/Query.pm > >diff --git a/Koha/REST/Plugin/Query.pm b/Koha/REST/Plugin/Query.pm >new file mode 100644 >index 0000000000..ebab345bba >--- /dev/null >+++ b/Koha/REST/Plugin/Query.pm >@@ -0,0 +1,108 @@ >+package Koha::REST::Plugin::Query; >+ >+# This file is part of Koha. >+# >+# Koha is free software; you can redistribute it and/or modify it under the >+# terms of the GNU General Public License as published by the Free Software >+# Foundation; either version 3 of the License, or (at your option) any later >+# version. >+# >+# Koha is distributed in the hope that it will be useful, but WITHOUT ANY >+# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR >+# A PARTICULAR PURPOSE. See the GNU General Public License for more details. >+# >+# You should have received a copy of the GNU General Public License along >+# with Koha; if not, write to the Free Software Foundation, Inc., >+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. >+ >+use Modern::Perl; >+ >+use Mojo::Base 'Mojolicious::Plugin'; >+ >+=head1 NAME >+ >+Koha::REST::Plugin::Query >+ >+=head1 API >+ >+=head2 Mojolicious::Plugin methods >+ >+=head3 register >+ >+=cut >+ >+sub register { >+ my ( $self, $app ) = @_; >+ >+=head2 Helper methods >+ >+=head3 generate_dbic_query >+ >+ my ( $filter, $attributes ) = $c->generate_dbic_query($params); >+ >+Generates the DBIC query from the query parameters. >+ >+=cut >+ >+ $app->helper( >+ 'generate_dbic_query' => sub { >+ my ( $c, $params ) = @_; >+ >+ my $attributes = _build_dbic_attributes( $params->{page}, $params->{per_page} ); >+ my $filter = _build_dbic_filter( $params ); >+ >+ return ( $filter, $attributes ); >+ } >+ ); >+} >+ >+=head2 Internal methods >+ >+=head3 _build_dbic_attributes >+ >+ my $attributes = _build_dbic_attributes( $page, $per_page ); >+ >+=cut >+ >+sub _build_dbic_attributes { >+ my ( $page, $per_page ) = @_; >+ >+ my $attributes = { >+ rows => $per_page, >+ page => $page >+ }; >+ >+ return $attributes; >+} >+ >+=head3 _build_dbic_filter >+ >+ my $filter = _build_dbic_filter( $params ); >+ >+=cut >+ >+sub _build_dbic_filter { >+ my ( $params ) = @_; >+ >+ my $filter; >+ my $exact_match = $params->{_exact_match}; >+ >+ for my $param ( keys %{ $params } ) { >+ >+ next if $param eq 'page' # reserved >+ or $param eq 'per_page' # reserved >+ or $param eq '_exact_match' ; # reserved >+ >+ if ( $exact_match ) { >+ $filter->{$param} = $params->{$param}; >+ } >+ else { >+ $filter->{$param} = { like => $params->{$param} . "%" } >+ } >+ } >+ >+ return $filter; >+} >+ >+ >+1; >-- >2.14.1
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 19234
:
66752
|
66753
|
66754
|
66991
|
66992
|
66993
|
67358
|
67359
|
67360
|
67363
|
67364
|
67365
|
67374
|
67375
|
67377
|
67785
|
67786
|
67787
|
69360
|
69361
|
69362