<commit_before>package DeviewSched::Controller::Sessions;
use utf8;

use Data::Dumper;

use Mojo::Base 'DeviewSched::Controller';


sub list_years {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');

    my @result = map { $_->year } $resultset->search(undef, {
        columns => ['year'],
        distinct => 1,
    });
    
    $self->render(json => {
        years_available => \@result 
    });
}

sub list_sessions {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');

    # { 
    #   tracks => [ 
    #     { sessions => [{ session 1 }, { session 2 } ... ], }, # track #1
    #     { sessions => [{ session 5 }, { session 6 } ... ], }, # track #2
    #     { sessions => [{ session 7 }, { session 8 } ... ], }, # track #3
    #   ]
    # }
    #
    # 이런 느낌일려나! > <)

    my @tracks;

    my @sessions = $resultset->search({
        year => $self->stash('year'),   
    });

    for my $session (@sessions) {
        push @{($tracks[$session->track - 1]->{sessions})}, {
            %{ $session->serialize_columns([qw/id title/]) },
            speakers => [ map {
                $_->serialize_columns([qw/name picture/]);
            } $session->speakers ]
        };
    }
      
    $self->render(json => {
        tracks => \@tracks, 
    });
}

sub session_details {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');
    
    my ($session) = $resultset->search({
        year => $self->stash('year'),
        id   => $self->stash('id'),
    });

    unless (defined $session) {
        return $self->fail($self->FAIL_RESOURCE_NOT_AVAILABLE);
    }

    $self->render(json => $session->serialize);
}

sub speakers {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Speaker');

    my @speakers = map { $_->serialize } $resultset->search({
        session_year => $self->stash('year'),
        session_id   => $self->stash('id'),    
    });

    $self->render(json => {
        year => $self->stash('year'),
        id   => $self->stash('id'),

        speakers => \@speakers
    });
} 

1;
__END__

=encoding utf-8

=head1 NAME

DeviewSched::Controller::Sessions - Deview 세션과 관련된 컨트롤러

=head2 METHODS

<commit_msg>1. perf(::Controller::Sessions): 세션 목록의 처리 속도 향상<commit_after>package DeviewSched::Controller::Sessions;
use utf8;

use Mojo::Base 'DeviewSched::Controller';
use Time::HiRes qw/time/;

sub COLUMNS_LIST_SESSIONS_SESSION () { qw/id track day title starts_at ends_at/ }
sub COLUMNS_LIST_SESSIONS_SPEAKER () { qw/name picture/ }

sub list_years {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');

    my @result = map { $_->year } $resultset->search(undef, {
        columns => ['year'],
        distinct => 1,
    });
    
    $self->render(json => {
        years_available => \@result 
    });
}

sub list_sessions {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');

    my @data;

    my @sessions = $resultset->search({
        year => $self->stash('year'),   
    }, {

        select => [
            (map { "me.$_" } COLUMNS_LIST_SESSIONS_SESSION),
            (map { "speakers.$_" } COLUMNS_LIST_SESSIONS_SPEAKER),
        ],

        join     => 'speakers',
        prefetch => 'speakers',
        order_by => {
            -asc => 'starts_at'
        }
    });

    for my $session (@sessions) {

        my $day   = $session->day;
        my $track = $session->track;

        push @{($data[$day - 1]->{tracks}[$track - 1]->{sessions})}, {
            %{ $session->serialize_columns([COLUMNS_LIST_SESSIONS_SESSION]) },
            speakers => [ map {
                $_->serialize_columns([COLUMNS_LIST_SESSIONS_SPEAKER]);
            } $session->speakers ]
        };
    }
     
    $self->render(json => {
        days => \@data, 
    });
}

sub session_details {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Session');
    
    my ($session) = $resultset->search({
        year => $self->stash('year'),
        id   => $self->stash('id'),
    });

    unless (defined $session) {
        return $self->fail($self->FAIL_RESOURCE_NOT_AVAILABLE);
    }

    $self->render(json => $session->serialize);
}

sub speakers {
    my $self = shift;
    my $resultset = $self->db_schema->resultset('Speaker');

    my @speakers = map { $_->serialize } $resultset->search({
        session_year => $self->stash('year'),
        session_id   => $self->stash('id'),    
    });

    $self->render(json => {
        year => $self->stash('year'),
        id   => $self->stash('id'),

        speakers => \@speakers
    });
} 

1;
__END__

=encoding utf-8

=head1 NAME

DeviewSched::Controller::Sessions - Deview 세션과 관련된 컨트롤러

=head2 ENDPOINTS


{'fraction_non_alphanumeric': 0.18212267113250993, 'fraction_numerical': 0.005233410090014654, 'mean_word_length': 2.2861072902338377, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 115, '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': '2750114', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1658, 'n_words': 302}