Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[koha.git] / Koha / REST / V1 / Cities.pm
1 package Koha::REST::V1::Cities;
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::Cities;
23
24 use Try::Tiny;
25
26 =head1 API
27
28 =head2 Methods
29
30 =head3 list
31
32 =cut
33
34 sub list {
35     my $c = shift->openapi->valid_input or return;
36
37     return try {
38         my $cities_set = Koha::Cities->new;
39         my $cities = $c->objects->search( $cities_set );
40         return $c->render( status => 200, openapi => $cities );
41     }
42     catch {
43         $c->unhandled_exception($_);
44     };
45
46 }
47
48 =head3 get
49
50 =cut
51
52 sub get {
53     my $c = shift->openapi->valid_input or return;
54
55     return try {
56         my $city = Koha::Cities->find( $c->validation->param('city_id') );
57         unless ($city) {
58             return $c->render( status  => 404,
59                             openapi => { error => "City not found" } );
60         }
61
62         return $c->render( status => 200, openapi => $city->to_api );
63     }
64     catch {
65         $c->unhandled_exception($_);
66     }
67 }
68
69 =head3 add
70
71 =cut
72
73 sub add {
74     my $c = shift->openapi->valid_input or return;
75
76     return try {
77         my $city = Koha::City->new_from_api( $c->validation->param('body') );
78         $city->store;
79         $c->res->headers->location( $c->req->url->to_string . '/' . $city->cityid );
80         return $c->render(
81             status  => 201,
82             openapi => $city->to_api
83         );
84     }
85     catch {
86         $c->unhandled_exception($_);
87     };
88 }
89
90 =head3 update
91
92 =cut
93
94 sub update {
95     my $c = shift->openapi->valid_input or return;
96
97     my $city = Koha::Cities->find( $c->validation->param('city_id') );
98
99     if ( not defined $city ) {
100         return $c->render( status  => 404,
101                            openapi => { error => "Object not found" } );
102     }
103
104     return try {
105         $city->set_from_api( $c->validation->param('body') );
106         $city->store();
107         return $c->render( status => 200, openapi => $city->to_api );
108     }
109     catch {
110         $c->unhandled_exception($_);
111     };
112 }
113
114 =head3 delete
115
116 =cut
117
118 sub delete {
119     my $c = shift->openapi->valid_input or return;
120
121     my $city = Koha::Cities->find( $c->validation->param('city_id') );
122     if ( not defined $city ) {
123         return $c->render( status  => 404,
124                            openapi => { error => "Object not found" } );
125     }
126
127     return try {
128         $city->delete;
129         return $c->render(
130             status  => 204,
131             openapi => q{}
132         );
133     }
134     catch {
135         $c->unhandled_exception($_);
136     };
137 }
138
139 1;