From 416dc898f5e1ba7ec3aa472089e70774ee4f2f93 Mon Sep 17 00:00:00 2001 From: Andreas Jonsson Date: Fri, 27 Oct 2023 11:58:47 +0200 Subject: [PATCH] Bug 34008: Rename item_type in api. --- Koha/ItemType.pm | 85 ++++++++++++------- .../{itemtype.yaml => item_type.yaml} | 8 +- .../itemtype_translated_description.yaml | 13 --- .../paths/{itemtypes.yaml => item_types.yaml} | 8 +- api/v1/swagger/swagger.yaml | 10 +-- .../api/v1/{itemtypes.t => item_types.t} | 22 ++--- 6 files changed, 77 insertions(+), 69 deletions(-) rename api/v1/swagger/definitions/{itemtype.yaml => item_type.yaml} (94%) delete mode 100644 api/v1/swagger/definitions/itemtype_translated_description.yaml rename api/v1/swagger/paths/{itemtypes.yaml => item_types.yaml} (92%) rename t/db_dependent/api/v1/{itemtypes.t => item_types.t} (84%) diff --git a/Koha/ItemType.pm b/Koha/ItemType.pm index dbbae3fbfc..92758a5df1 100644 --- a/Koha/ItemType.pm +++ b/Koha/ItemType.pm @@ -17,7 +17,6 @@ package Koha::ItemType; use Modern::Perl; - use C4::Koha qw( getitemtypeimagelocation ); use C4::Languages; use Koha::Caches; @@ -52,8 +51,7 @@ sub store { if ( !$self->in_storage ) { $flush = 1; - } - else { + } else { my $self_from_storage = $self->get_from_storage; $flush = 1 if ( $self_from_storage->description ne $self->description ); } @@ -96,22 +94,25 @@ sub image_location { sub translated_description { my ( $self, $lang ) = @_; if ( my $translated_description = eval { $self->get_column('translated_description') } ) { + # If the value has already been fetched (eg. from sarch_with_localization), # do not search for it again # Note: This is a bit hacky but should be fast return $translated_description - ? $translated_description - : $self->description; + ? $translated_description + : $self->description; } $lang ||= C4::Languages::getlanguage; - my $translated_description = Koha::Localizations->search({ - code => $self->itemtype, - entity => 'itemtypes', - lang => $lang - })->next; + my $translated_description = Koha::Localizations->search( + { + code => $self->itemtype, + entity => 'itemtypes', + lang => $lang + } + )->next; return $translated_description - ? $translated_description->translation - : $self->description; + ? $translated_description->translation + : $self->description; } =head3 translated_descriptions @@ -119,18 +120,21 @@ sub translated_description { =cut sub translated_descriptions { - my ( $self ) = @_; + my ($self) = @_; my @translated_descriptions = Koha::Localizations->search( - { entity => 'itemtypes', + { + entity => 'itemtypes', code => $self->itemtype, } )->as_list; - return [ map { - { - lang => $_->lang, - translation => $_->translation, - } - } @translated_descriptions ]; + return [ + map { + { + lang => $_->lang, + translation => $_->translation, + } + } @translated_descriptions + ]; } =head3 can_be_deleted @@ -142,8 +146,8 @@ Counts up the number of biblioitems and items with itemtype (code) and hands bac =cut sub can_be_deleted { - my ($self) = @_; - my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count; + my ($self) = @_; + my $nb_items = Koha::Items->search( { itype => $self->itemtype } )->count; my $nb_biblioitems = Koha::Biblioitems->search( { itemtype => $self->itemtype } )->count; return $nb_items + $nb_biblioitems == 0 ? 1 : 0; } @@ -162,10 +166,12 @@ sub may_article_request { my $itemtype = $self->itemtype; my $category = $params->{categorycode}; - my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes({ - $category ? ( categorycode => $category ) : (), - }); - return ( $guess->{ $itemtype // q{} } || $guess->{ '*' } ) ? 1 : q{}; + my $guess = Koha::CirculationRules->guess_article_requestable_itemtypes( + { + $category ? ( categorycode => $category ) : (), + } + ); + return ( $guess->{ $itemtype // q{} } || $guess->{'*'} ) ? 1 : q{}; } =head3 _library_limits @@ -176,8 +182,8 @@ sub may_article_request { sub _library_limits { return { - class => "ItemtypesBranch", - id => "itemtype", + class => "ItemtypesBranch", + id => "itemtype", library => "branchcode", }; } @@ -189,10 +195,10 @@ sub _library_limits { =cut sub parent { - my ( $self ) = @_; + my ($self) = @_; my $parent_rs = $self->_result->parent_type; return unless $parent_rs; - return Koha::ItemType->_new_from_dbic( $parent_rs ); + return Koha::ItemType->_new_from_dbic($parent_rs); } @@ -203,8 +209,23 @@ sub parent { =cut sub children_with_localization { - my ( $self ) = @_; - return Koha::ItemTypes->search_with_localization({ parent_type => $self->itemtype }); + my ($self) = @_; + return Koha::ItemTypes->search_with_localization( { parent_type => $self->itemtype } ); +} + +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::ItemType object +on the API. + +=cut + +sub to_api_mapping { + return { 'itemtype' => 'item_type' }; +} + +sub from_api_mapping { + return { 'item_type' => 'itemtype' }; } =head3 type diff --git a/api/v1/swagger/definitions/itemtype.yaml b/api/v1/swagger/definitions/item_type.yaml similarity index 94% rename from api/v1/swagger/definitions/itemtype.yaml rename to api/v1/swagger/definitions/item_type.yaml index e9e156db28..6023869aa4 100644 --- a/api/v1/swagger/definitions/itemtype.yaml +++ b/api/v1/swagger/definitions/item_type.yaml @@ -1,7 +1,7 @@ --- type: object properties: - itemtype: + item_type: type: string description: Unique key, a code associated with the item type readOnly: true @@ -70,7 +70,7 @@ properties: - string - "null" sip_media_type: - description: SIP2 protocol media type for this itemtype + description: SIP2 protocol media type for this item type type: - string - "null" @@ -89,8 +89,8 @@ properties: description: Translations of description plain text type: array items: - $ref: "itemtype_translated_description.yaml" + $ref: "item_type_translated_description.yaml" additionalProperties: false required: - - itemtype + - item_type diff --git a/api/v1/swagger/definitions/itemtype_translated_description.yaml b/api/v1/swagger/definitions/itemtype_translated_description.yaml deleted file mode 100644 index 8cb95287a6..0000000000 --- a/api/v1/swagger/definitions/itemtype_translated_description.yaml +++ /dev/null @@ -1,13 +0,0 @@ ---- -type: object -properties: - lang: - description: Language identifier - type: string - translation: - description: Translated plain text - type: string -additionalProperties: false -required: - - lang - - translation diff --git a/api/v1/swagger/paths/itemtypes.yaml b/api/v1/swagger/paths/item_types.yaml similarity index 92% rename from api/v1/swagger/paths/itemtypes.yaml rename to api/v1/swagger/paths/item_types.yaml index a13c53b894..d359a40d62 100644 --- a/api/v1/swagger/paths/itemtypes.yaml +++ b/api/v1/swagger/paths/item_types.yaml @@ -1,10 +1,10 @@ --- -/itemtypes: +/item_types: get: x-mojo-to: ItemTypes#list operationId: listItemTypes tags: - - itemtypes + - item_types summary: List item types produces: - application/json @@ -27,10 +27,10 @@ - $ref: "../swagger.yaml#/parameters/q_body" responses: 200: - description: A list of itemtypes + description: A list of item types schema: items: - $ref: "../swagger.yaml#/definitions/itemtype" + $ref: "../swagger.yaml#/definitions/item_type" type: array 400: description: Bad request diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 2b5248339b..5319f868d0 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -72,8 +72,8 @@ definitions: $ref: ./definitions/item.yaml item_group: $ref: ./definitions/item_group.yaml - itemtype: - $ref: ./definitions/itemtype.yaml + item_type: + $ref: ./definitions/item_type.yaml job: $ref: ./definitions/job.yaml library: @@ -265,8 +265,8 @@ paths: $ref: ./paths/import_batch_profiles.yaml#/~1import_batch_profiles "/import_batch_profiles/{import_batch_profile_id}": $ref: "./paths/import_batch_profiles.yaml#/~1import_batch_profiles~1{import_batch_profile_id}" - /itemtypes: - $ref: ./paths/itemtypes.yaml#/~1itemtypes + /item_types: + $ref: ./paths/item_types.yaml#/~1item_types /items: $ref: ./paths/items.yaml#/~1items "/items/{item_id}": @@ -851,7 +851,7 @@ tags: name: items x-displayName: Items - description: "Manage item types\n" - name: itemtypes + name: item_types x-displayName: Item Types - description: "Manage jobs\n" name: jobs diff --git a/t/db_dependent/api/v1/itemtypes.t b/t/db_dependent/api/v1/item_types.t similarity index 84% rename from t/db_dependent/api/v1/itemtypes.t rename to t/db_dependent/api/v1/item_types.t index 71e3d634fb..d45208ccfb 100755 --- a/t/db_dependent/api/v1/itemtypes.t +++ b/t/db_dependent/api/v1/item_types.t @@ -32,19 +32,19 @@ my $builder = t::lib::TestBuilder->new; my $t = Test::Mojo->new('Koha::REST::V1'); t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 ); -subtest 'list_itemtypes() tests' => sub { +subtest 'list_item_types() tests' => sub { plan tests => 12; $schema->storage->txn_begin; - my $itemtype = $builder->build_object( + my $item_type = $builder->build_object( { class => 'Koha::ItemTypes', value => { itemtype => 'TEST_IT', parent_type => undef, - description => 'Test itemtype', + description => 'Test item type', rentalcharge => 100.0, rentalcharge_daily => 50., rentalcharge_daily_calendar => 0, @@ -54,7 +54,7 @@ subtest 'list_itemtypes() tests' => sub { processfee => 20.0, notforloan => 0, imageurl => 'https://upload.wikimedia.org/wikipedia/commons/1/1f/202208_test-tube-4.svg', - summary => 'An itemtype for testing', + summary => 'An item type for testing', checkinmsg => 'Checking in test', checkinmsgtype => 'message', sip_media_type => 'spt', @@ -110,21 +110,21 @@ subtest 'list_itemtypes() tests' => sub { ## Authorized user tests # No category, 404 expected - $t->get_ok("//$userid:$password@/api/v1/itemtypes")->status_is(200)->json_has('/0'); + $t->get_ok("//$userid:$password@/api/v1/item_types")->status_is(200)->json_has('/0'); for my $json ( @{ $t->tx->res->json } ) { - if ( $json->{itemtype} eq 'TEST_IT' ) { - is( $json->{description}, 'Test itemtype' ); + if ( $json->{item_type} eq 'TEST_IT' ) { + is( $json->{description}, 'Test item type' ); ok( !exists $json->{translated_descriptions} ); } } - $t->get_ok( "//$userid:$password@/api/v1/itemtypes" => { 'x-koha-embed' => 'translated_descriptions' } ) + $t->get_ok( "//$userid:$password@/api/v1/item_types" => { 'x-koha-embed' => 'translated_descriptions' } ) ->status_is(200)->json_has('/0'); for my $json ( @{ $t->tx->res->json } ) { - if ( $json->{itemtype} eq 'TEST_IT' ) { - is( $json->{description}, 'Test itemtype' ); + if ( $json->{item_type} eq 'TEST_IT' ) { + is( $json->{description}, 'Test item type' ); is_deeply( $json->{translated_descriptions}, [ @@ -136,7 +136,7 @@ subtest 'list_itemtypes() tests' => sub { } # Unauthorized access - $t->get_ok("//$unauth_userid:$password@/api/v1/itemtypes")->status_is(403); + $t->get_ok("//$unauth_userid:$password@/api/v1/item_types")->status_is(403); $schema->storage->txn_rollback; }; -- 2.30.2