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

(-)a/Koha/OAI/Set.pm (+66 lines)
Lines 33-38 Koha::OAI::Set - Koha OaiSet Object class Link Here
33
33
34
=cut
34
=cut
35
35
36
=head3 biblionumbers
37
38
    my @biblionumbers = $set->biblionumbers->as_list;
39
40
Returns the biblionumbers contained in this set.
41
42
=cut
43
44
sub biblionumbers {
45
    my ( $self, $params ) = @_;
46
47
    return $self->_result->oai_sets_biblios->search( {}, { columns => ['biblionumber'] } );
48
}
49
50
=head3 biblios
51
52
    my @biblios = $set->biblios->as_list;
53
54
Returns the Koha::Biblio objects that exist in the catalog for records in this set.
55
56
=cut
57
58
sub biblios {
59
    my ( $self, $params ) = @_;
60
61
    return Koha::Biblios->search( { biblionumber => scalar $self->biblionumbers } );
62
}
63
64
=head3 to_api
65
66
    my $json = $set->to_api;
67
68
Overloaded method that returns a JSON representation of the Koha::OAI::Set object,
69
suitable for API output.
70
71
=cut
72
73
sub to_api {
74
    my ( $self, $params ) = @_;
75
76
    my $json_set = $self->SUPER::to_api($params);
77
78
    return unless $json_set;
79
80
    $json_set->{total_count} = $self->biblionumbers->count;
81
82
    $json_set->{catalog_count} = $self->biblios->count;
83
84
    return $json_set;
85
}
86
87
=head3 to_api_mapping
88
89
This method returns the mapping for representing a Koha::OAI::Set object
90
on the API.
91
92
=cut
93
94
sub to_api_mapping {
95
    return {
96
        id   => 'set_id',
97
        spec => 'spec',
98
        name => 'name',
99
    };
100
}
101
36
=head2 Internal methods
102
=head2 Internal methods
37
103
38
=head3 _type
104
=head3 _type
(-)a/Koha/REST/V1/OAI/Sets.pm (+58 lines)
Line 0 Link Here
1
package Koha::REST::V1::OAI::Sets;
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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Mojo::Base 'Mojolicious::Controller';
21
22
use Try::Tiny qw( catch try );
23
24
use Koha::OAI::Sets;
25
26
=head1 NAME
27
28
Koha::REST::V1::OAI::Sets;
29
30
=head1 API
31
32
=head2 Methods
33
34
=head3 get
35
36
Controller function that handles retrieving an OAI set
37
38
=cut
39
40
sub get {
41
    my $c = shift->openapi->valid_input or return;
42
43
    return try {
44
        my $set = Koha::OAI::Sets->find( $c->param('set_id') );
45
46
        return $c->render_resource_not_found("OAI set")
47
            unless $set;
48
49
        return $c->render(
50
            status  => 200,
51
            openapi => $c->objects->to_api($set),
52
        );
53
    } catch {
54
        $c->unhandled_exception($_);
55
    }
56
}
57
58
1;
(-)a/api/v1/swagger/definitions/oai_set.yaml (+21 lines)
Line 0 Link Here
1
---
2
type: object
3
properties:
4
  set_id:
5
    type: integer
6
    description: Internal OAI set identifier
7
  spec:
8
    type: string
9
    description: Indicates a hierarchy of sets
10
  name:
11
    type: string
12
    description: Name of set
13
  total_count:
14
    type: integer
15
    description: Number of total items in the set
16
  catalog_count:
17
    type: integer
18
    description: Number of items which exist both in the set and in the catalog
19
additionalProperties: false
20
required:
21
  - set_id
(-)a/api/v1/swagger/paths/oai_sets.yaml (+47 lines)
Line 0 Link Here
1
---
2
"/oai_sets/{set_id}":
3
  get:
4
    x-mojo-to: OAI::Sets#get
5
    operationId: getOAISet
6
    tags:
7
      - oai_sets
8
    summary: Get OAI set
9
    parameters:
10
      - name: set_id
11
        in: path
12
        description: Internal OAI set identifier
13
        required: true
14
        type: integer
15
    produces:
16
      - application/json
17
    responses:
18
      "200":
19
        description: An OAI set
20
        schema:
21
          $ref: "../swagger.yaml#/definitions/oai_set"
22
      "400":
23
        description: Bad request
24
        schema:
25
          $ref: "../swagger.yaml#/definitions/error"
26
      "403":
27
        description: Access forbidden
28
        schema:
29
          $ref: "../swagger.yaml#/definitions/error"
30
      "404":
31
        description: Patron not found
32
        schema:
33
          $ref: "../swagger.yaml#/definitions/error"
34
      "500":
35
        description: |
36
          Internal server error. Possible `error_code` attribute values:
37
38
          * `internal_server_error`
39
        schema:
40
          $ref: "../swagger.yaml#/definitions/error"
41
      "503":
42
        description: Under maintenance
43
        schema:
44
          $ref: "../swagger.yaml#/definitions/error"
45
    x-koha-authorization:
46
      permissions:
47
        parameters: manage_oai_sets
(-)a/api/v1/swagger/swagger.yaml (+4 lines)
Lines 142-147 definitions: Link Here
142
    $ref: ./definitions/list.yaml
142
    $ref: ./definitions/list.yaml
143
  merge_biblios:
143
  merge_biblios:
144
    $ref: ./definitions/merge_biblios.yaml
144
    $ref: ./definitions/merge_biblios.yaml
145
  oai_set:
146
    $ref: ./definitions/oai_set.yaml
145
  order:
147
  order:
146
    $ref: ./definitions/order.yaml
148
    $ref: ./definitions/order.yaml
147
  order_claim:
149
  order_claim:
Lines 483-488 paths: Link Here
483
    $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1cash_registers"
485
    $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1cash_registers"
484
  "/libraries/{library_id}/desks":
486
  "/libraries/{library_id}/desks":
485
    $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1desks"
487
    $ref: "./paths/libraries.yaml#/~1libraries~1{library_id}~1desks"
488
  "/oai_sets/{set_id}":
489
    $ref: "./paths/oai_sets.yaml#/~1oai_sets~1{set_id}"
486
  "/oauth/login/{provider_code}/{interface}":
490
  "/oauth/login/{provider_code}/{interface}":
487
    $ref: ./paths/oauth.yaml#/~1oauth~1login~1{provider_code}~1{interface}
491
    $ref: ./paths/oauth.yaml#/~1oauth~1login~1{provider_code}~1{interface}
488
  /oauth/token:
492
  /oauth/token:
(-)a/t/db_dependent/OAI/Sets.t (-1 / +42 lines)
Lines 19-30 Link Here
19
use Modern::Perl;
19
use Modern::Perl;
20
20
21
use Test::NoWarnings;
21
use Test::NoWarnings;
22
use Test::More tests => 154;
22
use Test::More tests => 155;
23
use Test::MockModule;
23
use Test::MockModule;
24
use Test::Warn;
24
use Test::Warn;
25
use MARC::Record;
25
use MARC::Record;
26
26
27
use Koha::Database;
27
use Koha::Database;
28
use Koha::OAI::Sets;
28
use C4::OAI::Sets
29
use C4::OAI::Sets
29
    qw( AddOAISet GetOAISets GetOAISet GetOAISetBySpec ModOAISet ModOAISetMappings GetOAISetsMappings GetOAISetMappings AddOAISetsBiblios GetOAISetsBiblio ModOAISetsBiblios DelOAISet DelOAISetsBiblio UpdateOAISetsBiblio CalcOAISetsBiblio );
30
    qw( AddOAISet GetOAISets GetOAISet GetOAISetBySpec ModOAISet ModOAISetMappings GetOAISetsMappings GetOAISetMappings AddOAISetsBiblios GetOAISetsBiblio ModOAISetsBiblios DelOAISet DelOAISetsBiblio UpdateOAISetsBiblio CalcOAISetsBiblio );
30
31
Lines 735-738 ModOAISetMappings( $setRep_id, $mappingRepC ); Link Here
735
my @setsRepC = CalcOAISetsBiblio($record_rep);
736
my @setsRepC = CalcOAISetsBiblio($record_rep);
736
is_deeply( \@setsRepC, [$setRep_id], 'The $record belongs to $setRep_id (matching on second field, second subfield)' );
737
is_deeply( \@setsRepC, [$setRep_id], 'The $record belongs to $setRep_id (matching on second field, second subfield)' );
737
738
739
subtest 'Koha::OAI::Sets tests' => sub {
740
741
    plan tests => 2;
742
743
    my $set = $builder->build_object(
744
        {
745
            class => 'Koha::OAI::Sets',
746
            value => { spec => 'A' }
747
        }
748
    );
749
    my $set_id = $set->id;
750
751
    my $set_item_deleted_1 = $builder->build_object(
752
        {
753
            class => 'Koha::OAI::Set::Biblios',
754
            value => { set_id => $set_id, biblionumber => 123456789 }
755
        }
756
    );
757
758
    my $set_item_deleted_2 = $builder->build_object(
759
        {
760
            class => 'Koha::OAI::Set::Biblios',
761
            value => { set_id => $set_id, biblionumber => 987654321 }
762
        }
763
    );
764
765
    my $biblio = $builder->build_sample_biblio();
766
767
    my $set_item_in_catalog = $builder->build_object(
768
        {
769
            class => 'Koha::OAI::Set::Biblios',
770
            value => { set_id => $set_id, biblionumber => $biblio->id }
771
        }
772
    );
773
774
    my $fetched_set = Koha::OAI::Sets->find($set_id);
775
    is( $fetched_set->biblionumbers->count, 3, "Set contains three items" );
776
    is( $fetched_set->biblios->count,       1, "Set contains one item that currently exists in the catalog" );
777
};
778
738
$schema->storage->txn_rollback;
779
$schema->storage->txn_rollback;
(-)a/t/db_dependent/api/v1/oai_sets.t (-1 / +103 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/env perl
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 <https://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 2;
21
use Test::NoWarnings;
22
use Test::Mojo;
23
24
use t::lib::TestBuilder;
25
use t::lib::Mocks;
26
27
use Koha::OAI::Sets;
28
use Koha::OAI::Set::Biblios;
29
use Koha::Database;
30
31
my $schema  = Koha::Database->new->schema;
32
my $builder = t::lib::TestBuilder->new;
33
34
my $t = Test::Mojo->new('Koha::REST::V1');
35
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
36
37
subtest 'get() tests' => sub {
38
39
    plan tests => 5;
40
41
    $schema->storage->txn_begin;
42
43
    Koha::OAI::Set::Biblios->search->delete;
44
    Koha::OAI::Sets->search->delete;
45
46
    my $librarian = $builder->build_object(
47
        {
48
            class => 'Koha::Patrons',
49
            value => { flags => 3**2 }    # parameters flag = 3
50
        }
51
    );
52
    my $password = 'thePassword123';
53
    $librarian->set_password( { password => $password, skip_validation => 1 } );
54
    my $userid = $librarian->userid;
55
56
    my $patron = $builder->build_object(
57
        {
58
            class => 'Koha::Patrons',
59
            value => { flags => 0 }
60
        }
61
    );
62
63
    $patron->set_password( { password => $password, skip_validation => 1 } );
64
    my $unauth_userid = $patron->userid;
65
66
    my $set = $builder->build_object(
67
        {
68
            class => 'Koha::OAI::Sets',
69
            value => { spec => 'A' }
70
        }
71
    );
72
    my $set_id = $set->id;
73
74
    my $set_item_deleted_1 = $builder->build_object(
75
        {
76
            class => 'Koha::OAI::Set::Biblios',
77
            value => { set_id => $set_id, biblionumber => 123456789 }
78
        }
79
    );
80
81
    my $set_item_deleted_2 = $builder->build_object(
82
        {
83
            class => 'Koha::OAI::Set::Biblios',
84
            value => { set_id => $set_id, biblionumber => 987654321 }
85
        }
86
    );
87
88
    my $biblio = $builder->build_sample_biblio();
89
90
    my $set_item_in_catalog = $builder->build_object(
91
        {
92
            class => 'Koha::OAI::Set::Biblios',
93
            value => { set_id => $set_id, biblionumber => $biblio->id }
94
        }
95
    );
96
97
    $t->get_ok("//$userid:$password@/api/v1/oai_sets/$set_id")->status_is(200)->json_is( $set->to_api );
98
99
    # Unauthorized access
100
    $t->get_ok("//$unauth_userid:$password@/api/v1/oai_sets/$set_id")->status_is(403);
101
102
    $schema->storage->txn_rollback;
103
};

Return to bug 41463