Rev | Line | |
---|
[3] | 1 | package Vote::Model::Vote; |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use warnings; |
---|
| 5 | use base 'Catalyst::Model'; |
---|
| 6 | use DBI; |
---|
| 7 | |
---|
| 8 | =head1 NAME |
---|
| 9 | |
---|
| 10 | Vote::Model::Vote - Catalyst Model |
---|
| 11 | |
---|
| 12 | =head1 DESCRIPTION |
---|
| 13 | |
---|
| 14 | Catalyst Model. |
---|
| 15 | |
---|
| 16 | =cut |
---|
| 17 | |
---|
| 18 | sub new { |
---|
| 19 | my ($class) = @_; |
---|
| 20 | my $db = DBI->connect( |
---|
| 21 | 'dbi:Pg:' . Vote->config->{db}, |
---|
| 22 | undef, undef, |
---|
| 23 | { |
---|
| 24 | RaiseError => 0, |
---|
| 25 | AutoCommit => 0, |
---|
| 26 | PrintWarn => 0, |
---|
| 27 | PrintError => 1, |
---|
| 28 | } |
---|
| 29 | ) or return; |
---|
| 30 | |
---|
| 31 | bless { |
---|
| 32 | db => $db, |
---|
| 33 | }, $class; |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | sub db { $_[0]->{db} } |
---|
| 37 | |
---|
| 38 | sub list_running_vote { |
---|
| 39 | my ($self) = @_; |
---|
| 40 | |
---|
| 41 | my $sth = $self->db->prepare_cached( |
---|
| 42 | q{ |
---|
| 43 | select id from poll where start < now() and "end" > now() |
---|
| 44 | } |
---|
| 45 | ); |
---|
| 46 | |
---|
| 47 | $sth->execute; |
---|
| 48 | my @id; |
---|
| 49 | while(my $res = $sth->fetchrow_hashref) { |
---|
| 50 | push(@id, $res->{id}); |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | @id |
---|
| 54 | } |
---|
| 55 | |
---|
| 56 | sub vote_status { |
---|
| 57 | my ($self, $id) = @_; |
---|
| 58 | |
---|
| 59 | my $sth = $self->db->prepare_cached( |
---|
| 60 | q{ |
---|
| 61 | select start < now() as before |
---|
| 62 | "end" > now() as after |
---|
| 63 | where id = ? |
---|
| 64 | } |
---|
| 65 | ); |
---|
| 66 | $sth->execute($id); |
---|
| 67 | my $res = $sth->fetchrow_hashref; |
---|
| 68 | $sth->finish; |
---|
| 69 | $res or return; |
---|
| 70 | if ($res->{before}) { |
---|
| 71 | return 'BEFORE'; |
---|
| 72 | } elsif ($res->{after}) { |
---|
| 73 | return 'AFTER'; |
---|
| 74 | } else { |
---|
| 75 | return 'RUNNING'; |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | sub vote_info { |
---|
| 80 | my ($self, $id) = @_; |
---|
| 81 | |
---|
| 82 | my $sth = $self->db->prepare_cached( |
---|
| 83 | q{ |
---|
| 84 | select * from poll where id = ? |
---|
| 85 | } |
---|
| 86 | ); |
---|
| 87 | |
---|
| 88 | $sth->execute($id); |
---|
| 89 | my $res = $sth->fetchrow_hashref; |
---|
| 90 | $sth->finish; |
---|
| 91 | $res |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | sub vote_signing { |
---|
| 95 | my ($self, $id) = @_; |
---|
| 96 | |
---|
| 97 | my $sth = $self->db->prepare_cached( |
---|
| 98 | q{ |
---|
| 99 | select * from voting left join signing |
---|
| 100 | on signing.key = voting.key |
---|
| 101 | where poll = ? order by voting.id |
---|
| 102 | } |
---|
| 103 | ); |
---|
| 104 | $sth->execute($id); |
---|
| 105 | my @people; |
---|
| 106 | while (my $res = $sth->fetchrow_hashref) { |
---|
| 107 | push(@people, $res); |
---|
| 108 | } |
---|
| 109 | @people |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | =head1 AUTHOR |
---|
| 113 | |
---|
| 114 | Thauvin Olivier |
---|
| 115 | |
---|
| 116 | =head1 LICENSE |
---|
| 117 | |
---|
| 118 | This library is free software, you can redistribute it and/or modify |
---|
| 119 | it under the same terms as Perl itself. |
---|
| 120 | |
---|
| 121 | =cut |
---|
| 122 | |
---|
| 123 | 1; |
---|
Note: See
TracBrowser
for help on using the repository browser.