|
Line 0
Link Here
|
|
|
1 |
package Koha::REST::V1::Biblio; |
| 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 C4::Biblio qw( GetBiblioData AddBiblio ModBiblio DelBiblio ); |
| 23 |
use C4::Items qw ( AddItemBatchFromMarc ); |
| 24 |
use Koha::Biblios; |
| 25 |
use MARC::Record; |
| 26 |
use MARC::Batch; |
| 27 |
use MARC::File::USMARC; |
| 28 |
use MARC::File::XML; |
| 29 |
|
| 30 |
use Data::Dumper; |
| 31 |
|
| 32 |
sub get { |
| 33 |
my ($c, $args, $cb) = @_; |
| 34 |
|
| 35 |
my $biblio = &GetBiblioData($args->{biblionumber}); |
| 36 |
unless ($biblio) { |
| 37 |
return $c->$cb({error => "Biblio not found"}, 404); |
| 38 |
} |
| 39 |
return $c->$cb($biblio, 200); |
| 40 |
} |
| 41 |
|
| 42 |
sub getitems { |
| 43 |
my ($c, $args, $cb) = @_; |
| 44 |
|
| 45 |
my $biblio = Koha::Biblios->find($args->{biblionumber}); |
| 46 |
unless ($biblio) { |
| 47 |
return $c->$cb({error => "Biblio not found"}, 404); |
| 48 |
} |
| 49 |
return $c->$cb({ biblio => $biblio->unblessed, items => $biblio->items->unblessed }, 200); |
| 50 |
} |
| 51 |
|
| 52 |
sub getexpanded { |
| 53 |
my ($c, $args, $cb) = @_; |
| 54 |
|
| 55 |
my $biblio = Koha::Biblios->find($args->{biblionumber}); |
| 56 |
unless ($biblio) { |
| 57 |
return $c->$cb({error => "Biblio not found"}, 404); |
| 58 |
} |
| 59 |
my $expanded = $biblio->items->unblessed; |
| 60 |
for my $item (@{$expanded}) { |
| 61 |
|
| 62 |
# we assume item is available by default |
| 63 |
$item->{status} = "available"; |
| 64 |
|
| 65 |
if ($item->{onloan}) { |
| 66 |
$item->{status} = "onloan" |
| 67 |
} |
| 68 |
|
| 69 |
if ($item->{restricted}) { |
| 70 |
$item->{status} = "restricted"; |
| 71 |
} |
| 72 |
|
| 73 |
# mark as unavailable if notforloan, damaged, lost, or withdrawn |
| 74 |
if ($item->{damaged} || $item->{itemlost} || $item->{withdrawn} || $item->{notforloan}) { |
| 75 |
$item->{status} = "unavailable"; |
| 76 |
} |
| 77 |
|
| 78 |
my $holds = Koha::Holds->search({itemnumber => $item->{itemnumber}})->unblessed; |
| 79 |
|
| 80 |
# mark as onhold if item marked as hold |
| 81 |
if (scalar(@{$holds}) > 0) { |
| 82 |
$item->{status} = "onhold"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
return $c->$cb({ biblio => $biblio->unblessed, items => $expanded }, 200); |
| 87 |
} |
| 88 |
|
| 89 |
sub add { |
| 90 |
my ($c, $args, $cb) = @_; |
| 91 |
|
| 92 |
my $biblionumber; |
| 93 |
my $biblioitemnumber; |
| 94 |
|
| 95 |
my $body = $c->req->body; |
| 96 |
unless ($body) { |
| 97 |
return $c->$cb({error => "Missing MARCXML body"}, 400); |
| 98 |
} |
| 99 |
|
| 100 |
my $record = eval {MARC::Record::new_from_xml( $body, "utf8", '')}; |
| 101 |
if ($@) { |
| 102 |
return $c->$cb({error => $@}, 400); |
| 103 |
} else { |
| 104 |
( $biblionumber, $biblioitemnumber ) = &AddBiblio($record, ''); |
| 105 |
} |
| 106 |
if ($biblionumber) { |
| 107 |
$c->res->headers->location($c->url_for('/api/v1/biblios/')->to_abs . $biblionumber); |
| 108 |
my ( $itemnumbers, $errors ) = &AddItemBatchFromMarc( $record, $biblionumber, $biblioitemnumber, '' ); |
| 109 |
unless (@{$errors}) { |
| 110 |
return $c->$cb({biblionumber => $biblionumber, items => join(",", @{$itemnumbers})}, 201); |
| 111 |
} else { |
| 112 |
warn Dumper($errors); |
| 113 |
return $c->$cb({error => "Error creating items, see Koha Logs for details.", biblionumber => $biblionumber, items => join(",", @{$itemnumbers})}, 400); |
| 114 |
} |
| 115 |
} else { |
| 116 |
return $c->$cb({error => "unable to create record"}, 400); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
# NB: This will not update any items, Items should be a separate API route |
| 121 |
sub update { |
| 122 |
my ($c, $args, $cb) = @_; |
| 123 |
|
| 124 |
my $biblionumber = $args->{biblionumber}; |
| 125 |
|
| 126 |
my $biblio = Koha::Biblios->find($biblionumber); |
| 127 |
unless ($biblio) { |
| 128 |
return $c->$cb({error => "Biblio not found"}, 404); |
| 129 |
} |
| 130 |
|
| 131 |
my $success; |
| 132 |
my $body = $c->req->body; |
| 133 |
my $record = eval {MARC::Record::new_from_xml( $body, "utf8", '')}; |
| 134 |
if ($@) { |
| 135 |
return $c->$cb({error => $@}, 400); |
| 136 |
} else { |
| 137 |
$success = &ModBiblio($record, $biblionumber, ''); |
| 138 |
} |
| 139 |
if ($success) { |
| 140 |
$c->res->headers->location($c->url_for('/api/v1/biblios/')->to_abs . $biblionumber); |
| 141 |
return $c->$cb({biblio => Koha::Biblios->find($biblionumber)->unblessed}, 200); |
| 142 |
} else { |
| 143 |
return $c->$cb({error => "unable to update record"}, 400); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
sub delete { |
| 148 |
my ($c, $args, $cb) = @_; |
| 149 |
|
| 150 |
my $biblio = Koha::Biblios->find($args->{biblionumber}); |
| 151 |
unless ($biblio) { |
| 152 |
return $c->$cb({error => "Biblio not found"}, 404); |
| 153 |
} |
| 154 |
my @items = $biblio->items; |
| 155 |
# Delete items first |
| 156 |
my @item_errors = (); |
| 157 |
foreach my $item (@items) { |
| 158 |
my $res = $item->delete; |
| 159 |
unless ($res eq 1) { |
| 160 |
push @item_errors, $item->unblessed->{itemnumber}; |
| 161 |
} |
| 162 |
} |
| 163 |
my $res = $biblio->delete; |
| 164 |
if ($res eq '1') { |
| 165 |
return $c->$cb({}, 200); |
| 166 |
} elsif ($res eq '-1') { |
| 167 |
return $c->$cb({error => "Not found. Error code: " . $res, items => @item_errors}, 404); |
| 168 |
} else { |
| 169 |
return $c->$cb({error => "Error code: " . $res, items => @item_errors}, 400); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
1; |