Bug 24545: Fix license statements
[srvgit] / Koha / REST / Plugin / Objects.pm
1 package Koha::REST::Plugin::Objects;
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Plugin';
21
22 =head1 NAME
23
24 Koha::REST::Plugin::Objects
25
26 =head1 API
27
28 =head2 Helper methods
29
30 =head3 objects.search
31
32     my $patrons_rs = Koha::Patrons->new;
33     my $patrons = $c->objects->search( $patrons_rs );
34
35 Performs a database search using given Koha::Objects object and query parameters.
36
37 Returns an arrayref of the hashrefs representing the resulting objects
38 for API rendering.
39
40 =cut
41
42 sub register {
43     my ( $self, $app ) = @_;
44
45     $app->helper(
46         'objects.search' => sub {
47             my ( $c, $result_set ) = @_;
48
49             my $args = $c->validation->output;
50             my $attributes = {};
51
52             # Extract reserved params
53             my ( $filtered_params, $reserved_params, $path_params ) = $c->extract_reserved_params($args);
54             # Look for embeds
55             my $embed = $c->stash('koha.embed');
56
57             # Merge sorting into query attributes
58             $c->dbic_merge_sorting(
59                 {
60                     attributes => $attributes,
61                     params     => $reserved_params,
62                     result_set => $result_set
63                 }
64             );
65
66             # Merge pagination into query attributes
67             $c->dbic_merge_pagination(
68                 {
69                     filter => $attributes,
70                     params => $reserved_params
71                 }
72             );
73
74             # Generate prefetches for embedded stuff
75             $c->dbic_merge_prefetch(
76                 {
77                     attributes => $attributes,
78                     result_set => $result_set
79                 }
80             );
81
82             # Call the to_model function by reference, if defined
83             if ( defined $filtered_params ) {
84
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 );
88             }
89
90             if ( defined $path_params ) {
91
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};
97                 }
98             }
99
100             # Perform search
101             my $objects = $result_set->search( $filtered_params, $attributes );
102
103             if ($objects->is_paged) {
104                 $c->add_pagination_headers({
105                     total => $objects->pager->total_entries,
106                     params => $args,
107                 });
108             }
109
110             return $objects->to_api({ embed => $embed });
111         }
112     );
113 }
114
115 1;