1 package Koha::REST::V1::Config::SMTP::Servers;
3 # This file is part of Koha.
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.
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.
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>.
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::SMTP::Servers;
24 use Try::Tiny qw( catch try );
32 Controller method that handles listing Koha::SMTP::Server objects
37 my $c = shift->openapi->valid_input or return;
40 my $smtp_servers_set = Koha::SMTP::Servers->new;
41 my $smtp_servers = $c->objects->search( $smtp_servers_set );
44 openapi => $smtp_servers
48 $c->unhandled_exception($_);
54 Controller method that handles retrieving a single Koha::SMTP::Server object
59 my $c = shift->openapi->valid_input or return;
62 my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
64 unless ($smtp_server) {
68 error => "SMTP server not found"
73 my $embed = $c->stash('koha.embed');
77 openapi => $smtp_server->to_api({ embed => $embed })
81 $c->unhandled_exception($_);
87 Controller method that handles adding a new Koha::SMTP::Server object
92 my $c = shift->openapi->valid_input or return;
96 my $smtp_server = Koha::SMTP::Server->new_from_api( $c->validation->param('body') );
97 $smtp_server->store->discard_changes;
99 $c->res->headers->location( $c->req->url->to_string . '/' . $smtp_server->id );
103 openapi => $smtp_server->to_api
107 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
112 conflict => $_->duplicate_id
117 $c->unhandled_exception($_);
123 Controller method that handles updating a Koha::SMTP::Server object
128 my $c = shift->openapi->valid_input or return;
130 my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
132 if ( not defined $smtp_server ) {
136 error => "Object not found"
142 $smtp_server->set_from_api( $c->validation->param('body') );
143 $smtp_server->store->discard_changes;
147 openapi => $smtp_server->to_api
151 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
156 conflict => $_->duplicate_id
161 $c->unhandled_exception($_);
167 Controller method that handles deleting a Koha::SMTP::Server object
172 my $c = shift->openapi->valid_input or return;
174 my $smtp_server = Koha::SMTP::Servers->find( $c->validation->param('smtp_server_id') );
176 if ( not defined $smtp_server ) {
177 return $c->render( status => 404,
178 openapi => { error => "Object not found" } );
182 $smtp_server->delete;
190 $c->unhandled_exception($_);