Delete a Poll

This commit is contained in:
Tagadda 2018-08-13 15:20:06 +02:00
parent d5ab16135d
commit 7a29c6265b
4 changed files with 42 additions and 4 deletions

View File

@ -3,14 +3,19 @@ require __DIR__ . "/vendor/autoload.php";
require __DIR__ . "/models/Poll.php";
require __DIR__ . "/config/app.php";
function format_poll($poll)
function format_poll($poll, $with_delete_token = false)
{
return [
$array = [
"id" => $poll->id,
"title" => $poll->title,
"creation_date" => $poll->creation_date,
"options" => $poll->options,
];
if ($with_delete_token === true)
$array['delete_token'] = $poll->delete_token;
return $array;
}
Flight::route("POST /polls", function () {
@ -20,7 +25,7 @@ Flight::route("POST /polls", function () {
$request_json = $request->data;
$poll = Poll::create_poll($request_json);
if ($poll)
Flight::json(format_poll($poll), 201);
Flight::json(format_poll($poll, true), 201);
else
Flight::halt(403, "<h1>403 Forbidden</h1><h3>Invalid data.</h3>");
}
@ -86,6 +91,26 @@ Flight::route("POST /polls/@id:[a-fA-F0-9]+/vote", function ($id) {
Flight::notFound();
});
Flight::route("GET|DELETE /polls/@id:[a-fA-F0-9]+/@token:[a-fA-F0-9]+", function ($id, $token) {
$poll = Poll::load_poll($id);
if ($poll)
{
if ($poll->delete_token !== $token)
Flight::halt(401, "<h1>401 Unauthorized</h1><h3>Invalid token.</h3>");
$poll->delete();
if (Flight::request()->type === "application/json")
Flight::json(format_poll($poll), 204);
else
{
Flight::redirect('/');
}
}
else
Flight::notFound();
});
Flight::route("/", function () {
global $VERLAINE;
Flight::render("home", ["app_url" => $VERLAINE["app_url"]], "body_content");

View File

@ -22,6 +22,7 @@ class Poll
];
}
$poll->gen_new_id();
$poll->delete_token = bin2hex(openssl_random_pseudo_bytes(16));
$poll->save();
return $poll;
}
@ -46,6 +47,7 @@ class Poll
$poll->title = $saved_poll_data->title;
$poll->creation_date = $saved_poll_data->creation_date;
$poll->options = $saved_poll_data->options;
$poll->delete_token = $saved_poll_data->delete_token;
dba_close($db);
return $poll;
@ -61,8 +63,9 @@ class Poll
public $title;
public $creation_date;
public $options = [];
public $delete_token;
public function gen_new_id()
private function gen_new_id()
{
$db = dba_open(SAVE_PATH . "/polls.db", "rd");
@ -97,7 +100,15 @@ class Poll
"title" => $this->title,
"creation_date" => $this->creation_date,
"options" => $this->options,
"delete_token" => $this->delete_token
]), $db);
dba_close($db);
}
public function delete()
{
$db = dba_open(SAVE_PATH . "/polls.db", "wd");
dba_delete($this->id, $db);
dba_close($db);
}
}

View File

@ -48,6 +48,7 @@ document.addEventListener("DOMContentLoaded", () => {
let result_el = document.getElementById("result");
result_el.innerHTML = result_el.innerHTML.replace(/:poll_title/g, json.title);
result_el.innerHTML = result_el.innerHTML.replace(/:poll_url/g, `/polls/${json.id}`);
result_el.innerHTML = result_el.innerHTML.replace(/:delete_url/g, `/polls/${json.id}/${json.delete_token}`);
result_el.removeAttribute("hidden");
});
});

View File

@ -14,6 +14,7 @@
<div id="result" hidden>
<p>Your poll <strong>:poll_title</strong> is ready!</p>
<input type="text" name="pollurl" value="<?= $app_url ?>:poll_url" />
<input type="text" name="deleteurl" value="<?= $app_url ?>:delete_url" />
<a class="button" href=":poll_url">See the poll!</a>
</div>
</main>