8720a426292a5c2d6a7272a8db1ecd3598cc5365
[srvgit] / 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
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::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         $c->unhandled_exception($_);
53     };
54 }
55
56 =head3 get
57
58 Controller function that handles retrieving a single Koha::Acquisition::Bookseller
59
60 =cut
61
62 sub get {
63     my $c = shift->openapi->valid_input or return;
64
65     my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
66     unless ($vendor) {
67         return $c->render( status  => 404,
68                            openapi => { error => "Vendor not found" } );
69     }
70
71     return try {
72         return $c->render(
73             status  => 200,
74             openapi => $vendor->to_api
75         );
76     }
77     catch {
78         $c->unhandled_exception($_);
79     };
80 }
81
82 =head3 add
83
84 Controller function that handles adding a new Koha::Acquisition::Bookseller object
85
86 =cut
87
88 sub add {
89     my $c = shift->openapi->valid_input or return;
90
91     my $vendor = Koha::Acquisition::Bookseller->new_from_api( $c->validation->param('body') );
92
93     return try {
94         $vendor->store;
95         $c->res->headers->location($c->req->url->to_string . '/' . $vendor->id );
96         return $c->render(
97             status  => 201,
98             openapi => $vendor->to_api
99         );
100     }
101     catch {
102         $c->unhandled_exception($_);
103     };
104 }
105
106 =head3 update
107
108 Controller function that handles updating a Koha::Acquisition::Bookseller object
109
110 =cut
111
112 sub update {
113     my $c = shift->openapi->valid_input or return;
114
115     my $vendor;
116
117     return try {
118         $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
119         $vendor->set_from_api( $c->validation->param('body') );
120         $vendor->store();
121         return $c->render(
122             status  => 200,
123             openapi => $vendor->to_api
124         );
125     }
126     catch {
127         if ( not defined $vendor ) {
128             return $c->render(
129                 status  => 404,
130                 openapi => { error => "Object not found" }
131             );
132         }
133
134         $c->unhandled_exception($_);
135
136     };
137
138 }
139
140 =head3 delete
141
142 Controller function that handles deleting a Koha::Acquisition::Bookseller object
143
144 =cut
145
146 sub delete {
147     my $c = shift->openapi->valid_input or return;
148
149     return try {
150         my $vendor = Koha::Acquisition::Booksellers->find( $c->validation->param('vendor_id') );
151
152         unless ( $vendor ) {
153             return $c->render(
154                 status  => 404,
155                 openapi => { error => "Object not found" }
156             );
157         }
158
159         $vendor->delete;
160
161         return $c->render(
162             status  => 204,
163             openapi => q{}
164         );
165     }
166     catch {
167         $c->unhandled_exception($_);
168     };
169 }
170
171 1;