1 package Koha::REST::Plugin::Objects;
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Mojo::Base 'Mojolicious::Plugin';
22 use JSON qw(decode_json);
26 Koha::REST::Plugin::Objects
34 my $patrons_rs = Koha::Patrons->new;
35 my $patrons = $c->objects->search( $patrons_rs );
37 Performs a database search using given Koha::Objects object and query parameters.
39 Returns an arrayref of the hashrefs representing the resulting objects
45 my ( $self, $app ) = @_;
48 'objects.search' => sub {
49 my ( $c, $result_set ) = @_;
51 my $args = $c->validation->output;
54 # Extract reserved params
55 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
57 my $embed = $c->stash('koha.embed');
59 # Merge sorting into query attributes
60 $c->dbic_merge_sorting(
62 attributes => $attributes,
63 params => $reserved_params,
64 result_set => $result_set
68 # Merge pagination into query attributes
69 $c->dbic_merge_pagination(
71 filter => $attributes,
72 params => $reserved_params
76 # Generate prefetches for embedded stuff
77 $c->dbic_merge_prefetch(
79 attributes => $attributes,
80 result_set => $result_set
84 # Call the to_model function by reference, if defined
85 if ( defined $filtered_params ) {
87 # Apply the mapping function to the passed params
88 $filtered_params = $result_set->attributes_from_api($filtered_params);
89 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
92 if ( defined $path_params ) {
94 # Apply the mapping function to the passed params
95 $filtered_params //= {};
96 $path_params = $result_set->attributes_from_api($path_params);
97 foreach my $param (keys %{$path_params}) {
98 $filtered_params->{$param} = $path_params->{$param};
102 if( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
103 $filtered_params //={};
104 my @query_params_array;
106 push @query_params_array, $reserved_params->{query} if defined $reserved_params->{query};
107 push @query_params_array, decode_json($reserved_params->{q}) if defined $reserved_params->{q};
108 push @query_params_array, decode_json($reserved_params->{'x-koha-query'}) if defined $reserved_params->{'x-koha-query'};
110 if(scalar(@query_params_array) > 1) {
111 $query_params = {'-and' => \@query_params_array};
113 $query_params = $query_params_array[0];
116 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set );
119 my $objects = $result_set->search( $filtered_params, $attributes );
121 if ($objects->is_paged) {
122 $c->add_pagination_headers({
123 total => $objects->pager->total_entries,
128 return $objects->to_api({ embed => $embed });