Bug 31028: Add API's for tickets
[koha-ffzg.git] / Koha / REST / V1 / Tickets.pm
1 package Koha::REST::V1::Tickets;
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use Mojo::Base 'Mojolicious::Controller';
21
22 use Koha::Ticket;
23 use Koha::Tickets;
24 use Koha::Ticket::Update;
25 use Koha::Ticket::Updates;
26
27 use Try::Tiny qw( catch try );
28
29 =head1 API
30
31 =head2 Methods
32
33 =head3 list
34
35 =cut
36
37 sub list {
38     my $c = shift->openapi->valid_input or return;
39
40     return try {
41         my $tickets_set = Koha::Tickets->new;
42         my $tickets     = $c->objects->search($tickets_set);
43         return $c->render( status => 200, openapi => $tickets );
44     }
45     catch {
46         $c->unhandled_exception($_);
47     };
48
49 }
50
51 =head3 get
52
53 =cut
54
55 sub get {
56     my $c = shift->openapi->valid_input or return;
57
58     return try {
59         my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
60         unless ($ticket) {
61             return $c->render(
62                 status  => 404,
63                 openapi => { error => "Ticket not found" }
64             );
65         }
66
67         return $c->render( status => 200, openapi => $ticket->to_api );
68     }
69     catch {
70         $c->unhandled_exception($_);
71     }
72 }
73
74 =head3 add
75
76 =cut
77
78 sub add {
79     my $c = shift->openapi->valid_input or return;
80     my $patron = $c->stash('koha.user');
81
82     return try {
83         my $body   = $c->validation->param('body');
84
85         # Set reporter from session
86         $body->{reporter_id} = $patron->id;
87         # FIXME: We should allow impersonation at a later date to
88         # allow an API user to submit on behalf of a user
89
90         my $ticket = Koha::Ticket->new_from_api($body)->store;
91         $ticket->discard_changes;
92         $c->res->headers->location(
93             $c->req->url->to_string . '/' . $ticket->id );
94         return $c->render(
95             status  => 201,
96             openapi => $ticket->to_api
97         );
98     }
99     catch {
100         $c->unhandled_exception($_);
101     };
102 }
103
104 =head3 update
105
106 =cut
107
108 sub update {
109     my $c = shift->openapi->valid_input or return;
110
111     my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
112
113     if ( not defined $ticket ) {
114         return $c->render(
115             status  => 404,
116             openapi => { error => "Object not found" }
117         );
118     }
119
120     return try {
121         $ticket->set_from_api( $c->validation->param('body') );
122         $ticket->store();
123         return $c->render( status => 200, openapi => $ticket->to_api );
124     }
125     catch {
126         $c->unhandled_exception($_);
127     };
128 }
129
130 =head3 delete
131
132 =cut
133
134 sub delete {
135     my $c = shift->openapi->valid_input or return;
136
137     my $ticket = Koha::Tickets->find( $c->validation->param('ticket_id') );
138     if ( not defined $ticket ) {
139         return $c->render(
140             status  => 404,
141             openapi => { error => "Object not found" }
142         );
143     }
144
145     return try {
146         $ticket->delete;
147         return $c->render(
148             status  => 204,
149             openapi => q{}
150         );
151     }
152     catch {
153         $c->unhandled_exception($_);
154     };
155 }
156
157 1;