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

(-)a/Koha/REST/V1/Items/Messages.pm (+152 lines)
Line 0 Link Here
1
package Koha::REST::V1::Items::Messages;
2
3
# This file is part of Koha.
4
#
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.
9
#
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.
13
#
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.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Koha::Items;
23
use Koha::Item::Message;
24
use Koha::Item::Messages;
25
use Koha::DateUtils;
26
27
use Try::Tiny;
28
29
sub list_item_messages {
30
    my ( $c, $args, $cb ) = @_;
31
32
    if ( !Koha::Items->search({ itemnumber => $args->{itemnumber} })->count > 0 ) {
33
        return $c->$cb( { error => 'Item not found' }, 400 );
34
    }
35
36
    return try {
37
        my $item_messages = Koha::Item::Messages->search({ itemnumber => $args->{itemnumber} });
38
        return $c->$cb( $item_messages, 200 );
39
    }
40
    catch {
41
        if ( $_->isa('DBIx::Class::Exception') ) {
42
            return $c->$cb( { error => $_->{msg} }, 500 );
43
        }
44
        else {
45
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
46
        }
47
    };
48
}
49
50
sub get_item_message {
51
    my ( $c, $args, $cb ) = @_;
52
53
    if ( !Koha::Items->search( { itemnumber => $args->{itemnumber} } )->count > 0 ) {
54
        return $c->$cb( { error => 'Item not found' }, 400 );
55
    }
56
57
    return try {
58
        my $item_message
59
            = Koha::Item::Messages->search(
60
            { itemnumber => $args->{itemnumber}, item_message_id => $args->{item_message_id} } )
61
            ->next;
62
        if ($item_message) {
63
            return $c->$cb( $item_message, 200 );
64
        }
65
        else {
66
            return $c->$cb( { error => 'Item message not found' }, 400 );
67
        }
68
    }
69
    catch {
70
        if ( $_->isa('DBIx::Class::Exception') ) {
71
            return $c->$cb( { error => $_->{msg} }, 500 );
72
        }
73
        else {
74
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
75
        }
76
    };
77
}
78
79
sub add_item_message {
80
    my ( $c, $args, $cb ) = @_;
81
82
    my $item_message = Koha::Item::Message->new( $args->{body} );
83
    $item_message->itemnumber( $args->{itemnumber} );
84
85
    return try {
86
        $item_message->store();
87
        $item_message = Koha::Item::Messages->find( $item_message->id );
88
        return $c->$cb( $item_message, 200 );
89
    }
90
    catch {
91
        if ( $_->isa('DBIx::Class::Exception') ) {
92
            return $c->$cb( { error => $_->msg }, 500 );
93
        }
94
        else {
95
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
96
        }
97
    };
98
}
99
100
sub update_item_message {
101
    my ( $c, $args, $cb ) = @_;
102
103
    my $item_message;
104
105
    return try {
106
        $item_message
107
            = Koha::Item::Messages->search(
108
            { itemnumber => $args->{itemnumber}, item_message_id => $args->{item_message_id} } )
109
            ->next;
110
        $item_message->set( $args->{body} );
111
        $item_message->store();
112
        return $c->$cb( $item_message, 200 );
113
    }
114
    catch {
115
        if ( not defined $item_message ) {
116
            return $c->$cb( { error => "Item message not found" }, 404 );
117
        }
118
        elsif ( $_->isa('Koha::Exceptions::Object') ) {
119
            return $c->$cb( { error => $_->message }, 500 );
120
        }
121
        else {
122
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
123
        }
124
    };
125
126
}
127
128
sub delete_item_message {
129
    my ( $c, $args, $cb ) = @_;
130
131
    my $item_message;
132
133
    return try {
134
        $item_message = Koha::Item::Messages->search(
135
            { itemnumber => $args->{itemnumber}, item_message_id => $args->{item_message_id} } );
136
        $item_message->delete;
137
        return $c->$cb( q{}, 200 );
138
    }
139
    catch {
140
        if ( not defined $item_message ) {
141
            return $c->$cb( { error => "Item message not found" }, 404 );
142
        }
143
        elsif ( $_->isa('DBIx::Class::Exception') ) {
144
            return $c->$cb( { error => $_->msg }, 500 );
145
        }
146
        else {
147
            return $c->$cb( { error => "Something went wrong, check the logs." }, 500 );
148
        }
149
    };
150
}
151
152
1;
(-)a/api/v1/swagger/definitions.json (+3 lines)
Lines 11-16 Link Here
11
  "hold": {
11
  "hold": {
12
    "$ref": "definitions/hold.json"
12
    "$ref": "definitions/hold.json"
13
  },
13
  },
14
  "item_message": {
15
    "$ref": "definitions/item_message.json"
16
  },
14
  "error": {
17
  "error": {
15
    "$ref": "definitions/error.json"
18
    "$ref": "definitions/error.json"
16
  }
19
  }
(-)a/api/v1/swagger/definitions/item_message.json (+25 lines)
Line 0 Link Here
1
{
2
  "type": "object",
3
  "properties": {
4
    "item_message_id": {
5
      "$ref": "../x-primitives.json#/item_message_id"
6
    },
7
    "itemnumber": {
8
      "$ref": "../x-primitives.json#/itemnumber"
9
    },
10
    "type": {
11
      "type": ["string", "null"],
12
      "description": "date and time the item message was created"
13
    },
14
    "message": {
15
      "type": "string",
16
      "description": "date and time the item message was created"
17
    },
18
    "created_on": {
19
      "type": "string",
20
      "description": "date and time the item message was created"
21
    }
22
  },
23
  "additionalProperties": false,
24
  "required": ["itemnumber","message"]
25
}
(-)a/api/v1/swagger/parameters.json (+6 lines)
Lines 10-14 Link Here
10
  },
10
  },
11
  "holdIdPathParam": {
11
  "holdIdPathParam": {
12
    "$ref": "parameters/hold.json#/holdIdPathParam"
12
    "$ref": "parameters/hold.json#/holdIdPathParam"
13
  },
14
  "itemIdPathParam": {
15
    "$ref": "parameters/item.json#/itemIdPathParam"
16
  },
17
  "itemMessageIdPathParam": {
18
    "$ref": "parameters/item_message.json#/itemMessageIdPathParam"
13
  }
19
  }
14
}
20
}
(-)a/api/v1/swagger/parameters/item.json (+9 lines)
Line 0 Link Here
1
{
2
  "itemIdPathParam": {
3
    "name": "itemnumber",
4
    "in": "path",
5
    "description": "Internal item identifier",
6
    "required": true,
7
    "type": "integer"
8
  }
9
}
(-)a/api/v1/swagger/parameters/item_message.json (+9 lines)
Line 0 Link Here
1
{
2
  "itemMessageIdPathParam": {
3
    "name": "item_message_id",
4
    "in": "path",
5
    "description": "Internal item message identifier",
6
    "required": true,
7
    "type": "integer"
8
  }
9
}
(-)a/api/v1/swagger/paths.json (+6 lines)
Lines 11-16 Link Here
11
  "/holds/{reserve_id}": {
11
  "/holds/{reserve_id}": {
12
    "$ref": "paths/holds.json#/~1holds~1{reserve_id}"
12
    "$ref": "paths/holds.json#/~1holds~1{reserve_id}"
13
  },
13
  },
14
  "/items/{itemnumber}/messages": {
15
    "$ref": "paths/item_messages.json#/~1items~1{itemnumber}~1messages"
16
  },
17
  "/items/{itemnumber}/messages/{item_message_id}": {
18
    "$ref": "paths/item_messages.json#/~1items~1{itemnumber}~1messages~1{item_message_id}"
19
  },
14
  "/patrons": {
20
  "/patrons": {
15
    "$ref": "paths/patrons.json#/~1patrons"
21
    "$ref": "paths/patrons.json#/~1patrons"
16
  },
22
  },
(-)a/api/v1/swagger/paths/item_messages.json (+255 lines)
Line 0 Link Here
1
{
2
  "/items/{itemnumber}/messages": {
3
    "get": {
4
      "x-mojo-controller": "Koha::REST::V1::Items::Messages",
5
      "operationId": "listItemMessages",
6
      "tags": ["items", "item_messages"],
7
      "parameters": [
8
        {
9
          "$ref": "../parameters.json#/itemIdPathParam"
10
        }
11
      ],
12
      "produces": [
13
        "application/json"
14
      ],
15
      "responses": {
16
        "200": {
17
          "description": "A list of item messages",
18
          "schema": {
19
            "type": "array",
20
            "items": {
21
              "$ref": "../definitions.json#/item_message"
22
            }
23
          }
24
        },
25
        "403": {
26
            "description": "Access forbidden",
27
            "schema": {
28
              "$ref": "../definitions.json#/error"
29
            }
30
        },
31
        "404": {
32
          "description": "Object not found",
33
          "schema": {
34
            "$ref": "../definitions.json#/error"
35
          }
36
        }
37
      },
38
      "x-koha-authorization": {
39
        "permissions": {
40
          "catalogue": "1"
41
        }
42
      }
43
    },
44
    "post": {
45
      "x-mojo-controller": "Koha::REST::V1::Items::Messages",
46
      "operationId": "addItemMessage",
47
      "tags": ["items", "item_messages"],
48
      "parameters": [
49
        {
50
          "$ref": "../parameters.json#/itemIdPathParam"
51
        },
52
        {
53
          "name": "body",
54
          "in": "body",
55
          "description": "A JSON object representing an item message",
56
          "required": true,
57
          "schema": {
58
            "$ref": "../definitions.json#/item_message"
59
          }
60
        }
61
      ],
62
      "produces": [
63
        "application/json"
64
      ],
65
      "responses": {
66
        "200": {
67
          "description": "Item message added",
68
          "schema": {
69
            "$ref": "../definitions.json#/item_message"
70
          }
71
        },
72
        "400": {
73
          "description": "Missing or wrong parameters",
74
          "schema": {
75
            "$ref": "../definitions.json#/error"
76
          }
77
        },
78
        "403": {
79
          "description": "Access forbidden",
80
          "schema": {
81
            "$ref": "../definitions.json#/error"
82
          }
83
        },
84
        "404": {
85
          "description": "Object not found",
86
          "schema": {
87
            "$ref": "../definitions.json#/error"
88
          }
89
        },
90
        "500": {
91
          "description": "Internal error",
92
          "schema": {
93
            "$ref": "../definitions.json#/error"
94
          }
95
        }
96
      },
97
      "x-koha-authorization": {
98
        "allow-owner": true,
99
        "permissions": {
100
          "editcatalogue": "1"
101
        }
102
      }
103
    }
104
  },
105
  "/items/{itemnumber}/messages/{item_message_id}": {
106
    "get": {
107
      "x-mojo-controller": "Koha::REST::V1::Items::Messages",
108
      "operationId": "getItemMessage",
109
      "tags": ["items","item_messages"],
110
      "parameters": [
111
        {
112
          "$ref": "../parameters.json#/itemIdPathParam"
113
        },
114
        {
115
          "$ref": "../parameters.json#/itemMessageIdPathParam"
116
        }
117
      ],
118
      "produces": [
119
        "application/json"
120
      ],
121
      "responses": {
122
        "200": {
123
          "description": "An item messge",
124
          "schema": {
125
            "$ref": "../definitions.json#/item_message"
126
          }
127
        },
128
        "403": {
129
          "description": "Access forbidden",
130
          "schema": {
131
            "$ref": "../definitions.json#/error"
132
          }
133
        },
134
        "404": {
135
          "description": "Vendor not found",
136
          "schema": {
137
            "$ref": "../definitions.json#/error"
138
          }
139
        },
140
        "500": {
141
          "description": "Internal error",
142
          "schema": {
143
            "$ref": "../definitions.json#/error"
144
          }
145
        }
146
      },
147
      "x-koha-authorization": {
148
        "permissions": {
149
          "catalogue": "1"
150
        }
151
      }
152
    },
153
    "put": {
154
      "x-mojo-controller": "Koha::REST::V1::Items::Messages",
155
      "operationId": "updateItemMessage",
156
      "tags": ["items","item_messages"],
157
      "parameters": [
158
        {
159
          "$ref": "../parameters.json#/itemIdPathParam"
160
        },
161
        {
162
          "$ref": "../parameters.json#/itemMessageIdPathParam"
163
        },
164
        {
165
          "name": "body",
166
          "in": "body",
167
          "description": "A JSON object representing an item message",
168
          "required": true,
169
          "schema": {
170
            "$ref": "../definitions.json#/item_message"
171
          }
172
        }
173
      ],
174
      "produces": [
175
        "application/json"
176
      ],
177
      "responses": {
178
        "200": {
179
          "description": "Updated item message",
180
          "schema": {
181
            "$ref": "../definitions.json#/item_message"
182
          }
183
        },
184
        "400": {
185
          "description": "Missing or wrong parameters",
186
          "schema": {
187
            "$ref": "../definitions.json#/error"
188
          }
189
        },
190
        "403": {
191
          "description": "Access forbidden",
192
          "schema": {
193
            "$ref": "../definitions.json#/error"
194
          }
195
        },
196
        "404": {
197
          "description": "Object not found",
198
          "schema": {
199
            "$ref": "../definitions.json#/error"
200
          }
201
        }
202
      },
203
      "x-koha-authorization": {
204
        "permissions": {
205
          "editcatalogue": "1"
206
        }
207
      }
208
    },
209
    "delete": {
210
      "x-mojo-controller": "Koha::REST::V1::Items::Messages",
211
      "operationId": "deleteItemMessage",
212
      "tags": ["items","item_messages"],
213
      "parameters": [
214
        {
215
          "$ref": "../parameters.json#/itemIdPathParam"
216
        },
217
        {
218
          "$ref": "../parameters.json#/itemMessageIdPathParam"
219
        }
220
      ],
221
      "produces": ["application/json"],
222
      "responses": {
223
        "200": {
224
          "description": "Item message deleted",
225
          "schema": {
226
            "type": "string"
227
          }
228
        },
229
        "403": {
230
          "description": "Access forbidden",
231
          "schema": {
232
            "$ref": "../definitions.json#/error"
233
          }
234
        },
235
        "404": {
236
          "description": "Object not found",
237
          "schema": {
238
            "$ref": "../definitions.json#/error"
239
          }
240
        },
241
        "500": {
242
          "description": "Internal error",
243
          "schema": {
244
            "$ref": "../definitions.json#/error"
245
          }
246
        }
247
      },
248
      "x-koha-authorization": {
249
        "permissions": {
250
          "editcatalogue": "1"
251
        }
252
      }
253
    }
254
  }
255
}
(-)a/api/v1/swagger/x-primitives.json (-3 / +8 lines)
Lines 16-21 Link Here
16
    "description": "internally assigned city identifier",
16
    "description": "internally assigned city identifier",
17
    "readOnly": true
17
    "readOnly": true
18
  },
18
  },
19
  "item_message_id": {
20
    "type": "integer",
21
    "description": "internally assigned item message identifier",
22
    "readOnly": true
23
  },
19
  "email": {
24
  "email": {
20
    "type": ["string", "null"],
25
    "type": ["string", "null"],
21
    "description": "primary email address for patron's primary address"
26
    "description": "primary email address for patron's primary address"
Lines 25-32 Link Here
25
    "description": "patron's first name"
30
    "description": "patron's first name"
26
  },
31
  },
27
  "itemnumber": {
32
  "itemnumber": {
28
    "type": ["integer", "null"],
33
    "type": "integer",
29
    "description": "internally assigned item identifier"
34
    "description": "internally assigned item identifier",
35
    "readOnly": true
30
  },
36
  },
31
  "phone": {
37
  "phone": {
32
    "type": ["string", "null"],
38
    "type": ["string", "null"],
33
- 

Return to bug 17361