<commit_before>package CoGe::Services::API::Genome;

use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json);
use Data::Dumper;

use CoGe::Services::Auth qw(init);
use CoGe::Services::API::Job;
use CoGe::Core::Genome qw(genomecmp search_genomes);
use CoGe::Core::Storage qw(get_genome_seq);
use CoGe::Core::Favorites;
use CoGe::Accessory::Utils qw(sanitize_name);
use CoGeDBI qw(get_feature_counts);

sub search {
    my $self = shift;
    my $search_term = $self->stash('term');
    my $fast = $self->param('fast');
    $fast = (defined $fast && ($fast eq '1' || $fast eq 'true')); # default to false
    my $sort = $self->param('sort');
    $sort = (defined $sort && ($sort eq '1' || $sort eq 'true')); # default to false

    # Validate input
    if (!$search_term or length($search_term) < 3) {
        $self->render(status => 400, json => { error => { Error => 'Search term is shorter than 3 characters' } });
        return;
    }

    # Authenticate user and connect to the database
    my ($db, $user) = CoGe::Services::Auth::init($self);
    
    # Search notebooks and filter based on user permissions
    my $filtered = search_genomes(db => $db, search_term => $search_term, user => $user, sort => $sort);

    # Get user's favorites
    my $favorites = CoGe::Core::Favorites->new(user => $user);

    # Format response
    my @result;
    if ($fast) {
        @result = map {
          {
            id => int($_->id),
            info => $_->info,
            certified  => $_->certified ? Mojo::JSON->true : Mojo::JSON->false,
            restricted => $_->restricted ? Mojo::JSON->true : Mojo::JSON->false,
            favorited  => $favorites->is_favorite($_) ? Mojo::JSON->true : Mojo::JSON->false
          }
        } @$filtered;
    }
    else {
        @result = map {
          {
            id => int($_->id),
            name => $_->name,
            description => $_->description,
            link => $_->link,
            version => $_->version,
            info => $_->info,
            organism_id  => int($_->organism->id),
            sequence_type => {
                id => int($_->type ? $_->type->id : 0),
                name => ($_->type ? $_->type->name : ''),
                description => ($_->type ? $_->type->description : ''),
            },
            restricted => $_->restricted ? Mojo::JSON->true : Mojo::JSON->false,
            certified => $_->certified ? Mojo::JSON->true : Mojo::JSON->false,
            chromosome_count => int($_->chromosome_count),
            organism => {
                id => int($_->organism->id),
                name => $_->organism->name,
                description => $_->organism->description
            }
          }
        } @$filtered;
    }
    
    $self->render(json => { genomes => \@result });
}

sub fetch {
    my $self = shift;
    my $id = int($self->stash('id'));
    
    # Validate input
    unless ($id) {
        $self->render(status => 400, json => {
            error => { Error => "Invalid input"}
        });
        return;
    }
<commit_msg>Add "deleted" flag to search results<commit_after>package CoGe::Services::API::Genome;

use Mojo::Base 'Mojolicious::Controller';
use Mojo::JSON qw(decode_json);
use Data::Dumper;

use CoGe::Services::Auth qw(init);
use CoGe::Services::API::Job;
use CoGe::Core::Genome qw(genomecmp search_genomes);
use CoGe::Core::Storage qw(get_genome_seq);
use CoGe::Core::Favorites;
use CoGe::Accessory::Utils qw(sanitize_name);
use CoGeDBI qw(get_feature_counts);

sub search {
    my $self = shift;
    my $search_term = $self->stash('term');
    my $fast = $self->param('fast');
    $fast = (defined $fast && ($fast eq '1' || $fast eq 'true')); # default to false
    my $sort = $self->param('sort');
    $sort = (defined $sort && ($sort eq '1' || $sort eq 'true')); # default to false

    # Validate input
    if (!$search_term or length($search_term) < 3) {
        $self->render(status => 400, json => { error => { Error => 'Search term is shorter than 3 characters' } });
        return;
    }

    # Authenticate user and connect to the database
    my ($db, $user) = CoGe::Services::Auth::init($self);
    
    # Search notebooks and filter based on user permissions
    my $filtered = search_genomes(db => $db, search_term => $search_term, user => $user, sort => $sort);

    # Get user's favorites
    my $favorites = CoGe::Core::Favorites->new(user => $user);

    # Format response
    my @result;
    if ($fast) {
        @result = map {
          {
            id => int($_->id),
            info => $_->info,
            certified  => $_->certified ? Mojo::JSON->true : Mojo::JSON->false,
            restricted => $_->restricted ? Mojo::JSON->true : Mojo::JSON->false,
            deleted    => $_->deleted ? Mojo::JSON->true : Mojo::JSON->false,
            favorited  => $favorites->is_favorite($_) ? Mojo::JSON->true : Mojo::JSON->false
          }
        } @$filtered;
    }
    else {
        @result = map {
          {
            id          => int($_->id),
            name        => $_->name,
            description => $_->description,
            link        => $_->link,
            version     => $_->version,
            info        => $_->info,
            organism_id   => int($_->organism->id),
            sequence_type => {
                id          => ($_->type ? $_->type->id : 0),
                name        => ($_->type ? $_->type->name : ''),
                description => ($_->type ? $_->type->description : ''),
            },
            restricted => $_->restricted ? Mojo::JSON->true : Mojo::JSON->false,
            certified  => $_->certified ? Mojo::JSON->true : Mojo::JSON->false,
            deleted    => $_->deleted ? Mojo::JSON->true : Mojo::JSON->false,
            chromosome_count => int($_->chromosome_count),
            organism => {
                id          => int($_->organism->id),
                name        => $_->organism->name,
                description => $_->organism->description
            }
          }
        } @$filtered;
    }
    
    $self->render(json => { genomes => \@result });
}

sub fetch {
    my $self = shift;
    my $id = int($self->stash('id'));
    
    # Validate input
    unless ($id) {
        $self->render(status => 400, json => {
            error => { Error => "Invalid input"}
        });
        return;
    }

{'fraction_non_alphanumeric': 0.17748510504860457, 'fraction_numerical': 0.003449357165255566, 'mean_word_length': 2.108674463937622, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 185, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '127730', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1935, 'n_words': 449}