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

use Mojo::Base qw/DeviewSched::Controller/;

sub METHOD_PUT    () { 'PUT' }
sub METHOD_DELETE () { 'DELETE' }
sub COLUMNS_LIST_SCHEDULES_SESSION () { qw/id track day title starts_at ends_at/ }

sub list {
    my $self = shift;

    my ($user, $fetch_all) = map { $self->stash($_) } qw/user fetch_all/;
    my ($year, $day)       = map { $self->param($_) } qw/year day/;
    
    return $self->fail($self->FAIL_BAD_PARAMETERS) 
        if (!(( defined $fetch_all && defined $year ) ||
              ( defined $year && defined $day )));


    my $search_conditions = {
        ( !defined $fetch_all ) ? 

        (
            'session.year' => $year,
            'session.day'  => $day,
        ) : (
            'session.year' => $year
        )
    };

    my $schedule_rs = $user->schedules->search_rs($search_conditions, {
        select => [
            ( map { "session.$_" } COLUMNS_LIST_SCHEDULES_SESSION ) 
        ],

        join     => 'session',
        prefetch => 'session'
    });

    my @sessions;

    # TODO: 발표자 정보 포함하기
    map { 
        push @sessions, $_->session->serialize_columns(
            [COLUMNS_LIST_SCHEDULES_SESSION]
        ); 
    } $schedule_rs->all; 
    
    return $self->render_wrap(200, {
        sessions => \@sessions    
    });
}

sub process {
    my $self = shift;

    my $user   = $self->stash('user');
    my $method = $self->req->method;
    my ($session_year, $session_id) = map { $self->param($_) } qw/year id/;
    
    if ($method eq METHOD_PUT) { 
        my $schedule     = eval { $self->_register_schedule($user, $session_year, $session_id) } 
                           or return $self->fail($self->FAIL_SCHEDULE_INSERTION, $@, $@);

        my $collision_id = $self->stash('collision_id');
        
        if (defined $collision_id) {
            return $self->render_wrap(200, { collision_id => $collision_id });
        } else {
            return $self->render_wrap(201);
        }
    } elsif ($method eq METHOD_DELETE) {
        my $schedule = $self->_find_schedule($user, $session_year, $session_id);

        if (defined $schedule) {
            $schedule->delete;
            return $self->render_wrap(200);
        } else {
            return $self->fail($self->FAIL_SCHEDULE_NOT_FOUND);
        }
    }

    return $self->fail($self->FAIL_UNKNOWN_ERROR);
}

sub process_all {
    my $self = shift;

    my $user   = $self->stash('user');
    my $method = $self->req->method;
    my ($session_year, $data) = map { $self->param($_) } qw/year data/;

    # 트랜잭션 개시
    my $tx_guard = $self->db_schema->txn_scope_guard;

    # remove all schedules
    my $resultset = $self->db_schema->resultset('UserSchedule');
    $resultset->delete_all({
        user_id      => $user->id,
        session_year => $session_year
    });


    if ($method eq METHOD_PUT) { 
        my @sessions = split ",", $data;
        
        for my $session_id (@sessions) {
            eval {
                $self->_register_schedule($self, $session_year, $session_id);
            } or return $self->fail($self->FAIL_SCHEDULE_INSERTION); 
        }
    }

    # 트랜잭션 커밋
    $tx_guard->commit;

    return $self->render_wrap(200);
}

sub _register_schedule {
    my $self = shift;
    my ($user, $session_year, $session_id, $db_schema) = @_;

    my $session = $self->_find_session($session_year, $session_id);
    die "Session not found" unless defined $session;   
    
    my $old_schedule = $self->_find_collision($user, $session);
    if (defined $old_schedule) {
        $self->stash('collision_id' => $old_schedule->session_id);
        $old_schedule->delete;
    }<commit_msg>1. fix(::Controller::UserSchedule): 스케줄 전체 삭제시 발생하는 오류 수정<commit_after>package DeviewSched::Controller::UserSchedule;
use utf8;

use Mojo::Base qw/DeviewSched::Controller/;

sub METHOD_PUT    () { 'PUT' }
sub METHOD_DELETE () { 'DELETE' }
sub COLUMNS_LIST_SCHEDULES_SESSION () { qw/id track day title starts_at ends_at/ }

sub list {
    my $self = shift;

    my ($user, $fetch_all) = map { $self->stash($_) } qw/user fetch_all/;
    my ($year, $day)       = map { $self->param($_) } qw/year day/;
    
    return $self->fail($self->FAIL_BAD_PARAMETERS) 
        if (!(( defined $fetch_all && defined $year ) ||
              ( defined $year && defined $day )));


    my $search_conditions = {
        ( !defined $fetch_all ) ? 

        (
            'session.year' => $year,
            'session.day'  => $day,
        ) : (
            'session.year' => $year
        )
    };

    my $schedule_rs = $user->schedules->search_rs($search_conditions, {
        select => [
            ( map { "session.$_" } COLUMNS_LIST_SCHEDULES_SESSION ) 
        ],

        join     => 'session',
        prefetch => 'session'
    });

    my @sessions;

    # TODO: 발표자 정보 포함하기
    map { 
        push @sessions, $_->session->serialize_columns(
            [COLUMNS_LIST_SCHEDULES_SESSION]
        ); 
    } $schedule_rs->all; 
    
    return $self->render_wrap(200, {
        sessions => \@sessions    
    });
}

sub process {
    my $self = shift;

    my $user   = $self->stash('user');
    my $method = $self->req->method;
    my ($session_year, $session_id) = map { $self->param($_) } qw/year id/;
    
    if ($method eq METHOD_PUT) { 
        my $schedule     = eval { $self->_register_schedule($user, $session_year, $session_id) } 
                           or return $self->fail($self->FAIL_SCHEDULE_INSERTION, $@, $@);

        my $collision_id = $self->stash('collision_id');
        
        if (defined $collision_id) {
            return $self->render_wrap(200, { collision_id => $collision_id });
        } else {
            return $self->render_wrap(201);
        }
    } elsif ($method eq METHOD_DELETE) {
        my $schedule = $self->_find_schedule($user, $session_year, $session_id);

        if (defined $schedule) {
            $schedule->delete;
            return $self->render_wrap(200);
        } else {
            return $self->fail($self->FAIL_SCHEDULE_NOT_FOUND);
        }
    }

    return $self->fail($self->FAIL_UNKNOWN_ERROR);
}

sub process_all {
    my $self = shift;

    my $user   = $self->stash('user');
    my $method = $self->req->method;
    my ($session_year, $data) = map { $self->param($_) } qw/year data/;

    # 트랜잭션 개시
    my $tx_guard = $self->db_schema->txn_scope_guard;

    # remove all schedules
    my $resultset = $self->db_schema->resultset('UserSchedule')
                         ->search_rs({
        user_id      => $user->id,
        session_year => $session_year
    });

    $resultset->delete_all();


    if ($method eq METHOD_PUT) { 
        my @sessions = split ",", $data;
        
        for my $session_id (@sessions) {
            eval {
                $self->_register_schedule($user, $session_year, $session_id);
            } or return $self->fail($self->FAIL_SCHEDULE_INSERTION, $@, $@); 
        }
    }

    # 트랜잭션 커밋
    $tx_guard->commit;

    return $self->render_wrap(200);
}

sub _register_schedule {
    my $self = shift;
    my ($user, $session_year, $session_id, $db_schema) = @_;

    my $session = $self->_find_session($session_year, $session_id);
    die "Session not found" unless defined $session;   
    
    my $old_schedule = $self->_find_collision($user, $session);
    if (defined $old_schedule) {
        $self->stash('collision_id' => $old_schedule->session_id);
        $old_schedule->delete;
    }
{'fraction_non_alphanumeric': 0.15284075753534276, 'fraction_numerical': 0.004401173646305681, 'mean_word_length': 2.250541829215431, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 120, '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': '4429439', 'n_tokens_mistral': 2615, 'n_tokens_neox': 2558, 'n_words': 490}