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 under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 3 of the License, or (at your option) any later
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use Mojo::Base 'Mojolicious::Plugin';
24 Koha::REST::Plugin::Objects
32 my $patrons_rs = Koha::Patrons->new;
33 my $patrons = $c->objects->search( $patrons_rs );
35 Performs a database search using given Koha::Objects object and query parameters.
37 Returns an arrayref of the hashrefs representing the resulting objects
43 my ( $self, $app ) = @_;
46 'objects.search' => sub {
47 my ( $c, $result_set ) = @_;
49 my $args = $c->validation->output;
52 # Extract reserved params
53 my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
55 my $embed = $c->stash('koha.embed');
57 # Merge sorting into query attributes
58 $c->dbic_merge_sorting(
60 attributes => $attributes,
61 params => $reserved_params,
62 result_set => $result_set
66 # Merge pagination into query attributes
67 $c->dbic_merge_pagination(
69 filter => $attributes,
70 params => $reserved_params
74 # Generate prefetches for embedded stuff
75 $c->dbic_merge_prefetch(
77 attributes => $attributes,
78 result_set => $result_set
82 # Call the to_model function by reference, if defined
83 if ( defined $filtered_params ) {
85 # Apply the mapping function to the passed params
86 $filtered_params = $result_set->attributes_from_api($filtered_params);
87 $filtered_params = $c->build_query_params( $filtered_params, $reserved_params );
90 if ( defined $path_params ) {
92 # Apply the mapping function to the passed params
93 $filtered_params //= {};
94 $path_params = $result_set->attributes_from_api($path_params);
95 foreach my $param (keys %{$path_params}) {
96 $filtered_params->{$param} = $path_params->{$param};
101 my $objects = $result_set->search( $filtered_params, $attributes );
103 if ($objects->is_paged) {
104 $c->add_pagination_headers({
105 total => $objects->pager->total_entries,
110 return $objects->to_api({ embed => $embed });