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,
`PollItemId` int(11) unsigned NOT NULL,
`Created` datetime NOT NULL,

View file

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

View file

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

View file

@ -36,7 +36,7 @@ class Poll extends PropertiesBase{
protected function GetVoteCount(): int{
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;

View file

@ -19,7 +19,7 @@ class PollItem extends PropertiesBase{
protected function GetVoteCount(): int{
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;

View file

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

View file

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

View file

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

View file

@ -5,12 +5,12 @@ use function Safe\session_unset;
session_start();
$vote = new Vote();
$vote = new PollVote();
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);
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!']) ?>
<main>
<section>
<section class="narrow">
<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="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.']) ?>
<main>
<section>
<section class="narrow">
<h1>Results for the <?= Formatter::ToPlainText($poll->Name) ?> Poll</h1>
<p class="center-notice">Total votes: <?= number_format($poll->VoteCount) ?></p>
<? if($poll->IsActive()){ ?>

View file

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

View file

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