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 138-143 sub _type { Link Here
138
    return 'Suggestion';
138
    return 'Suggestion';
139
}
139
}
140
140
141
=head3 to_api_mapping
142
143
This method returns the mapping for representing a Koha::Patron object
144
on the API.
145
146
=cut
147
148
sub to_api_mapping {
149
    return {
150
        suggestionid         => 'suggestion_id',
151
        suggestedby          => 'suggested_by',
152
        suggesteddate        => 'suggestion_date',
153
        managedby            => 'managed_by',
154
        manageddate          => 'managed_date',
155
        acceptedby           => 'accepted_by',
156
        accepteddate         => 'accepted_date',
157
        rejectedby           => 'rejected_by',
158
        rejecteddate         => 'rejected_date',
159
        lastmodificationdate => 'last_status_change_date',
160
        lastmodificationby   => 'last_status_change_by',
161
        STATUS               => 'status',
162
        note                 => 'note',
163
        author               => 'author',
164
        title                => 'title',
165
        copyrightdate        => 'copyright_date',
166
        publishercode        => 'publisher_code',
167
        date                 => 'timestamp',
168
        volumedesc           => 'volume_desc',
169
        publicationyear      => 'publication_year',
170
        place                => 'publication_place',
171
        isbn                 => 'isbn',
172
        biblionumber         => 'biblio_id',
173
        reason               => 'reason',
174
        patronreason         => 'patron_reason',
175
        budgetid             => 'budget_id',
176
        branchcode           => 'library_id',
177
        collectiontitle      => 'collection_title',
178
        itemtype             => 'item_type',
179
        quantity             => 'quantity',
180
        currency             => 'currency',
181
        price                => 'item_price',
182
        total                => 'total_price',
183
        archived             => 'archived',
184
    };
185
}
186
141
=head1 AUTHOR
187
=head1 AUTHOR
142
188
143
Kyle M Hall <kyle@bywatersolutions.com>
189
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