Tweak patrons poll REST structure

This commit is contained in:
Alex Cabal 2022-07-04 12:46:48 -05:00
parent 011cd747f1
commit 497f749523
10 changed files with 73 additions and 37 deletions

View file

@ -258,14 +258,15 @@ Define webroot /standardebooks.org/web
# Newsletter # Newsletter
RewriteRule ^/newsletter$ /newsletter/subscriptions/new.php [L] RewriteRule ^/newsletter$ /newsletter/subscriptions/new.php [L]
RewriteRule ^/newsletter/subscriptions/([^/\.]+?)$ /newsletter/subscriptions/get.php?uuid=$1 [L] RewriteRule ^/newsletter/subscriptions/([^/\.]+?)$ /newsletter/subscriptions/get.php?uuid=$1 [L]
RewriteRule ^/newsletter/subscriptions/([^/\.]+?)/(confirm|delete|success)$ /newsletter/subscriptions/$2.php?uuid=$1 [L] RewriteRule ^/newsletter/subscriptions/([^/\.]+?)/(confirm|delete)$ /newsletter/subscriptions/$2.php?uuid=$1 [L]
# Polls # Polls
RewriteRule ^/patrons-circle/polls/([^/\.]+)$ /patrons-circle/polls/get.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)$ /patrons-circle/polls/get.php?pollurlname=$1 [L]
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/(new|success)$ /patrons-circle/polls/votes/$2.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/new$ /patrons-circle/polls/votes/new.php?pollurlname=$1 [L]
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/([0-9]+)$ /patrons-circle/polls/votes/get.php?pollurlname=$1&userid=$2 [L]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^get$/" RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^get$/"
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/index.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/index.php?pollurlname=$1 [L]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/" RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/"
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/post.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/post.php?pollurlname=$1 [L]

View file

@ -257,14 +257,15 @@ Define webroot /standardebooks.org/web
# Newsletter # Newsletter
RewriteRule ^/newsletter$ /newsletter/subscriptions/new.php [L] RewriteRule ^/newsletter$ /newsletter/subscriptions/new.php [L]
RewriteRule ^/newsletter/subscriptions/([^/\.]+?)$ /newsletter/subscriptions/get.php?uuid=$1 [L] RewriteRule ^/newsletter/subscriptions/([^/\.]+?)$ /newsletter/subscriptions/get.php?uuid=$1 [L]
RewriteRule ^/newsletter/subscriptions/([^/\.]+?)/(confirm|delete|success)$ /newsletter/subscriptions/$2.php?uuid=$1 [L] RewriteRule ^/newsletter/subscriptions/([^/\.]+?)/(confirm|delete)$ /newsletter/subscriptions/$2.php?uuid=$1 [L]
# Polls # Polls
RewriteRule ^/patrons-circle/polls/([^/\.]+)$ /patrons-circle/polls/get.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)$ /patrons-circle/polls/get.php?pollurlname=$1 [L]
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/(new|success)$ /patrons-circle/polls/votes/$2.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/new$ /patrons-circle/polls/votes/new.php?pollurlname=$1 [L]
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes/([0-9]+)$ /patrons-circle/polls/votes/get.php?pollurlname=$1&userid=$2 [L]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^get$/" RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^get$/"
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/index.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/index.php?pollurlname=$1 [L]
RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/" RewriteCond expr "tolower(%{REQUEST_METHOD}) =~ /^post$/"
RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/post.php?pollurlname=$1 [L] RewriteRule ^/patrons-circle/polls/([^/\.]+)/votes$ /patrons-circle/polls/votes/post.php?pollurlname=$1 [L]

View file

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

View file

@ -22,7 +22,7 @@ class Vote extends PropertiesBase{
protected function GetUrl(): string{ protected function GetUrl(): string{
if($this->_Url === null){ if($this->_Url === null){
$this->_Url = '/patrons-circle/polls/' . $this->PollItem->Poll->Url . '/votes/' . $this->UserId; $this->_Url = $this->PollItem->Poll->Url . '/votes/' . $this->UserId;
} }
return $this->_Url; return $this->_Url;
@ -96,5 +96,25 @@ 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 Votes (UserId, PollItemId, Created) values (?, ?, ?)', [$this->UserId, $this->PollItemId, $this->Created]);
$this->VoteId = Db::GetLastInsertedId();
}
public static function Get(?string $pollUrlName, ?int $userId): ?Vote{
if($pollUrlName === null || $userId === null){
vdd('nn');
return null;
}
$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
where p.UrlName = ?
) x on v.PollItemId = x.PollItemId where v.UserId = ?', [$pollUrlName, $userId], 'Vote');
if(sizeof($result) == 0){
throw new Exceptions\InvalidVoteException();
}
return $result[0];
} }
} }

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>
<article> <section>
<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()){ ?>
@ -33,6 +33,6 @@ catch(Exceptions\SeException $ex){
<p class="button-row narrow"><a href="<?= $poll->Url ?>/votes" class="button">View results</a></p> <p class="button-row narrow"><a href="<?= $poll->Url ?>/votes" class="button">View results</a></p>
<? } ?> <? } ?>
<? } ?> <? } ?>
</article> </section>
</main> </main>
<?= Template::Footer() ?> <?= Template::Footer() ?>

View file

@ -0,0 +1,28 @@
<?
require_once('Core.php');
session_start();
$vote = new Vote();
try{
$vote = Vote::Get(HttpInput::Str(GET, 'pollurlname'), HttpInput::Int(GET, 'userid'));
if(isset($_SESSION['vote-created']) && $_SESSION['vote-created'] == $vote->VoteId){
http_response_code(201);
session_unset();
}
}
catch(Exceptions\SeException $ex){
Template::Emit404();
}
?><?= Template::Header(['title' => 'Thank you for voting!', 'highlight' => '', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?>
<main>
<section>
<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>
</section>
</main>
<?= Template::Footer() ?>

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>
<article> <section>
<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()){ ?>
@ -39,6 +39,6 @@ catch(Exceptions\SeException $ex){
<? } ?> <? } ?>
</tbody> </tbody>
</table> </table>
</article> </section>
</main> </main>
<?= Template::Footer() ?> <?= Template::Footer() ?>

View file

@ -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>
<article> <section>
<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">
@ -51,6 +51,6 @@ if($exception){
</fieldset> </fieldset>
<button>Vote</button> <button>Vote</button>
</form> </form>
</article> </section>
</main> </main>
<?= Template::Footer() ?> <?= Template::Footer() ?>

View file

@ -25,8 +25,9 @@ try{
session_unset(); session_unset();
if($requestType == WEB){ if($requestType == WEB){
$_SESSION['vote-created'] = $vote->VoteId;
http_response_code(303); http_response_code(303);
header('Location: ' . $vote->PollItem->Poll->Url . '/votes/success'); header('Location: ' . $vote->Url);
} }
else{ else{
// Access via REST api; 201 CREATED with location // Access via REST api; 201 CREATED with location

View file

@ -1,21 +0,0 @@
<?
require_once('Core.php');
$poll = new Poll();
try{
$poll = Poll::GetByUrlName(HttpInput::Str(GET, 'pollurlname', false));
}
catch(Exceptions\SeException $ex){
Template::Emit404();
}
?><?= Template::Header(['title' => 'Thank you for voting!', 'highlight' => 'newsletter', 'description' => 'Thank you for voting in a Standard Ebooks poll!']) ?>
<main>
<article>
<h1>Thank you for voting!</h1>
<p class="center-notice">Your vote in the <?= Formatter::ToPlainText($poll->Name) ?> poll has been recorded.</p>
<p class="button-row narrow"><a class="button" href="<?= $poll->Url ?>/votes"> view results</a></p>
</article>
</main>
<?= Template::Footer() ?>