Bug 29806: Fix POST /holds use of pickup_locations
[koha.git] / Koha / REST / V1 / Suggestions.pm
1 package Koha::REST::V1::Suggestions;
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
21 use Koha::Suggestions;
22
23 use Try::Tiny;
24
25 =head1 NAME
26
27 Koha::REST::V1::Suggestion
28
29 =head1 API
30
31 =head2 Methods
32
33 =head3 list
34
35 Controller method that handles listing Koha::Suggestion objects
36
37 =cut
38
39 sub list {
40     my $c = shift->openapi->valid_input or return;
41
42     return try {
43
44         my $suggestions = $c->objects->search( Koha::Suggestions->new );
45
46         return $c->render(
47             status  => 200,
48             openapi => $suggestions
49         );
50     }
51     catch {
52         $c->unhandled_exception($_);
53     };
54 }
55
56 =head3 get
57
58 Controller method that handles retrieving a single Koha::Suggestion object
59
60 =cut
61
62 sub get {
63     my $c = shift->openapi->valid_input or return;
64
65     return try {
66         my $suggestion_id = $c->validation->param('suggestion_id');
67         my $suggestion = $c->objects->find( Koha::Suggestions->new, $suggestion_id );
68
69         unless ($suggestion) {
70             return $c->render(
71                 status  => 404,
72                 openapi => { error => "Suggestion not found." }
73             );
74         }
75
76         return $c->render(
77             status  => 200,
78             openapi => $suggestion
79         );
80     }
81     catch {
82         $c->unhandled_exception($_);
83     };
84 }
85
86 =head3 add
87
88 Controller method that handles adding a new Koha::Suggestion object
89
90 =cut
91
92 sub add {
93     my $c = shift->openapi->valid_input or return;
94
95     my $body = $c->validation->param('body');
96
97     # FIXME: This should be handled in Koha::Suggestion->store
98     $body->{'status'} = 'ASKED'
99         unless defined $body->{'status'};
100
101     return try {
102         my $suggestion = Koha::Suggestion->new_from_api( $body )->store;
103         $suggestion->discard_changes;
104         $c->res->headers->location( $c->req->url->to_string . '/' . $suggestion->suggestionid );
105
106         return $c->render(
107             status  => 201,
108             openapi => $suggestion->to_api
109         );
110     }
111     catch {
112         $c->unhandled_exception($_);
113     };
114 }
115
116 =head3 update
117
118 Controller method that handles modifying Koha::Suggestion object
119
120 =cut
121
122 sub update {
123     my $c = shift->openapi->valid_input or return;
124
125     my $suggestion_id = $c->validation->param('suggestion_id');
126     my $suggestion = Koha::Suggestions->find( $suggestion_id );
127
128     return $c->render(
129         status  => 404,
130         openapi => { error => 'Suggestion not found.' }
131     ) unless $suggestion;
132
133     return try {
134
135         my $body = $c->validation->param('body');
136
137         $suggestion->set_from_api( $body )->store;
138         $suggestion->discard_changes;
139
140         return $c->render(
141             status  => 200,
142             openapi => $suggestion->to_api
143         );
144     }
145     catch {
146         $c->unhandled_exception($_);
147     };
148
149 }
150
151 =head3 delete
152
153 Controller method that handles removing a Koha::Suggestion object
154
155 =cut
156
157 sub delete {
158     my $c = shift->openapi->valid_input or return;
159
160     my $suggestion_id = $c->validation->param('suggestion_id');
161     my $suggestion = Koha::Suggestions->find( $suggestion_id );
162
163     return $c->render(
164         status  => 404,
165         openapi => { error => 'Suggestion not found.' }
166     ) unless $suggestion;
167
168     return try {
169         $suggestion->delete;
170         return $c->render(
171             status  => 204,
172             openapi => q{}
173         );
174     }
175     catch {
176         $c->unhandled_exception($_);
177     };
178 }
179
180 1;