View | Details | Raw Unified | Return to bug 18137
Collapse All | Expand All

(-)a/Koha/REST/V1/Cities.pm (-32 / +41 lines)
Lines 26-133 use Koha::Cities; Link Here
26
use Try::Tiny;
26
use Try::Tiny;
27
27
28
sub list {
28
sub list {
29
    my ( $c, $args, $cb ) = @_;
29
    my $c = shift->openapi->valid_input or return;
30
30
31
    my $cities;
31
    my $cities;
32
    my $filter;
32
    my $filter;
33
    $args //= {};
33
    my $args = $c->req->params->to_hash;
34
34
35
    for my $filter_param ( keys %$args ) {
35
    for my $filter_param ( keys %$args ) {
36
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
36
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
37
    }
37
    }
38
38
39
    return try {
39
    return try {
40
        $cities = Koha::Cities->search($filter)->unblessed;
40
        $cities = Koha::Cities->search($filter);
41
        return $c->$cb( $cities, 200 );
41
        return $c->render( status => 200, openapi => $cities );
42
    }
42
    }
43
    catch {
43
    catch {
44
        if ( $_->isa('DBIx::Class::Exception') ) {
44
        if ( $_->isa('DBIx::Class::Exception') ) {
45
            return $c->$cb( { error => $_->{msg} }, 500 );
45
            return $c->render( status  => 500,
46
                               openapi => { error => $_->msg } );
46
        }
47
        }
47
        else {
48
        else {
48
            return $c->$cb(
49
            return $c->render( status => 500,
49
                { error => "Something went wrong, check the logs." }, 500 );
50
                openapi => { error => "Something went wrong, check the logs."} );
50
        }
51
        }
51
    };
52
    };
53
52
}
54
}
53
55
54
sub get {
56
sub get {
55
    my ( $c, $args, $cb ) = @_;
57
    my $c = shift->openapi->valid_input or return;
56
58
57
    my $city = Koha::Cities->find( $args->{cityid} );
59
    my $city = Koha::Cities->find( $c->validation->param('cityid') );
58
    unless ($city) {
60
    unless ($city) {
59
        return $c->$cb( { error => "City not found" }, 404 );
61
        return $c->render( status  => 404,
62
                           openapi => { error => "City not found" } );
60
    }
63
    }
61
64
62
    return $c->$cb( $city->unblessed, 200 );
65
    return $c->render( status => 200, openapi => $city );
63
}
66
}
64
67
65
sub add {
68
sub add {
66
    my ( $c, $args, $cb ) = @_;
69
    my $c = shift->openapi->valid_input or return;
67
70
68
    my $city = Koha::City->new( $args->{body} );
71
    my $city = Koha::City->new( $c->validation->param('body') );
69
72
70
    return try {
73
    return try {
71
        $city->store;
74
        $city->store;
72
        return $c->$cb( $city->unblessed, 200 );
75
        return $c->render( status => 200, openapi => $city );
73
    }
76
    }
74
    catch {
77
    catch {
75
        if ( $_->isa('DBIx::Class::Exception') ) {
78
        if ( $_->isa('DBIx::Class::Exception') ) {
76
            return $c->$cb( { error => $_->msg }, 500 );
79
            return $c->render( status  => 500,
80
                               openapi => { error => $_->message } );
77
        }
81
        }
78
        else {
82
        else {
79
            return $c->$cb(
83
            return $c->render( status => 500,
80
                { error => "Something went wrong, check the logs." }, 500 );
84
                openapi => { error => "Something went wrong, check the logs."} );
81
        }
85
        }
82
    };
86
    };
83
}
87
}
84
88
85
sub update {
89
sub update {
86
    my ( $c, $args, $cb ) = @_;
90
    my $c = shift->openapi->valid_input or return;
87
91
88
    my $city;
92
    my $city;
89
93
90
    return try {
94
    return try {
91
        $city = Koha::Cities->find( $args->{cityid} );
95
        $city = Koha::Cities->find( $c->validation->param('cityid') );
92
        $city->set( $args->{body} );
96
        my $params = $c->req->json;
97
        $city->set( $params );
93
        $city->store();
98
        $city->store();
94
        return $c->$cb( $city->unblessed, 200 );
99
        return $c->render( status => 200, openapi => $city );
95
    }
100
    }
96
    catch {
101
    catch {
97
        if ( not defined $city ) {
102
        if ( not defined $city ) {
98
            return $c->$cb( { error => "Object not found" }, 404 );
103
            return $c->render( status  => 404,
104
                               openapi => { error => "Object not found" } );
99
        }
105
        }
100
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
106
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
101
            return $c->$cb( { error => $_->message }, 500 );
107
            return $c->render( status  => 500,
108
                               openapi => { error => $_->message } );
102
        }
109
        }
103
        else {
110
        else {
104
            return $c->$cb(
111
            return $c->render( status => 500,
105
                { error => "Something went wrong, check the logs." }, 500 );
112
                openapi => { error => "Something went wrong, check the logs."} );
106
        }
113
        }
107
    };
114
    };
108
115
109
}
116
}
110
117
111
sub delete {
118
sub delete {
112
    my ( $c, $args, $cb ) = @_;
119
    my $c = shift->openapi->valid_input or return;
113
120
114
    my $city;
121
    my $city;
115
122
116
    return try {
123
    return try {
117
        $city = Koha::Cities->find( $args->{cityid} );
124
        $city = Koha::Cities->find( $c->validation->param('cityid') );
118
        $city->delete;
125
        $city->delete;
119
        return $c->$cb( "", 200 );
126
        return $c->render( status => 200, openapi => "" );
120
    }
127
    }
121
    catch {
128
    catch {
122
        if ( not defined $city ) {
129
        if ( not defined $city ) {
123
            return $c->$cb( { error => "Object not found" }, 404 );
130
            return $c->render( status  => 404,
131
                               openapi => { error => "Object not found" } );
124
        }
132
        }
125
        elsif ( $_->isa('DBIx::Class::Exception') ) {
133
        elsif ( $_->isa('DBIx::Class::Exception') ) {
126
            return $c->$cb( { error => $_->msg }, 500 );
134
            return $c->render( status  => 500,
135
                               openapi => { error => $_->msg } );
127
        }
136
        }
128
        else {
137
        else {
129
            return $c->$cb(
138
            return $c->render( status => 500,
130
                { error => "Something went wrong, check the logs." }, 500 );
139
                openapi => { error => "Something went wrong, check the logs."} );
131
        }
140
        }
132
    };
141
    };
133
142
(-)a/api/v1/swagger/paths/cities.json (-11 / +53 lines)
Lines 1-7 Link Here
1
{
1
{
2
  "/cities": {
2
  "/cities": {
3
    "get": {
3
    "get": {
4
      "x-mojo-controller": "Koha::REST::V1::Cities",
4
      "x-mojo-to": "Cities#list",
5
      "operationId": "list",
5
      "operationId": "list",
6
      "tags": ["cities"],
6
      "tags": ["cities"],
7
      "produces": [
7
      "produces": [
Lines 53-63 Link Here
53
          "schema": {
53
          "schema": {
54
            "$ref": "../definitions.json#/error"
54
            "$ref": "../definitions.json#/error"
55
          }
55
          }
56
        },
57
        "503": {
58
          "description": "Under maintenance",
59
          "schema": {
60
            "$ref": "../definitions.json#/error"
61
          }
56
        }
62
        }
57
      }
63
      }
58
    },
64
    },
59
    "post": {
65
    "post": {
60
      "x-mojo-controller": "Koha::REST::V1::Cities",
66
      "x-mojo-to": "Cities#add",
61
      "operationId": "add",
67
      "operationId": "add",
62
      "tags": ["cities"],
68
      "tags": ["cities"],
63
      "parameters": [{
69
      "parameters": [{
Lines 79-84 Link Here
79
            "$ref": "../definitions.json#/city"
85
            "$ref": "../definitions.json#/city"
80
          }
86
          }
81
        },
87
        },
88
        "401": {
89
          "description": "Authentication required",
90
          "schema": {
91
            "$ref": "../definitions.json#/error"
92
          }
93
        },
82
        "403": {
94
        "403": {
83
          "description": "Access forbidden",
95
          "description": "Access forbidden",
84
          "schema": {
96
          "schema": {
Lines 90-95 Link Here
90
          "schema": {
102
          "schema": {
91
            "$ref": "../definitions.json#/error"
103
            "$ref": "../definitions.json#/error"
92
          }
104
          }
105
        },
106
        "503": {
107
          "description": "Under maintenance",
108
          "schema": {
109
            "$ref": "../definitions.json#/error"
110
          }
93
        }
111
        }
94
      },
112
      },
95
      "x-koha-authorization": {
113
      "x-koha-authorization": {
Lines 101-107 Link Here
101
  },
119
  },
102
  "/cities/{cityid}": {
120
  "/cities/{cityid}": {
103
    "get": {
121
    "get": {
104
      "x-mojo-controller": "Koha::REST::V1::Cities",
122
      "x-mojo-to": "Cities#get",
105
      "operationId": "get",
123
      "operationId": "get",
106
      "tags": ["cities"],
124
      "tags": ["cities"],
107
      "parameters": [{
125
      "parameters": [{
Lines 117-128 Link Here
117
            "$ref": "../definitions.json#/city"
135
            "$ref": "../definitions.json#/city"
118
          }
136
          }
119
        },
137
        },
120
        "403": {
121
          "description": "Access forbidden",
122
          "schema": {
123
            "$ref": "../definitions.json#/error"
124
          }
125
        },
126
        "404": {
138
        "404": {
127
          "description": "City not found",
139
          "description": "City not found",
128
          "schema": {
140
          "schema": {
Lines 134-144 Link Here
134
          "schema": {
146
          "schema": {
135
            "$ref": "../definitions.json#/error"
147
            "$ref": "../definitions.json#/error"
136
          }
148
          }
149
        },
150
        "503": {
151
          "description": "Under maintenance",
152
          "schema": {
153
            "$ref": "../definitions.json#/error"
154
          }
137
        }
155
        }
138
      }
156
      }
139
    },
157
    },
140
    "put": {
158
    "put": {
141
      "x-mojo-controller": "Koha::REST::V1::Cities",
159
      "x-mojo-to": "Cities#update",
142
      "operationId": "update",
160
      "operationId": "update",
143
      "tags": ["cities"],
161
      "tags": ["cities"],
144
      "parameters": [{
162
      "parameters": [{
Lines 162-167 Link Here
162
            "$ref": "../definitions.json#/city"
180
            "$ref": "../definitions.json#/city"
163
          }
181
          }
164
        },
182
        },
183
        "401": {
184
          "description": "Authentication required",
185
          "schema": {
186
            "$ref": "../definitions.json#/error"
187
          }
188
        },
165
        "403": {
189
        "403": {
166
          "description": "Access forbidden",
190
          "description": "Access forbidden",
167
          "schema": {
191
          "schema": {
Lines 179-184 Link Here
179
          "schema": {
203
          "schema": {
180
            "$ref": "../definitions.json#/error"
204
            "$ref": "../definitions.json#/error"
181
          }
205
          }
206
        },
207
        "503": {
208
          "description": "Under maintenance",
209
          "schema": {
210
            "$ref": "../definitions.json#/error"
211
          }
182
        }
212
        }
183
      },
213
      },
184
      "x-koha-authorization": {
214
      "x-koha-authorization": {
Lines 188-194 Link Here
188
      }
218
      }
189
    },
219
    },
190
    "delete": {
220
    "delete": {
191
      "x-mojo-controller": "Koha::REST::V1::Cities",
221
      "x-mojo-to": "Cities#delete",
192
      "operationId": "delete",
222
      "operationId": "delete",
193
      "tags": ["cities"],
223
      "tags": ["cities"],
194
      "parameters": [{
224
      "parameters": [{
Lines 204-209 Link Here
204
            "type": "string"
234
            "type": "string"
205
          }
235
          }
206
        },
236
        },
237
        "401": {
238
          "description": "Authentication required",
239
          "schema": {
240
            "$ref": "../definitions.json#/error"
241
          }
242
        },
207
        "403": {
243
        "403": {
208
          "description": "Access forbidden",
244
          "description": "Access forbidden",
209
          "schema": {
245
          "schema": {
Lines 221-226 Link Here
221
          "schema": {
257
          "schema": {
222
            "$ref": "../definitions.json#/error"
258
            "$ref": "../definitions.json#/error"
223
          }
259
          }
260
        },
261
        "503": {
262
          "description": "Under maintenance",
263
          "schema": {
264
            "$ref": "../definitions.json#/error"
265
          }
224
        }
266
        }
225
      },
267
      },
226
      "x-koha-authorization": {
268
      "x-koha-authorization": {
(-)a/t/db_dependent/api/v1/cities.t (-2 / +1 lines)
Lines 332-338 subtest 'delete() tests' => sub { Link Here
332
    $tx->req->cookies(
332
    $tx->req->cookies(
333
        { name => 'CGISESSID', value => $authorized_session_id } );
333
        { name => 'CGISESSID', value => $authorized_session_id } );
334
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
334
    $tx->req->env( { REMOTE_ADDR => $remote_address } );
335
    $t->request_ok($tx)->status_is(200)->content_is('');
335
    $t->request_ok($tx)->status_is(200)->content_is('""');
336
336
337
    $tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" );
337
    $tx = $t->ua->build_tx( DELETE => "/api/v1/cities/$city_id" );
338
    $tx->req->cookies(
338
    $tx->req->cookies(
339
- 

Return to bug 18137