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

(-)a/Koha/REST/V1/Cities.pm (-31 / +40 lines)
Lines 25-35 use Koha::Cities; Link Here
25
use Try::Tiny;
25
use Try::Tiny;
26
26
27
sub list {
27
sub list {
28
    my ( $c, $args, $cb ) = @_;
28
    my $c = shift->openapi->valid_input or return;
29
29
30
    my $cities;
30
    my $cities;
31
    my $filter;
31
    my $filter;
32
    $args //= {};
32
    my $args = $c->req->params->to_hash;
33
33
34
    for my $filter_param ( keys %$args ) {
34
    for my $filter_param ( keys %$args ) {
35
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
35
        $filter->{$filter_param} = { LIKE => $args->{$filter_param} . "%" };
Lines 37-132 sub list { Link Here
37
37
38
    return try {
38
    return try {
39
        $cities = Koha::Cities->search($filter);
39
        $cities = Koha::Cities->search($filter);
40
        return $c->$cb( $cities, 200 );
40
        return $c->render( status => 200, openapi => $cities );
41
    }
41
    }
42
    catch {
42
    catch {
43
        if ( $_->isa('DBIx::Class::Exception') ) {
43
        if ( $_->isa('DBIx::Class::Exception') ) {
44
            return $c->$cb( { error => $_->{msg} }, 500 );
44
            return $c->render( status  => 500,
45
                               openapi => { error => $_->msg } );
45
        }
46
        }
46
        else {
47
        else {
47
            return $c->$cb(
48
            return $c->render( status => 500,
48
                { error => "Something went wrong, check the logs." }, 500 );
49
                openapi => { error => "Something went wrong, check the logs."} );
49
        }
50
        }
50
    };
51
    };
52
51
}
53
}
52
54
53
sub get {
55
sub get {
54
    my ( $c, $args, $cb ) = @_;
56
    my $c = shift->openapi->valid_input or return;
55
57
56
    my $city = Koha::Cities->find( $args->{cityid} );
58
    my $city = Koha::Cities->find( $c->validation->param('cityid') );
57
    unless ($city) {
59
    unless ($city) {
58
        return $c->$cb( { error => "City not found" }, 404 );
60
        return $c->render( status  => 404,
61
                           openapi => { error => "City not found" } );
59
    }
62
    }
60
63
61
    return $c->$cb( $city, 200 );
64
    return $c->render( status => 200, openapi => $city );
62
}
65
}
63
66
64
sub add {
67
sub add {
65
    my ( $c, $args, $cb ) = @_;
68
    my $c = shift->openapi->valid_input or return;
66
69
67
    my $city = Koha::City->new( $args->{body} );
70
    my $city = Koha::City->new( $c->validation->param('body') );
68
71
69
    return try {
72
    return try {
70
        $city->store;
73
        $city->store;
71
        return $c->$cb( $city, 200 );
74
        return $c->render( status => 200, openapi => $city );
72
    }
75
    }
73
    catch {
76
    catch {
74
        if ( $_->isa('DBIx::Class::Exception') ) {
77
        if ( $_->isa('DBIx::Class::Exception') ) {
75
            return $c->$cb( { error => $_->msg }, 500 );
78
            return $c->render( status  => 500,
79
                               openapi => { error => $_->message } );
76
        }
80
        }
77
        else {
81
        else {
78
            return $c->$cb(
82
            return $c->render( status => 500,
79
                { error => "Something went wrong, check the logs." }, 500 );
83
                openapi => { error => "Something went wrong, check the logs."} );
80
        }
84
        }
81
    };
85
    };
82
}
86
}
83
87
84
sub update {
88
sub update {
85
    my ( $c, $args, $cb ) = @_;
89
    my $c = shift->openapi->valid_input or return;
86
90
87
    my $city;
91
    my $city;
88
92
89
    return try {
93
    return try {
90
        $city = Koha::Cities->find( $args->{cityid} );
94
        $city = Koha::Cities->find( $c->validation->param('cityid') );
91
        $city->set( $args->{body} );
95
        my $params = $c->req->json;
96
        $city->set( $params );
92
        $city->store();
97
        $city->store();
93
        return $c->$cb( $city, 200 );
98
        return $c->render( status => 200, openapi => $city );
94
    }
99
    }
95
    catch {
100
    catch {
96
        if ( not defined $city ) {
101
        if ( not defined $city ) {
97
            return $c->$cb( { error => "Object not found" }, 404 );
102
            return $c->render( status  => 404,
103
                               openapi => { error => "Object not found" } );
98
        }
104
        }
99
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
105
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
100
            return $c->$cb( { error => $_->message }, 500 );
106
            return $c->render( status  => 500,
107
                               openapi => { error => $_->message } );
101
        }
108
        }
102
        else {
109
        else {
103
            return $c->$cb(
110
            return $c->render( status => 500,
104
                { error => "Something went wrong, check the logs." }, 500 );
111
                openapi => { error => "Something went wrong, check the logs."} );
105
        }
112
        }
106
    };
113
    };
107
114
108
}
115
}
109
116
110
sub delete {
117
sub delete {
111
    my ( $c, $args, $cb ) = @_;
118
    my $c = shift->openapi->valid_input or return;
112
119
113
    my $city;
120
    my $city;
114
121
115
    return try {
122
    return try {
116
        $city = Koha::Cities->find( $args->{cityid} );
123
        $city = Koha::Cities->find( $c->validation->param('cityid') );
117
        $city->delete;
124
        $city->delete;
118
        return $c->$cb( "", 200 );
125
        return $c->render( status => 200, openapi => "" );
119
    }
126
    }
120
    catch {
127
    catch {
121
        if ( not defined $city ) {
128
        if ( not defined $city ) {
122
            return $c->$cb( { error => "Object not found" }, 404 );
129
            return $c->render( status  => 404,
130
                               openapi => { error => "Object not found" } );
123
        }
131
        }
124
        elsif ( $_->isa('DBIx::Class::Exception') ) {
132
        elsif ( $_->isa('DBIx::Class::Exception') ) {
125
            return $c->$cb( { error => $_->msg }, 500 );
133
            return $c->render( status  => 500,
134
                               openapi => { error => $_->msg } );
126
        }
135
        }
127
        else {
136
        else {
128
            return $c->$cb(
137
            return $c->render( status => 500,
129
                { error => "Something went wrong, check the logs." }, 500 );
138
                openapi => { error => "Something went wrong, check the logs."} );
130
        }
139
        }
131
    };
140
    };
132
141
(-)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