From b69781c1e813454383f9890792d3c2b35d747bd4 Mon Sep 17 00:00:00 2001 From: Marius Gavrilescu Date: Thu, 18 Jun 2015 17:08:41 +0300 Subject: [PATCH] Simplify several functions --- lib/Gruntmaster/Data.pm | 68 ++++++++++------------------------------- t/Gruntmaster-Data.t | 45 +++++++++++++-------------- 2 files changed, 37 insertions(+), 76 deletions(-) diff --git a/lib/Gruntmaster/Data.pm b/lib/Gruntmaster/Data.pm index 8a0a723..4bd7513 100644 --- a/lib/Gruntmaster/Data.pm +++ b/lib/Gruntmaster/Data.pm @@ -90,7 +90,7 @@ sub add_names ($) { ## no critic (ProhibitSubroutinePrototypes) $el } -sub user_list { +{us => scalar query('user_list_sth')->hashes} } +sub user_list { scalar query('user_list_sth')->hashes } sub user_entry { my ($id) = @_; @@ -111,14 +111,7 @@ sub problem_list { $where{owner} = $args{owner} if $args{owner}; my $table = $args{contest} ? 'problems JOIN contest_problems cp ON cp.problem = id' : 'problems'; - my $ret = add_names $db->select(\$table, \@columns, \%where, 'name')->hashes; - - my %params; - for (@$ret) { - $params{$_->{level}} //= []; - push @{$params{$_->{level}}}, $_ - } - \%params + add_names $db->select(\$table, \@columns, \%where, 'name')->hashes } sub problem_entry { @@ -137,42 +130,19 @@ sub problem_entry { $ret } -sub problem_full { - my ($id) = @_; - scalar query(problem_full_sth => $id)->hash; -} +sub problem_full { scalar query(problem_full_sth => $_[0])->hash } -sub contest_list { - my $ret = add_names query('contest_list_sth')->hashes; +sub contest_list { add_names query('contest_list_sth')->hashes } - my %ret; - for (@$ret) { - my $state = $_->{finished} ? 'finished' : $_->{started} ? 'running' : 'pending'; - $ret{$state} //= []; - push @{$ret{$state}}, $_; - } +sub contest_entry { add_names query(contest_entry_sth => $_[0])->hash } - \%ret -} +sub contest_full { scalar query(contest_full_sth => $_[0])->hash } -sub contest_entry { - my ($id) = @_; - add_names query(contest_entry_sth => $id)->hash; -} - -sub contest_full { - my ($id) = @_; - scalar query(contest_full_sth => $id)->hash; -} - -sub contest_has_problem { - my ($contest, $problem) = @_; - query('contest_has_problem_sth', $contest, $problem)->flat -} +sub contest_has_problem { query('contest_has_problem_sth', @_[0, 1])->flat } sub job_list { my (%args) = @_; - $args{page} //= 1; + $args{page} = int ($args{page} // 1); my %where = ( maybe contest => $args{contest}, maybe owner => $args{owner}, @@ -184,29 +154,23 @@ sub job_list { my $rows = $db->select('job_entry', 'COUNT(*)', \%where)->list; my $pages = int (($rows + JOBS_PER_PAGE - 1) / JOBS_PER_PAGE); my ($stmt, @bind) = $db->abstract->select('job_entry', '*', \%where, {-desc => 'id'}); - my $jobs = $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes; - my %ret = ( - log => add_names $jobs, + my $jobs = add_names $db->query("$stmt LIMIT " . JOBS_PER_PAGE . ' OFFSET ' . ($args{page} - 1) * JOBS_PER_PAGE, @bind)->hashes; + my $pageinfo = { current_page => $args{page}, last_page => $pages, - ); - $ret{previous_page} = $args{page} - 1 if $args{page} - 1; - $ret{next_page} = $args{page} + 1 if $args{page} < $pages; - - \%ret; + ($args{page} - 1) ? (previous_page => $args{page} - 1) : (), + ($args{page} < $pages) ? (next_page => $args{page} + 1) : (), + }; + wantarray ? ($jobs, $pageinfo) : $jobs; } sub job_entry { - my ($id) = @_; - my $ret = add_names query(job_entry_sth => $id)->hash; + my $ret = add_names query(job_entry_sth => $_[0])->hash; $ret->{results} = decode_json $ret->{results} if $ret->{results}; $ret } -sub job_full { - my ($id) = @_; - scalar query(job_full_sth => $id)->hash -} +sub job_full { scalar query(job_full_sth => $_[0])->hash } sub create_job { my (%args) = @_; diff --git a/t/Gruntmaster-Data.t b/t/Gruntmaster-Data.t index 54861af..0b325a3 100644 --- a/t/Gruntmaster-Data.t +++ b/t/Gruntmaster-Data.t @@ -10,7 +10,7 @@ BEGIN { eval { Gruntmaster::Data::init 'dbi:Pg:dbname=gmtest'; 1; } or plan skip_all => 'Cannot connect to test database. Create it by running createdb gmtest before running this test. '. "Error: $@"; - plan tests => 37; + plan tests => 35; } note 'Setting up test database'; @@ -21,7 +21,7 @@ system 'psql', 'gmtest', '-qf', 'testdata.sql'; note 'Running update_status'; update_status; -my $x = user_list->{us}; +my $x = user_list; is @$x, 2, 'user_list has two elements'; is_deeply $x->[0], {id => 'nobody', admin => 0, name => undef, town => undef, university => undef, country => undef, level => undef, lastjob => undef, contests => 1, solved => 2, attempted => 0}, 'user_list first element is correct'; is $x->[1]{admin}, 1, 'user_list second user is admin'; @@ -36,22 +36,22 @@ is_deeply $x->{contests}, [ {contest => 'fc', contest_name => 'Finished contest', rank => 2, score => 40}, ], 'user_entry contests'; -sub pbids { [map { $_->{id} } @{$x->{beginner}}] } +sub ids { [map { $_->{id} } @$x] } $x = problem_list; -cmp_bag pbids, [qw/arc fca/], 'problem_list'; +cmp_bag ids, [qw/arc fca/], 'problem_list'; $x = problem_list private => 1; -cmp_bag pbids, [qw/arc fca rca pca prv/], 'problem_list private => 1'; +cmp_bag ids, [qw/arc fca rca pca prv/], 'problem_list private => 1'; $x = problem_list contest => 'rc'; -cmp_bag pbids, [qw/rca/], q/problem_list contest => 'rc'/; +cmp_bag ids, [qw/rca/], q/problem_list contest => 'rc'/; $x = problem_list contest => 'rc', solution => 1; -ok exists $x->{beginner}[0]{solution}, q/problem_list contest => 'rc', solution => 1 has solution/; +ok exists $x->[0]{solution}, q/problem_list contest => 'rc', solution => 1 has solution/; $x = problem_list owner => 'nobody'; -cmp_bag pbids, [], q/problem_list owner => 'nobody'/; +cmp_bag ids, [], q/problem_list owner => 'nobody'/; $x = problem_entry 'arc'; cmp_bag $x->{limits}, [{format => 'C', timeout => 0.1}, {format => 'CPP', timeout => 0.1}], 'problem_entry limits'; @@ -62,9 +62,7 @@ ok !exists $x->{solution}, 'problem_entry during contest does not have solution' ok exists $x->{contest_start}, 'problem_entry during contest has contest_start '; $x = contest_list; -is $x->{finished}[0]{id}, 'fc', 'contest_list fc is finished'; -is $x->{running}[0]{id}, 'rc', 'contest_list rc is running'; -is $x->{pending}[0]{id}, 'pc', 'contest_list pc is pending'; +cmp_bag ids, [qw/pc rc fc/], 'contest_list'; $x = contest_entry 'fc'; cmp_deeply $x, {id => 'fc', name => 'Finished contest', start => ignore, stop => ignore, owner => 'MGV', owner_name => undef, finished => bool (1), started => bool (1), description => undef}, 'contest_entry fc'; @@ -75,29 +73,28 @@ ok exists $x->{editorial}, 'contest_full fc has editorial'; ok contest_has_problem('rc', 'rca'), 'contest rc has problem rca'; ok contest_has_problem('rc', 'arc'), 'contest rc does not have problem arc'; -sub jobids { [ map { $_->{id} } @{$x->{log}} ] } - -$x = job_list; -cmp_bag jobids, [1..5], 'job_list'; -is $x->{current_page}, 1, 'current page is 1'; -is $x->{last_page}, 1, 'last page is 1'; -ok !exists $x->{previous_page}, 'there is no previous page'; -ok !exists $x->{next_page}, 'there is no next page'; +my $pageinfo; +($x, $pageinfo) = job_list; +cmp_bag ids, [1..5], 'job_list'; +is $pageinfo->{current_page}, 1, 'current page is 1'; +is $pageinfo->{last_page}, 1, 'last page is 1'; +ok !exists $pageinfo->{previous_page}, 'there is no previous page'; +ok !exists $pageinfo->{next_page}, 'there is no next page'; $x = job_list private => 1; -cmp_bag jobids, [1..7], 'job_list private => 1'; +cmp_bag ids, [1..7], 'job_list private => 1'; $x = job_list contest => 'fc'; -cmp_bag jobids, [1..3], 'job_list contest => fc'; +cmp_bag ids, [1..3], 'job_list contest => fc'; $x = job_list owner => 'MGV'; -cmp_bag jobids, [1], 'job_ids owner => MGV'; +cmp_bag ids, [1], 'job_ids owner => MGV'; $x = job_list problem => 'fca'; -cmp_bag jobids, [1..4], 'job_ids problem => fca'; +cmp_bag ids, [1..4], 'job_ids problem => fca'; $x = job_list problem => 'fca', result => 1; -cmp_bag jobids, [2], 'job_ids problem => fca, result => 1'; +cmp_bag ids, [2], 'job_ids problem => fca, result => 1'; $x = job_entry 1; is $x->{size}, 21, 'job_entry size'; -- 2.39.2