Rename Vote object to PollVote

This commit is contained in:
Alex Cabal 2022-07-06 11:36:42 -05:00
parent 8168a125d0
commit 4efc5dcdaf
12 changed files with 41 additions and 30 deletions

View file

@ -1,4 +1,4 @@
CREATE TABLE `Votes` ( CREATE TABLE `PollVotes` (
`UserId` int(11) unsigned NOT NULL, `UserId` int(11) unsigned NOT NULL,
`PollItemId` int(11) unsigned NOT NULL, `PollItemId` int(11) unsigned NOT NULL,
`Created` datetime NOT NULL, `Created` datetime NOT NULL,

View file

@ -1,6 +1,6 @@
<? <?
namespace Exceptions; namespace Exceptions;
class InvalidVoteException extends SeException{ class InvalidPollVoteException extends SeException{
protected $message = 'We couldnt locate that vote.'; protected $message = 'We couldnt locate that vote.';
} }

View file

@ -1,6 +1,6 @@
<? <?
namespace Exceptions; namespace Exceptions;
class VoteExistsException extends SeException{ class PollVoteExistsException extends SeException{
protected $message = 'Youve already voted in this poll.'; protected $message = 'Youve already voted in this poll.';
} }

View file

@ -36,7 +36,7 @@ class Poll extends PropertiesBase{
protected function GetVoteCount(): int{ protected function GetVoteCount(): int{
if($this->_VoteCount === null){ if($this->_VoteCount === null){
$this->_VoteCount = Db::QueryInt('select count(*) from Votes v inner join PollItems pi on v.PollItemId = pi.PollItemId where pi.PollId = ?', [$this->PollId]); $this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollId = ?', [$this->PollId]);
} }
return $this->_VoteCount; return $this->_VoteCount;

View file

@ -19,7 +19,7 @@ class PollItem extends PropertiesBase{
protected function GetVoteCount(): int{ protected function GetVoteCount(): int{
if($this->_VoteCount === null){ if($this->_VoteCount === null){
$this->_VoteCount = Db::QueryInt('select count(*) from Votes v inner join PollItems pi on v.PollItemId = pi.PollItemId where pi.PollItemId = ?', [$this->PollItemId]); $this->_VoteCount = Db::QueryInt('SELECT count(*) from PollVotes pv inner join PollItems pi using (PollItemId) where pi.PollItemId = ?', [$this->PollItemId]);
} }
return $this->_VoteCount; return $this->_VoteCount;

View file

@ -6,8 +6,7 @@ use Safe\DateTime;
* @property PollItem $PollItem * @property PollItem $PollItem
* @property string $Url * @property string $Url
*/ */
class Vote extends PropertiesBase{ class PollVote extends PropertiesBase{
public $VoteId;
public $UserId; public $UserId;
protected $_User = null; protected $_User = null;
public $Created; public $Created;
@ -63,10 +62,10 @@ class Vote extends PropertiesBase{
else{ else{
// Do we already have a vote for this poll, from this user? // Do we already have a vote for this poll, from this user?
if(Db::QueryInt(' if(Db::QueryInt('
SELECT count(*) from Votes v inner join SELECT count(*) from PollVotes pv inner join
(select PollItemId from PollItems pi inner join Polls p on pi.PollId = p.PollId) x (select PollItemId from PollItems pi inner join Polls p using (PollId)) x
on v.PollItemId = x.PollItemId where v.UserId = ?', [$this->UserId]) > 0){ using (PollItemId) where pv.UserId = ?', [$this->UserId]) > 0){
$error->Add(new Exceptions\VoteExistsException()); $error->Add(new Exceptions\PollVoteExistsException());
} }
} }
} }
@ -95,23 +94,21 @@ class Vote extends PropertiesBase{
$this->Validate(); $this->Validate();
$this->Created = new DateTime(); $this->Created = new DateTime();
Db::Query('INSERT into Votes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]); Db::Query('INSERT into PollVotes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]);
$this->VoteId = Db::GetLastInsertedId();
} }
public static function Get(?string $pollUrlName, ?int $userId): Vote{ public static function Get(?string $pollUrlName, ?int $userId): PollVote{
if($pollUrlName === null || $userId === null){ if($pollUrlName === null || $userId === null){
throw new Exceptions\InvalidVoteException(); throw new Exceptions\InvalidPollVoteException();
} }
$result = Db::Query('SELECT v.* from Votes v inner join $result = Db::Query('SELECT pv.* from PollVotes pv inner join
(select pi.PollItemId from PollItems pi inner join Polls p on pi.PollId = p.PollID (select pi.PollItemId from PollItems pi inner join Polls p using (PollId)
where p.UrlName = ? where p.UrlName = ?
) x on v.PollItemId = x.PollItemId where v.UserId = ?', [$pollUrlName, $userId], 'Vote'); ) x using (PollItemId) where pv.UserId = ?', [$pollUrlName, $userId], 'PollVote');
if(sizeof($result) == 0){ if(sizeof($result) == 0){
throw new Exceptions\InvalidVoteException(); throw new Exceptions\InvalidPollVoteException();
} }
return $result[0]; return $result[0];

View file

@ -238,6 +238,16 @@ main > article{
align-items: center; align-items: center;
} }
main > section.narrow > *{
margin-left: auto;
margin-right: auto;
}
main > section.narrow > h1{
max-width: none;
}
main > section.narrow > *,
main > article > *{ main > article > *{
width: 100%; width: 100%;
max-width: 40rem; max-width: 40rem;
@ -625,6 +635,10 @@ input[type="checkbox"]:focus{
outline: none; outline: none;
} }
h1 + .message{
margin-top: 0;
}
.message{ .message{
border: 2px solid rgba(0, 0, 0, .25); border: 2px solid rgba(0, 0, 0, .25);
border-radius: .25rem; border-radius: .25rem;

View file

@ -14,7 +14,7 @@ catch(Exceptions\SeException $ex){
?><?= Template::Header(['title' => $poll->Name, 'highlight' => '', 'description' => $poll->Description]) ?> ?><?= Template::Header(['title' => $poll->Name, 'highlight' => '', 'description' => $poll->Description]) ?>
<main> <main>
<section> <section class="narrow">
<h1><?= Formatter::ToPlainText($poll->Name) ?></h1> <h1><?= Formatter::ToPlainText($poll->Name) ?></h1>
<p><?= $poll->Description ?></p> <p><?= $poll->Description ?></p>
<? if($poll->IsActive()){ ?> <? if($poll->IsActive()){ ?>

View file

@ -5,12 +5,12 @@ use function Safe\session_unset;
session_start(); session_start();
$vote = new Vote(); $vote = new PollVote();
try{ try{
$vote = Vote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid')); $vote = PollVote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid'));
if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->VoteId){ if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->UserId){
http_response_code(201); http_response_code(201);
session_unset(); session_unset();
} }
@ -21,7 +21,7 @@ catch(Exceptions\SeException $ex){
?><?= Template::Header(['title' => 'Thank you for voting!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?> ?><?= Template::Header(['title' => 'Thank you for voting!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?>
<main> <main>
<section> <section class="narrow">
<h1>Thank you for voting!</h1> <h1>Thank you for voting!</h1>
<p class="center-notice">Your vote in the <a href="<?= $vote->PollItem->Poll->Url ?>"><?= Formatter::ToPlainText($vote->PollItem->Poll->Name) ?> poll</a> has been recorded.</p> <p class="center-notice">Your vote in the <a href="<?= $vote->PollItem->Poll->Url ?>"><?= Formatter::ToPlainText($vote->PollItem->Poll->Name) ?> poll</a> has been recorded.</p>
<p class="button-row narrow"><a class="button" href="<?= $vote->PollItem->Poll->Url ?>/votes"> view results</a></p> <p class="button-row narrow"><a class="button" href="<?= $vote->PollItem->Poll->Url ?>/votes"> view results</a></p>

View file

@ -12,7 +12,7 @@ catch(Exceptions\SeException $ex){
?><?= Template::Header(['title' => 'Results for the ' . $poll->Name . ' poll', 'highlight' => '', 'description' => 'The voting results for the ' . $poll->Name . ' poll.']) ?> ?><?= Template::Header(['title' => 'Results for the ' . $poll->Name . ' poll', 'highlight' => '', 'description' => 'The voting results for the ' . $poll->Name . ' poll.']) ?>
<main> <main>
<section> <section class="narrow">
<h1>Results for the <?= Formatter::ToPlainText($poll->Name) ?> Poll</h1> <h1>Results for the <?= Formatter::ToPlainText($poll->Name) ?> Poll</h1>
<p class="center-notice">Total votes: <?= number_format($poll->VoteCount) ?></p> <p class="center-notice">Total votes: <?= number_format($poll->VoteCount) ?></p>
<? if($poll->IsActive()){ ?> <? if($poll->IsActive()){ ?>

View file

@ -5,7 +5,7 @@ use function Safe\session_unset;
session_start(); session_start();
$vote = $_SESSION['vote'] ?? new Vote(); $vote = $_SESSION['vote'] ?? new PollVote();
$exception = $_SESSION['exception'] ?? null; $exception = $_SESSION['exception'] ?? null;
$poll = new Poll(); $poll = new Poll();
@ -24,7 +24,7 @@ if($exception){
?><?= Template::Header(['title' => $poll->Name . ' - Vote Now', 'highlight' => '', 'description' => 'Vote in the ' . $poll->Name . ' poll']) ?> ?><?= Template::Header(['title' => $poll->Name . ' - Vote Now', 'highlight' => '', 'description' => 'Vote in the ' . $poll->Name . ' poll']) ?>
<main> <main>
<section> <section class="narrow">
<h1>Vote in the <?= Formatter::ToPlainText($poll->Name) ?> Poll</h1> <h1>Vote in the <?= Formatter::ToPlainText($poll->Name) ?> Poll</h1>
<?= Template::Error(['exception' => $exception]) ?> <?= Template::Error(['exception' => $exception]) ?>
<form method="post" action="<?= Formatter::ToPlainText($poll->Url) ?>/votes"> <form method="post" action="<?= Formatter::ToPlainText($poll->Url) ?>/votes">

View file

@ -13,7 +13,7 @@ session_start();
$requestType = HttpInput::RequestType(); $requestType = HttpInput::RequestType();
$vote = new Vote(); $vote = new PollVote();
try{ try{
$error = new Exceptions\ValidationException(); $error = new Exceptions\ValidationException();
@ -25,7 +25,7 @@ try{
session_unset(); session_unset();
if($requestType == WEB){ if($requestType == WEB){
$_SESSION['vote-created'] = $vote->VoteId; $_SESSION['vote-created'] = $vote->UserId;
http_response_code(303); http_response_code(303);
header('Location: ' . $vote->Url); header('Location: ' . $vote->Url);
} }