Bug 33161: (follow-up) Consistent use of db fields throughout

We should stick to DB field names as long as possible to allow re-use
and only convert to api field names via to_api_mapping at the last
moment inside the parent to_api function.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
This commit is contained in:
Martin Renvoize 2023-03-09 12:43:05 +00:00 committed by Tomas Cohen Arazi
parent dab0260702
commit 04736dd0ba
2 changed files with 36 additions and 25 deletions

View File

@ -2089,17 +2089,6 @@ sub api_strings_mapping {
foreach my $col ( keys %{$columns_info} ) {
# Skip columns not in public read list
next
unless !$params->{public}
|| any { $col eq $_ } $public_read_list;
# Skip columns that are not exposed on the API by to_api_mapping
# i.e. mapping exists but points to undef
next
if $col eq 'more_subfields_xml' # not dealt with as a regular field
|| ( exists $to_api_mapping->{$col} && !defined $to_api_mapping->{$col} );
# By now, we are done with known columns, now check the framework for mappings
my $field = $self->_result->result_source->name . '.' . $col;
@ -2113,11 +2102,7 @@ sub api_strings_mapping {
my $str = C4::Biblio::GetAuthorisedValueDesc( $itemtagfield, $subfield->{tagsubfield}, $self->$col, '', $tagslib, undef, $params->{public} );
my $type = exists $code_to_type->{$code} ? $code_to_type->{$code} : 'av';
# The _strings entry should match the API attribute name
my $mapped_attr = exists $to_api_mapping->{$col} ? $to_api_mapping->{$col} : $col;
$strings->{$mapped_attr} = {
$strings->{$col} = {
str => $str,
type => $type,
( $type eq 'av' ? ( category => $code ) : () ),

View File

@ -570,13 +570,14 @@ sub to_api {
# Remove forbidden attributes if required (including their coded values)
if ( $params->{public} ) {
for my $field ( keys %{$json_object} ) {
delete $json_object->{$field} unless any { $_ eq $field } @{ $self->public_read_list };
delete $json_object->{$field}
unless any { $_ eq $field } @{ $self->public_read_list };
}
if ( $strings ) {
foreach my $field (keys %{$avs}) {
if ($strings) {
foreach my $field ( keys %{$avs} ) {
delete $avs->{$field}
unless any { $_ eq $field } @{ $self->public_read_list };
unless any { $_ eq $field } @{ $self->public_read_list };
}
}
}
@ -588,15 +589,18 @@ sub to_api {
foreach my $column ( keys %{ $self->to_api_mapping } ) {
my $mapped_column = $self->to_api_mapping->{$column};
if ( exists $json_object->{$column}
&& defined $mapped_column ) {
&& defined $mapped_column )
{
# key != undef
$json_object->{$mapped_column} = delete $json_object->{$column};
$avs->{$mapped_column} = delete $avs->{$column}
if exists $avs->{$column};
} elsif ( exists $json_object->{$column}
&& !defined $mapped_column ) {
}
elsif ( exists $json_object->{$column}
&& !defined $mapped_column )
{
# key == undef
delete $json_object->{$column};
@ -610,8 +614,9 @@ sub to_api {
if ($embeds) {
foreach my $embed ( keys %{$embeds} ) {
if ( $embed =~ m/^(?<relation>.*)_count$/
and $embeds->{$embed}->{is_count} ) {
if ( $embed =~ m/^(?<relation>.*)_count$/
and $embeds->{$embed}->{is_count} )
{
my $relation = $+{relation};
$json_object->{$embed} = $self->$relation->count;
@ -671,6 +676,27 @@ sub to_api_mapping {
return {};
}
=head3 api_strings_mapping
my $params = { is_public => 1 };
my $string_map = $object->api_strings_mapping($params);
Generic method that returns the string map for coded attributes.
Return should be a hashref keyed on database field name with the values
being hashrefs containing 'str', 'type' and optionally 'category'.
This is then use in to_api to render the _strings embed when requested.
Note: this only returns an empty I<hashref>. Each class should have its
own mapping returned.
=cut
sub api_strings_mapping {
return {};
}
=head3 public_read_list