Bug 33573: Add public endpoint for cancelling holds
[koha.git] / Koha / REST / V1 / SearchFilter.pm
1 package Koha::REST::V1::SearchFilter;
2
3 # This file is part of Koha.
4 #
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
8 # version.
9 #
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.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with Koha; if not, see <http://www.gnu.org/licenses>.
16
17 use Modern::Perl;
18
19 use Mojo::Base 'Mojolicious::Controller';
20 use Koha::SearchFilters;
21
22 use Try::Tiny qw( catch try );
23
24 =head1 Name
25
26 Koha::REST::V1::SearchFilters
27
28 =head1 API
29
30 =head2 Methods
31
32 =head3 list
33
34 Controller function that handles listing Koha::SearchFilter objects
35
36 =cut
37
38 sub list {
39     my $c = shift->openapi->valid_input or return;
40     return try {
41         my $filters_set = Koha::SearchFilters->search({});
42         my $filters = $c->objects->search( $filters_set );
43         return $c->render(
44             status  => 200,
45             openapi => $filters
46         );
47     }
48     catch {
49         $c->unhandled_exception($_);
50     };
51
52 }
53
54 =head3 get
55
56 Controller function that handles retrieving a single Koha::AdvancedEditorMacro
57
58 =cut
59
60 sub get {
61     my $c = shift->openapi->valid_input or return;
62     my $filter = Koha::SearchFilters->find( $c->validation->param('search_filter_id') );
63     unless ($filter) {
64         return $c->render( status  => 404,
65                            openapi => { error => "Search filter not found" } );
66     }
67
68     return $c->render( status => 200, openapi => $filter->to_api );
69 }
70
71 =head3 add
72
73 Controller function that handles adding a new Koha::SearchFilter object
74
75 =cut
76
77 sub add {
78     my $c = shift->openapi->valid_input or return;
79
80     return try {
81         my $filter = Koha::SearchFilter->new_from_api( $c->validation->param('body') );
82         $filter->store->discard_changes;
83         $c->res->headers->location( $c->req->url->to_string . '/' . $filter->id );
84         return $c->render(
85             status  => 201,
86             openapi => $filter->to_api
87         );
88     }
89     catch {
90         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
91             return $c->render(
92                 status  => 409,
93                 openapi => { error => $_->error, conflict => $_->duplicate_id }
94             );
95         }
96         $c->unhandled_exception($_);
97     };
98 }
99
100 =head3 update
101
102 Controller function that handles updating a Koha::SearchFilter object
103
104 =cut
105
106 sub update {
107     my $c = shift->openapi->valid_input or return;
108
109     my $filter = Koha::SearchFilters->find( $c->validation->param('search_filter_id') );
110
111     if ( not defined $filter ) {
112         return $c->render( status  => 404,
113                            openapi => { error => "Object not found" } );
114     }
115
116     return try {
117         my $params = $c->req->json;
118         $filter->set_from_api( $params );
119         $filter->store->discard_changes;
120         return $c->render( status => 200, openapi => $filter->to_api );
121     }
122     catch {
123         $c->unhandled_exception($_);
124     };
125 }
126
127 =head3 delete
128
129 Controller function that handles deleting a Koha::SearchFilter object
130
131 =cut
132
133 sub delete {
134     my $c = shift->openapi->valid_input or return;
135
136     my $filter = Koha::SearchFilters->find( $c->validation->param('search_filter_id') );
137     if ( not defined $filter ) {
138         return $c->render( status  => 404,
139                            openapi => { error => "Object not found" } );
140     }
141
142     return try {
143         $filter->delete;
144         return $c->render( status => 204, openapi => q{} );
145     }
146     catch {
147         $c->unhandled_exception($_);
148     };
149 }
150
151 1;