<commit_before>package PhaidraAPI::Controller::Datacite;

use strict;
use warnings;
use v5.10;
use base 'Mojolicious::Controller';
use Mojo::ByteStream qw(b);
use Mojo::JSON qw(encode_json decode_json);
use PhaidraAPI::Model::Object;
use PhaidraAPI::Model::Datacite;

sub get {

  my $self = shift;

  my $pid = $self->stash('pid');
  my $format = $self->param('format');
 
  unless(defined($pid)){
    $self->render(json => { alerts => [{ type => 'danger', msg => 'Undefined pid' }]} , status => 400) ;
    return;
  }

  my $model = PhaidraAPI::Model::Datacite->new;

  my $res = $model->get($self, $pid, $self->stash->{basic_auth_credentials}->{username}, $self->stash->{basic_auth_credentials}->{password});
  if($res->{status} ne 200){
    $self->render(json => { alerts => $res->{alerts} }, status => $res->{status});
    return;
  }

  # when extracting metadata using Mojo::DOM we use ->content which returns UTF-8 encoded data
  # since we're not going to save the data to fedora but render them, we need to decode them first before passing it to renderer because
  # the renderer will UTF-8 encode the data again
  $self->_decode_rec(undef, $res->{datacite}->{datacite_elements});

  if($format eq 'xml'){
    $self->render(text => $model->json_2_xml($self, $res->{datacite}), format => 'xml');
    return;
  }

  $self->respond_to(<commit_msg>datacite xml render fix<commit_after>package PhaidraAPI::Controller::Datacite;

use strict;
use warnings;
use v5.10;
use base 'Mojolicious::Controller';
use Mojo::ByteStream qw(b);
use Mojo::JSON qw(encode_json decode_json);
use PhaidraAPI::Model::Object;
use PhaidraAPI::Model::Datacite;

sub get {

  my $self = shift;

  my $pid = $self->stash('pid');
  my $format = $self->param('format');
 
  unless(defined($pid)){
    $self->render(json => { alerts => [{ type => 'danger', msg => 'Undefined pid' }]} , status => 400) ;
    return;
  }

  my $model = PhaidraAPI::Model::Datacite->new;

  my $res = $model->get($self, $pid, $self->stash->{basic_auth_credentials}->{username}, $self->stash->{basic_auth_credentials}->{password});
  if($res->{status} ne 200){
    $self->render(json => { alerts => $res->{alerts} }, status => $res->{status});
    return;
  }

  # when extracting metadata using Mojo::DOM we use ->content which returns UTF-8 encoded data
  # since we're not going to save the data to fedora but render them, we need to decode them first before passing it to renderer because
  # the renderer will UTF-8 encode the data again
  $self->_decode_rec(undef, $res->{datacite}->{datacite_elements});

  if($format eq 'xml'){
    $self->render(text => $model->json_2_xml($self, $res->{datacite}->{datacite_elements}), format => 'xml');
    return;
  }

  $self->respond_to(
{'fraction_non_alphanumeric': 0.17670682730923695, 'fraction_numerical': 0.008762322015334063, 'mean_word_length': 4.626283367556468, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 7}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4491294', 'n_tokens_mistral': 978, 'n_tokens_neox': 930, 'n_words': 242}