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';
26 Koha::REST::Plugin::Objects
35 my ( $self, $app ) = @_;
39 my $patrons_rs = Koha::Patrons->new;
40 my $patrons = $c->objects->find( $patrons_rs, $id );
42 Performs a database search using given Koha::Objects object and the $id.
44 Returns I<undef> if no object is found Returns the I<API representation> of
45 the requested object. It passes through any embeds if specified.
50 'objects.find' => sub {
51 my ( $c, $result_set, $id ) = @_;
56 my $embed = $c->stash('koha.embed');
57 # Generate prefetches for embedded stuff
58 $c->dbic_merge_prefetch(
60 attributes => $attributes,
61 result_set => $result_set
65 my $object = $result_set->find( $id, $attributes );
67 return unless $object;
69 return $object->to_api({ embed => $embed });
75 my $patrons_rs = Koha::Patrons->new;
76 my $patrons = $c->objects->search( $patrons_rs );
78 Performs a database search using given Koha::Objects object and query parameters.
80 Returns an arrayref of the hashrefs representing the resulting objects
86 'objects.search' => sub {
87 my ( $c, $result_set ) = @_;
89 my $args = $c->validation->output;
92 # Extract reserved params
93 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
95 my $embed = $c->stash('koha.embed');
97 # Merge sorting into query attributes
98 $c->dbic_merge_sorting(
100 attributes => $attributes,
101 params => $reserved_params,
102 result_set => $result_set
106 # If no pagination parameters are passed, default
107 $reserved_params->{_per_page} //= C4::Context->preference('RESTdefaultPageSize');
108 $reserved_params->{_page} //= 1;
110 unless ( $reserved_params->{_per_page} == -1 ) {
111 # Merge pagination into query attributes
112 $c->dbic_merge_pagination(
114 filter => $attributes,
115 params => $reserved_params
120 # Generate prefetches for embedded stuff
121 $c->dbic_merge_prefetch(
123 attributes => $attributes,
124 result_set => $result_set
128 # Call the to_model function by reference, if defined
129 if ( defined $filtered_params ) {
131 # Apply the mapping function to the passed params
132 $filtered_params = $result_set->attributes_from_api($filtered_params);
133 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
136 if( defined $reserved_params->{q} || defined $reserved_params->{query} || defined $reserved_params->{'x-koha-query'}) {
137 $filtered_params //={};
138 my @query_params_array;
140 push @query_params_array, $reserved_params->{query} if defined $reserved_params->{query};
141 my $json = JSON->new;
142 push @query_params_array, $json->decode($reserved_params->{q}) if defined $reserved_params->{q};
143 push @query_params_array, $json->decode($reserved_params->{'x-koha-query'}) if defined $reserved_params->{'x-koha-query'};
145 if(scalar(@query_params_array) > 1) {
146 $query_params = {'-and' => \@query_params_array};
148 $query_params = $query_params_array[0];
151 $filtered_params = $c->merge_q_params( $filtered_params, $query_params, $result_set );
154 my $objects = $result_set->search( $filtered_params, $attributes );
155 my $total = $result_set->search->count;
157 $c->add_pagination_headers(
159 total => ($objects->is_paged ? $objects->pager->total_entries : $objects->count),
160 base_total => $total,
165 return $objects->to_api({ embed => $embed });