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';
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 });