Bug 31793: Add REST endpoint to delete authorities
[srvgit] / Koha / REST / V1 / Authorities.pm
1 package Koha::REST::V1::Authorities;
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::Authorities;
23 use C4::AuthoritiesMarc qw( DelAuthority );
24
25 use List::MoreUtils qw( any );
26 use MARC::Record::MiJ;
27
28 use Try::Tiny qw( catch try );
29
30 =head1 API
31
32 =head2 Methods
33
34 =head3 get
35
36 Controller function that handles retrieving a single authority object
37
38 =cut
39
40 sub get {
41     my $c = shift->openapi->valid_input or return;
42
43     my $authority = Koha::Authorities->find( { authid => $c->validation->param('authority_id') } );
44     unless ( $authority ) {
45         return $c->render(
46             status  => 404,
47             openapi => {
48                 error => "Object not found."
49             }
50         );
51     }
52
53     return try {
54
55         if ( $c->req->headers->accept =~ m/application\/json/ ) {
56             return $c->render(
57                 status => 200,
58                 json   => $authority->to_api
59             );
60         }
61         else {
62             my $record = $authority->record;
63
64             $c->respond_to(
65                 marcxml => {
66                     status => 200,
67                     format => 'marcxml',
68                     text   => $record->as_xml_record
69                 },
70                 mij => {
71                     status => 200,
72                     format => 'mij',
73                     data   => $record->to_mij
74                 },
75                 marc => {
76                     status => 200,
77                     format => 'marc',
78                     text   => $record->as_usmarc
79                 },
80                 txt => {
81                     status => 200,
82                     format => 'text/plain',
83                     text   => $record->as_formatted
84                 },
85                 any => {
86                     status  => 406,
87                     openapi => [
88                         "application/json",
89                         "application/marcxml+xml",
90                         "application/marc-in-json",
91                         "application/marc",
92                         "text/plain"
93                     ]
94                 }
95             );
96         }
97     }
98     catch {
99         $c->unhandled_exception($_);
100     };
101 }
102
103 =head3 delete
104
105 Controller function that handles deleting an authority object
106
107 =cut
108
109 sub delete {
110     my $c = shift->openapi->valid_input or return;
111
112     my $authority = Koha::Authorities->find( { authid => $c->validation->param('authority_id') } );
113
114     if ( not defined $authority ) {
115         return $c->render(
116             status  => 404,
117             openapi => { error => "Object not found" }
118         );
119     }
120
121     return try {
122         my $error = DelAuthority( { authid => $authority->authid } );
123
124         if ($error) {
125             return $c->render(
126                 status  => 409,
127                 openapi => { error => $error }
128             );
129         }
130         else {
131             return $c->render( status => 204, openapi => "" );
132         }
133     }
134     catch {
135         $c->unhandled_exception($_);
136     };
137 }
138
139 1;