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

(-)a/Koha/REST/V1/Suggestions.pm (+180 lines)
Line 0 Link Here
1
package Koha::REST::V1::Suggestions;
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
15
# along with Koha; if not, see <http:┬░www.gnu.org/licenses>.
16
17
use Modern::Perl;
18
19
use Mojo::Base 'Mojolicious::Controller';
20
21
use Koha::Suggestions;
22
23
use Try::Tiny;
24
25
=head1 NAME
26
27
Koha::REST::V1::Suggestion
28
29
=head1 API
30
31
=head2 Methods
32
33
=head3 list
34
35
Controller method that handles listing Koha::Suggestion objects
36
37
=cut
38
39
sub list {
40
    my $c = shift->openapi->valid_input or return;
41
42
    return try {
43
44
        my $suggestions = $c->objects->search( Koha::Suggestions->new );
45
46
        return $c->render(
47
            status  => 200,
48
            openapi => $suggestions
49
        );
50
    }
51
    catch {
52
        $c->unhandled_exception($_);
53
    };
54
}
55
56
=head3 get
57
58
Controller method that handles retrieving a single Koha::Suggestion object
59
60
=cut
61
62
sub get {
63
    my $c = shift->openapi->valid_input or return;
64
65
    return try {
66
        my $suggestion_id = $c->validation->param('suggestion_id');
67
        my $suggestion = $c->objects->find( Koha::Suggestions->new, $suggestion_id );
68
69
        unless ($suggestion) {
70
            return $c->render(
71
                status  => 404,
72
                openapi => { error => "Suggestion not found." }
73
            );
74
        }
75
76
        return $c->render(
77
            status  => 200,
78
            openapi => $suggestion
79
        );
80
    }
81
    catch {
82
        $c->unhandled_exception($_);
83
    };
84
}
85
86
=head3 add
87
88
Controller method that handles adding a new Koha::Suggestion object
89
90
=cut
91
92
sub add {
93
    my $c = shift->openapi->valid_input or return;
94
95
    my $body = $c->validation->param('body');
96
97
    # FIXME: This should be handled in Koha::Suggestion->store
98
    $body->{'status'} = 'ASKED'
99
        unless defined $body->{'status'};
100
101
    return try {
102
        my $suggestion = Koha::Suggestion->new_from_api( $body )->store;
103
        $suggestion->discard_changes;
104
        $c->res->headers->location( $c->req->url->to_string . '/' . $suggestion->suggestionid );
105
106
        return $c->render(
107
            status  => 201,
108
            openapi => $suggestion->to_api
109
        );
110
    }
111
    catch {
112
        $c->unhandled_exception($_);
113
    };
114
}
115
116
=head3 update
117
118
Controller method that handles modifying Koha::Suggestion object
119
120
=cut
121
122
sub update {
123
    my $c = shift->openapi->valid_input or return;
124
125
    my $suggestion_id = $c->validation->param('suggestion_id');
126
    my $suggestion = Koha::Suggestions->find( $suggestion_id );
127
128
    return $c->render(
129
        status  => 404,
130
        openapi => { error => 'Suggestion not found.' }
131
    ) unless $suggestion;
132
133
    return try {
134
135
        my $body = $c->validation->param('body');
136
137
        $suggestion->set_from_api( $body )->store;
138
        $suggestion->discard_changes;
139
140
        return $c->render(
141
            status  => 200,
142
            openapi => $suggestion->to_api
143
        );
144
    }
145
    catch {
146
        $c->unhandled_exception($_);
147
    };
148
149
}
150
151
=head3 delete
152
153
Controller method that handles removing a Koha::Suggestion object
154
155
=cut
156
157
sub delete {
158
    my $c = shift->openapi->valid_input or return;
159
160
    my $suggestion_id = $c->validation->param('suggestion_id');
161
    my $suggestion = Koha::Suggestions->find( $suggestion_id );
162
163
    return $c->render(
164
        status  => 404,
165
        openapi => { error => 'Suggestion not found.' }
166
    ) unless $suggestion;
167
168
    return try {
169
        $suggestion->delete;
170
        return $c->render(
171
            status  => 204,
172
            openapi => q{}
173
        );
174
    }
175
    catch {
176
        $c->unhandled_exception($_);
177
    };
178
}
179
180
1;
(-)a/Koha/Suggestion.pm (+46 lines)
Lines 139-144 sub _type { Link Here
139
    return 'Suggestion';
139
    return 'Suggestion';
140
}
140
}
141
141
142
=head3 to_api_mapping
143
144
This method returns the mapping for representing a Koha::Patron object
145
on the API.
146
147
=cut
148
149
sub to_api_mapping {
150
    return {
151
        suggestionid         => 'suggestion_id',
152
        suggestedby          => 'suggested_by',
153
        suggesteddate        => 'suggestion_date',
154
        managedby            => 'managed_by',
155
        manageddate          => 'managed_date',
156
        acceptedby           => 'accepted_by',
157
        accepteddate         => 'accepted_date',
158
        rejectedby           => 'rejected_by',
159
        rejecteddate         => 'rejected_date',
160
        lastmodificationdate => 'last_status_change_date',
161
        lastmodificationby   => 'last_status_change_by',
162
        STATUS               => 'status',
163
        note                 => 'note',
164
        author               => 'author',
165
        title                => 'title',
166
        copyrightdate        => 'copyright_date',
167
        publishercode        => 'publisher_code',
168
        date                 => 'timestamp',
169
        volumedesc           => 'volume_desc',
170
        publicationyear      => 'publication_year',
171
        place                => 'publication_place',
172
        isbn                 => 'isbn',
173
        biblionumber         => 'biblio_id',
174
        reason               => 'reason',
175
        patronreason         => 'patron_reason',
176
        budgetid             => 'budget_id',
177
        branchcode           => 'library_id',
178
        collectiontitle      => 'collection_title',
179
        itemtype             => 'item_type',
180
        quantity             => 'quantity',
181
        currency             => 'currency',
182
        price                => 'item_price',
183
        total                => 'total_price',
184
        archived             => 'archived',
185
    };
186
}
187
142
=head1 AUTHOR
188
=head1 AUTHOR
143
189
144
Kyle M Hall <kyle@bywatersolutions.com>
190
Kyle M Hall <kyle@bywatersolutions.com>
(-)a/t/db_dependent/api/v1/suggestions.t (-3 lines)
Lines 26-33 use t::lib::Mocks; Link Here
26
use Koha::Suggestions;
26
use Koha::Suggestions;
27
use Koha::Database;
27
use Koha::Database;
28
28
29
use Data::Printer colored => 1;
30
31
my $schema  = Koha::Database->new->schema;
29
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
30
my $builder = t::lib::TestBuilder->new;
33
31
34
- 

Return to bug 17314