Bug 22343: API routes for SMTP servers CRUD
[koha.git] / Koha / REST / V1 / Config / SMTP / Servers.pm
1 package Koha::REST::V1::Config::SMTP::Servers;
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::SMTP::Servers;
23
24 use Try::Tiny;
25
26 =head1 API
27
28 =head2 Methods
29
30 =head3 list
31
32 Controller method that handles listing Koha::SMTP::Server objects
33
34 =cut
35
36 sub list {
37     my $c = shift->openapi->valid_input or return;
38
39     return try {
40         my $smtp_servers_set = Koha::SMTP::Servers->new;
41         my $smtp_servers = $c->objects->search( $smtp_servers_set );
42         return $c->render(
43             status  => 200,
44             openapi => $smtp_servers
45         );
46     }
47     catch {
48         $c->unhandled_exception($_);
49     };
50 }
51
52 =head3 get
53
54 Controller method that handles retrieving a single Koha::SMTP::Server object
55
56 =cut
57
58 sub get {
59     my $c = shift->openapi->valid_input or return;
60
61     return try {
62         my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
63
64         unless ($smtp_server) {
65             return $c->render(
66                 status  => 404,
67                 openapi => {
68                     error => "SMTP server not found"
69                 }
70             );
71         }
72
73         my $embed = $c->stash('koha.embed');
74
75         return $c->render(
76             status  => 200,
77             openapi => $smtp_server->to_api({ embed => $embed })
78         );
79     }
80     catch {
81         $c->unhandled_exception($_);
82     }
83 }
84
85 =head3 add
86
87 Controller method that handles adding a new Koha::SMTP::Server object
88
89 =cut
90
91 sub add {
92     my $c = shift->openapi->valid_input or return;
93
94     return try {
95
96         my $smtp_server = Koha::SMTP::Server->new_from_api( $c->validation->param('body') );
97         $smtp_server->store->discard_changes;
98
99         $c->res->headers->location( $c->req->url->to_string . '/' . $smtp_server->id );
100
101         return $c->render(
102             status  => 201,
103             openapi => $smtp_server->to_api
104         );
105     }
106     catch {
107         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
108             return $c->render(
109                 status  => 409,
110                 openapi => {
111                     error    => $_->error,
112                     conflict => $_->duplicate_id
113                 }
114             );
115         }
116
117         $c->unhandled_exception($_);
118     };
119 }
120
121 =head3 update
122
123 Controller method that handles updating a Koha::SMTP::Server object
124
125 =cut
126
127 sub update {
128     my $c = shift->openapi->valid_input or return;
129
130     my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
131
132     if ( not defined $smtp_server ) {
133         return $c->render(
134             status  => 404,
135             openapi => {
136                 error => "Object not found"
137             }
138         );
139     }
140
141     return try {
142         $smtp_server->set_from_api( $c->validation->param('body') );
143         $smtp_server->store->discard_changes;
144
145         return $c->render(
146             status  => 200,
147             openapi => $smtp_server->to_api
148         );
149     }
150     catch {
151         if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
152             return $c->render(
153                 status  => 409,
154                 openapi => {
155                     error    => $_->error,
156                     conflict => $_->duplicate_id
157                 }
158             );
159         }
160
161         $c->unhandled_exception($_);
162     };
163 }
164
165 =head3 delete
166
167 Controller method that handles deleting a Koha::SMTP::Server object
168
169 =cut
170
171 sub delete {
172     my $c = shift->openapi->valid_input or return;
173
174     my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
175
176     if ( not defined $smtp_server ) {
177         return $c->render( status  => 404,
178                            openapi => { error => "Object not found" } );
179     }
180
181     return try {
182         $smtp_server->delete;
183
184         return $c->render(
185             status  => 204,
186             openapi => q{}
187         );
188     }
189     catch {
190         $c->unhandled_exception($_);
191     };
192 }
193
194 1;