Bug 16213: Allow to select hold's itemtype when using API
[koha.git] / Koha / REST / V1 / Cities.pm
blob3003e3aefe986b5d04edb4b7cf82f7528a33889d
1 package Koha::REST::V1::Cities;
3 # This file is part of Koha.
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.
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.
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.
18 use Modern::Perl;
20 use Mojo::Base 'Mojolicious::Controller';
22 use Koha::City;
23 use Koha::Cities;
25 use Try::Tiny;
27 sub list {
28 my $c = shift->openapi->valid_input or return;
30 my $cities;
31 my $filter;
32 my $args = $c->req->params->to_hash;
34 for my $filter_param ( keys %$args ) {
35 $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
38 return try {
39 $cities = Koha::Cities->search($filter);
40 return $c->render( status => 200, openapi => $cities );
42 catch {
43 if ( $_->isa('DBIx::Class::Exception') ) {
44 return $c->render( status => 500,
45 openapi => { error => $_->{msg} } );
47 else {
48 return $c->render( status => 500,
49 openapi => { error => "Something went wrong, check the logs."} );
55 sub get {
56 my $c = shift->openapi->valid_input or return;
58 my $city = Koha::Cities->find( $c->validation->param('cityid') );
59 unless ($city) {
60 return $c->render( status => 404,
61 openapi => { error => "City not found" } );
64 return $c->render( status => 200, openapi => $city );
67 sub add {
68 my $c = shift->openapi->valid_input or return;
70 my $city = Koha::City->new( $c->validation->param('body') );
72 return try {
73 $city->store;
74 return $c->render( status => 200, openapi => $city );
76 catch {
77 if ( $_->isa('DBIx::Class::Exception') ) {
78 return $c->render( status => 500,
79 openapi => { error => $_->{msg} } );
81 else {
82 return $c->render( status => 500,
83 openapi => { error => "Something went wrong, check the logs."} );
88 sub update {
89 my $c = shift->openapi->valid_input or return;
91 my $city;
93 return try {
94 $city = Koha::Cities->find( $c->validation->param('cityid') );
95 my $params = $c->req->json;
96 $city->set( $params );
97 $city->store();
98 return $c->render( status => 200, openapi => $city );
100 catch {
101 if ( not defined $city ) {
102 return $c->render( status => 404,
103 openapi => { error => "Object not found" } );
105 elsif ( $_->isa('Koha::Exceptions::Object') ) {
106 return $c->render( status => 500,
107 openapi => { error => $_->message } );
109 else {
110 return $c->render( status => 500,
111 openapi => { error => "Something went wrong, check the logs."} );
117 sub delete {
118 my $c = shift->openapi->valid_input or return;
120 my $city;
122 return try {
123 $city = Koha::Cities->find( $c->validation->param('cityid') );
124 $city->delete;
125 return $c->render( status => 200, openapi => "" );
127 catch {
128 if ( not defined $city ) {
129 return $c->render( status => 404,
130 openapi => { error => "Object not found" } );
132 elsif ( $_->isa('DBIx::Class::Exception') ) {
133 return $c->render( status => 500,
134 openapi => { error => $_->{msg} } );
136 else {
137 return $c->render( status => 500,
138 openapi => { error => "Something went wrong, check the logs."} );