From Redis to Postgres - Part 2 (Better routes)
[gruntmaster-page.git] / lib / Gruntmaster / Page / Generic.pm
CommitLineData
fdbf59e5
MG
1package Gruntmaster::Page::Generic;
2
3use 5.014000;
4use strict;
5use warnings;
e046c73a 6our $VERSION = '5999.000_001';
fdbf59e5 7
fdbf59e5 8use Gruntmaster::Page::Base;
5c6aea93 9use JSON qw/encode_json decode_json/;
d3200993
MG
10use Plack::Request;
11use Sub::Name qw/subname/;
fdbf59e5 12
d3200993 13use constant PAGE_SIZE => 10;
fdbf59e5
MG
14
15sub putsym {
16 my ($key, $value) = @_;
17 no strict 'refs';
d3200993 18 subname $key => $value if ref $value eq 'CODE';
fdbf59e5
MG
19 *{"$key"} = $value;
20}
21
22sub makepkg {
23 my ($pkg, $id, $title) = @_;
24 my $fn = $pkg =~ s,::,/,gr;
25 return if $INC{"$fn.pm"};
26 $INC{"$fn.pm"} = 1;
27 Gruntmaster::Page::Base->import_to($pkg, $id, $title);
5c6aea93 28 1
fdbf59e5
MG
29}
30
81cce380 31sub list {
491e82eb 32 my ($thing, $lang, $env) = @_;
81cce380 33 my %thing = %$thing;
491e82eb
MG
34 #debug $env => "Contest is $ct";
35 $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) };
36 my $rs = $thing{makers}->($env);
37 $rs = $rs->search(undef, {order_by => 'me.id'}) unless $rs->is_ordered;
38 if (my $page = $env->{'gruntmaster.page'}) {
d3200993
MG
39 my $pages = $rs->count / PAGE_SIZE;
40 $rs = $rs->search(undef, {offset => ($page - 1) * PAGE_SIZE, ($page == $pages ? () : (rows => PAGE_SIZE))});
41 }
42 $rs = $rs->search(undef, {prefetch => $thing{prefetch}}) if exists $thing{prefetch};
43 my @thing = map +{rs => $_, $_->get_columns}, $rs->all;
81cce380
MG
44 @thing = map { $thing{mangle}->(); $_ } @thing if exists $thing{mangle};
45 @thing = grep { $thing{choose}->() } @thing if exists $thing{choose};
46 @thing = sort { $thing{sortby}->() } @thing if exists $thing{sortby};
47 my %params;
48 $thing{group} //= sub { $thing{id} };
49 for (@thing) {
50 my $group = $thing{group}->();
51 $params{$group} //= [];
52 push $params{$group}, $_
fdbf59e5 53 }
81cce380 54 wantarray ? %params : \%params
fdbf59e5
MG
55}
56
81cce380 57sub entry {
491e82eb 58 my ($thing, $lang, $env, $id) = @_;
81cce380 59 my %thing = %$thing;
d3200993 60 debug $env => "Rsname is $thing{rsname} and id is $id";
491e82eb
MG
61 $thing{makers} //= sub { db(shift)->resultset($thing{rsname}) };
62 my %params = map {+ rs => $_, $_->get_columns } $thing{makers}->($env)->find($id);
81cce380
MG
63 $thing{mangle}->(local $_ = \%params) if exists $thing{mangle};
64 wantarray ? %params : \%params
fdbf59e5
MG
65}
66
81cce380
MG
67sub headers ($) { ['Content-Type' => 'application/json', 'Cache-Control' => 'max-age=' . $_[0]->max_age] }
68
fdbf59e5
MG
69sub create_thing {
70 my %thing = @_;
71 my $ucid = ucfirst $thing{id};
72 my $pkg = "Gruntmaster::Page::$ucid";
73
81cce380
MG
74 putsym "${pkg}::_generate", sub { $_[1]->param(list \%thing, @_[2..$#_]) } if makepkg $pkg, @thing{qw/id title/};
75 putsym "${pkg}::Entry::_generate", sub { $_[1]->param(entry \%thing, @_[2..$#_]) } if makepkg "${pkg}::Entry", "$thing{id}_entry", '<tmpl_var name>';
76 putsym "${pkg}::Read::generate", sub { [200, headers shift, [encode_json list \%thing, @_]] } if makepkg "${pkg}::Read";
77 putsym "${pkg}::Entry::Read::generate", sub { [200, headers shift, [encode_json entry \%thing, @_]] } if makepkg "${pkg}::Entry::Read";
fdbf59e5
MG
78}
79
80sub params;
d3200993 81sub makers (&);
fdbf59e5
MG
82sub choose (&);
83sub sortby (&);
84sub group (&);
85sub mangle (&);
d3200993 86sub prefetch;
fdbf59e5
MG
87
88sub thing (&){
89 my %thing;
90 no strict 'refs';
d3200993 91 local *{"params"} = sub { @thing{qw/id rsname title/} = @_ };
fdbf59e5
MG
92 local *{"choose"} = sub { $thing{choose} = shift };
93 local *{"sortby"} = sub { $thing{sortby} = shift };
94 local *{"mangle"} = sub { $thing{mangle} = shift };
95 local *{"group"} = sub { $thing{group} = shift };
d3200993
MG
96 local *{"makers"} = sub { $thing{makers} = shift };
97 local *{"prefetch"} = sub { $thing{prefetch} = \@_ };
fdbf59e5
MG
98 use strict 'refs';
99
100 shift->();
101 create_thing %thing
102}
103
104##################################################
105
106thing {
d3200993 107 params qw/us User Users/;
fdbf59e5
MG
108 choose { $_->{name} =~ /\w/ };
109 sortby { lc $a->{name} cmp lc $b->{name} };
110};
111
112thing {
d3200993
MG
113 params qw/pb Problem Problems/;
114 prefetch 'owner';
491e82eb
MG
115 makers {
116 my $env = $_[0];
117 my $db = db $env;
118 return $db->contest($env->{'gruntmaster.contest'})->problems->search(undef, {order_by => 'problem.id'}) if exists $env->{'gruntmaster.contest'};
119 return $db->problems->search({owner => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
120 $db->problems->search({private => 0});
121 };
d3200993 122 sortby { $a->{name} cmp $b->{name}};
fdbf59e5 123 group { $_->{level} };
d3200993
MG
124 mangle {
125 my $env = shift;
126 $_->{owner_name} = $_->{rs}->owner->name;
127 $_->{cansubmit} = $_->{contest} ? time < $_->{rs}->contest->stop : 1;
128 eval {
129 db($env)->open->create({
130 contest => $_->{contest},
131 problem => $_->{id},
132 owner => $env->{REMOTE_USER},
133 })
134 } if $_->{contest} && time >= $_->{rs}->contest->start;
135 };
fdbf59e5
MG
136};
137
138thing {
d3200993
MG
139 params qw/ct Contest Contests/;
140 prefetch 'owner';
d9f11916 141 sortby { $b->{start} <=> $a->{start} };
d3200993
MG
142 group { time < $_->{start} ? 'pending' : time > $_->{stop} ? 'finished' : 'running' };
143 mangle { $_->{started} = time >= $_->{start}; $_->{owner_name} = $_->{rs}->owner->name };
fdbf59e5
MG
144};
145
146thing {
d3200993
MG
147 params qw/log Job/, 'Job log';
148 prefetch 'owner', 'problem';
491e82eb
MG
149 makers {
150 my $env = $_[0];
151 my $db = db $env;
152 return $db->jobs->search({'me.owner' => $env->{'gruntmaster.user'}}) if exists $env->{'gruntmaster.user'};
153 return $db->jobs->search({problem => $env->{'gruntmaster.problem'}}) if exists $env->{'gruntmaster.problem'};
154 $db->jobs->search({contest => $env->{'gruntmaster.contest'}})
155 };
d3200993
MG
156 sortby { $b->{id} <=> $a->{id}};
157 mangle {
158 $_->{results} &&= decode_json $_->{results};
159 $_->{owner_name} = $_->{rs}->owner->name;
160 $_->{problem_name} = $_->{rs}->problem->name;
161 $_->{size} = length $_->{source};
162 delete $_->{source};
163 }
fdbf59e5
MG
164};
165
d3200993
MG
166putsym 'Gruntmaster::Page::Pb::Entry::vary', sub { 'Authorization' };
167putsym 'Gruntmaster::Page::Pb::Entry::max_age', sub { 600 };
168
fdbf59e5 1691
This page took 0.021211 seconds and 4 git commands to generate.