0cb33fbf283066f0bfe3461cea89bbb5d1f95ec9
[koha.git] / Koha / REST / V1 / Acquisitions / Vendors.pm
1 package Koha::REST::V1::Acquisitions::Vendors;
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 along
15 # with Koha; if not, write to the Free Software Foundation, Inc.,
16 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Acquisition::Booksellers;
23
24 use Try::Tiny;
25
26 =head1 NAME
27
28 Koha::REST::V1::Acquisitions::Vendors
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 list
35
36 Controller function that handles listing Koha::Acquisition::Bookseller objects
37
38 =cut
39
40 sub list {
41     my $c = shift->openapi->valid_input or return;
42
43     return try {
44         my $vendors_rs = Koha::Acquisition::Booksellers->new;
45         my $vendors    = $c->objects->search( $vendors_rs );
46         return $c->render(
47             status  => 200,
48             openapi => $vendors
49         );
50     }
51     catch {
52         if ( $_->isa('DBIx::Class::Exception') ) {
53             return $c->render( status  => 500,
54                                openapi => { error => $_->{msg} } );
55         }
56         else {
57             return $c->render( status  => 500,
58                                openapi => { error => "Something went wrong, check the logs." } );
59         }
60     };
61 }
62
63 =head3 get
64
65 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
66
67 =cut
68
69 sub get {
70     my $c = shift->openapi->valid_input or return;
71
72     my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
73     unless ($vendor) {
74         return $c->render( status  => 404,
75                            openapi => { error => "Vendor not found" } );
76     }
77
78     return $c->render(
79         status  => 200,
80         openapi => $vendor->to_api
81     );
82 }
83
84 =head3 add
85
86 Controller function that handles adding a new Koha::Acquisition::Bookseller object
87
88 =cut
89
90 sub add {
91     my $c = shift->openapi->valid_input or return;
92
93     my $vendor = Koha::Acquisition::Bookseller->new_from_api( $c->validation->param('body') );
94
95     return try {
96         $vendor->store;
97         $c->res->headers->location($c->req->url->to_string . '/' . $vendor->id );
98         return $c->render(
99             status  => 201,
100             openapi => $vendor->to_api
101         );
102     }
103     catch {
104         if ( $_->isa('DBIx::Class::Exception') ) {
105             return $c->render( status  => 500,
106                                openapi => { error => $_->msg } );
107         }
108         else {
109             return $c->render( status  => 500,
110                                openapi => { error => "Something went wrong, check the logs." } );
111         }
112     };
113 }
114
115 =head3 update
116
117 Controller function that handles updating a Koha::Acquisition::Bookseller object
118
119 =cut
120
121 sub update {
122     my $c = shift->openapi->valid_input or return;
123
124     my $vendor;
125
126     return try {
127         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
128         $vendor->set_from_api( $c->validation->param('body') );
129         $vendor->store();
130         return $c->render(
131             status  => 200,
132             openapi => $vendor->to_api
133         );
134     }
135     catch {
136         if ( not defined $vendor ) {
137             return $c->render( status  => 404,
138                                openapi => { error => "Object not found" } );
139         }
140         elsif ( $_->isa('Koha::Exceptions::Object') ) {
141             return $c->render( status  => 500,
142                                openapi => { error => $_->message } );
143         }
144         else {
145             return $c->render( status  => 500,
146                                openapi => { error => "Something went wrong, check the logs." } );
147         }
148     };
149
150 }
151
152 =head3 delete
153
154 Controller function that handles deleting a Koha::Acquisition::Bookseller object
155
156 =cut
157
158 sub delete {
159     my $c = shift->openapi->valid_input or return;
160
161     my $vendor;
162
163     return try {
164         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
165         $vendor->delete;
166         return $c->render( status => 200,
167                            openapi => q{} );
168     }
169     catch {
170         if ( not defined $vendor ) {
171             return $c->render( status  => 404,
172                                openapi => { error => "Object not found" } );
173         }
174         elsif ( $_->isa('DBIx::Class::Exception') ) {
175             return $c->render( status  => 500,
176                                openapi => { error => $_->msg } );
177         }
178         else {
179             return $c->render( status  => 500,
180                                openapi => { error => "Something went wrong, check the logs." } );
181         }
182     };
183
184 }
185
186 1;