Bug 30719: (QA follow-up) Squash:
[koha.git] / Koha / REST / V1 / IllbatchStatuses.pm
1 package Koha::REST::V1::IllbatchStatuses;
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::Controller';
21
22 use Koha::IllbatchStatuses;
23
24 =head1 NAME
25
26 Koha::REST::V1::IllbatchStatuses
27
28 =head2 Operations
29
30 =head3 list
31
32 Return a list of available ILL batch statuses
33
34 =cut
35
36 sub list {
37     my $c = shift->openapi->valid_input;
38
39     my @statuses = Koha::IllbatchStatuses->search()->as_list;
40
41     return $c->render( status => 200, openapi => \@statuses );
42 }
43
44 =head3 get
45
46 Get one batch statuses
47
48 =cut
49
50 sub get {
51     my $c = shift->openapi->valid_input;
52
53     my $status_code = $c->validation->param('illbatchstatus_code');
54
55     my $status = Koha::IllbatchStatuses->find( { code => $status_code } );
56
57     if ( not defined $status ) {
58         return $c->render(
59             status  => 404,
60             openapi => { error => "ILL batch status not found" }
61         );
62     }
63
64     return $c->render(
65         status  => 200,
66         openapi => { %{ $status->unblessed } }
67     );
68 }
69
70 =head3 add
71
72 Add a new batch status
73
74 =cut
75
76 sub add {
77     my $c = shift->openapi->valid_input or return;
78
79     my $body = $c->validation->param('body');
80
81     my $status = Koha::IllbatchStatus->new($body);
82
83     return try {
84         my $return = $status->create_and_log;
85         if ( $return && $return->{error} ) {
86             return $c->render(
87                 status  => 500,
88                 openapi => $return
89             );
90         } else {
91             return $c->render(
92                 status  => 201,
93                 openapi => $status
94             );
95         }
96     } catch {
97         $c->unhandled_exception($_);
98     };
99 }
100
101 =head3 update
102
103 Update a batch status
104
105 =cut
106
107 sub update {
108     my $c = shift->openapi->valid_input or return;
109
110     my $status = Koha::IllbatchStatuses->find( { code => $c->validation->param('illbatchstatus_code') } );
111
112     if ( not defined $status ) {
113         return $c->render(
114             status  => 404,
115             openapi => { error => "ILL batch status not found" }
116         );
117     }
118
119     my $params = $c->req->json;
120
121     return try {
122
123         # Only permit updating of name
124         $status->update_and_log( { name => $params->{name} } );
125
126         return $c->render(
127             status  => 200,
128             openapi => $status
129         );
130     } catch {
131         $c->unhandled_exception($_);
132     };
133 }
134
135 =head3 delete
136
137 Delete a batch status
138
139 =cut
140
141 sub delete {
142
143     my $c = shift->openapi->valid_input or return;
144
145     my $status = Koha::IllbatchStatuses->find( { code => $c->validation->param('illbatchstatus_code') } );
146
147     if ( not defined $status ) {
148         return $c->render( status => 404, openapi => { errors => [ { message => "ILL batch status not found" } ] } );
149     }
150
151     if ( $status->is_system ) {
152         return $c->render(
153             status  => 400,
154             openapi => { errors => [ { message => "ILL batch status cannot be deleted" } ] }
155         );
156     }
157
158     return try {
159         $status->delete_and_log;
160         return $c->render( status => 204, openapi => '' );
161     } catch {
162         $c->unhandled_exception($_);
163     };
164 }
165
166 1;