feat: add "mark as broken"

This commit is contained in:
Tomáš Mládek 2022-11-25 16:52:15 +01:00
parent 198fec613a
commit fba9fc8d78
2 changed files with 66 additions and 32 deletions

View file

@ -10,45 +10,54 @@ if (!$exists) {
url TEXT NOT NULL,
audio BOOLEAN NOT NULL CHECK (audio IN (0, 1)),
video BOOLEAN NOT NULL CHECK (video IN (0, 1)),
ts NUMBER
ts NUMBER,
works BOOLEAN NOT NULL CHECK (works IN (0, 1))
);
EOD);
}
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$data = json_decode(file_get_contents('php://input'));
switch ($_SERVER['REQUEST_METHOD']) {
case "POST":
$data = json_decode(file_get_contents('php://input'));
$url = $data->url;
$audio = $data->audio;
$video = $data->video;
$url = $data->url;
$audio = $data->audio;
$video = $data->video;
if ($url !== null && $audio !== null && $video !== null) {
$stmt = $db->prepare('INSERT INTO links (url, audio, video, ts) VALUES (:url, :audio, :video, :ts);');
$stmt->bindValue(':url', $url);
$stmt->bindValue(':audio', $audio);
$stmt->bindValue(':video', $video);
$stmt->bindValue(':ts', time());
$result = $stmt->execute();
if (!$result) {
http_response_code(500);
if ($url !== null && $audio !== null && $video !== null) {
$stmt = $db->prepare('INSERT INTO links (url, audio, video, ts, works) VALUES (:url, :audio, :video, :ts, TRUE);');
$stmt->bindValue(':url', $url);
$stmt->bindValue(':audio', $audio);
$stmt->bindValue(':video', $video);
$stmt->bindValue(':ts', time());
$result = $stmt->execute();
if (!$result) {
http_response_code(500);
die();
}
$result = $db->querySingle("SELECT last_insert_rowid()");
print_r($result);
} else {
http_response_code(400);
die();
}
$result = $db->querySingle("SELECT last_insert_rowid()");
print_r($result);
} else {
http_response_code(400);
die();
}
} else {
$result = [];
break;
case "DELETE":
$stmt = $db->prepare('UPDATE links SET works=0 WHERE id=:id');
$stmt->bindValue(':id', $_GET["id"], SQLITE3_INTEGER);
$stmt->execute();
break;
default:
$result = [];
$db_result = $db->query("SELECT * FROM links;");
while ($row = $db_result->fetchArray(SQLITE3_ASSOC)) {
$row["audio"] = $row["audio"] == 1;
$row["video"] = $row["video"] == 1;
array_push($result, $row);
}
$db_result = $db->query("SELECT * FROM links WHERE works != 0;");
while ($row = $db_result->fetchArray(SQLITE3_ASSOC)) {
$row["audio"] = $row["audio"] == 1;
$row["video"] = $row["video"] == 1;
array_push($result, $row);
}
header('Content-type: application/json; charset=utf-8');
echo json_encode($result);
header('Content-type: application/json; charset=utf-8');
echo json_encode($result);
break;
}

View file

@ -1,7 +1,8 @@
<script>
import { Link } from "svelte-navigator";
import { Link, useNavigate } from "svelte-navigator";
import { onMount } from "svelte";
import Spinner from "./components/Spinner.svelte";
const navigate = useNavigate();
export let audioId;
export let videoId;
@ -115,6 +116,18 @@
videoPlayer.mute();
audioPlayer.playVideo();
}
async function onBroken(id) {
const result = await fetch(`grid.php?id=${id}`, {
method: "DELETE",
});
if (result.ok) {
alert(`ID ${id} marked as broken, thanks!`);
navigate("/");
} else {
alert(`ERR: ${result.status} ${result.statusText}`);
}
}
</script>
<main>
@ -129,9 +142,15 @@
<h2 title={videoUrl}>Video</h2>
{#if !videoReady}<Spinner />{/if}
<div id="videoPlayer" bind:this={videoEl} />
<a class="broken" href="javascript:;" on:click={() => onBroken(videoId)}
>broken?</a
>
<h2 title={audioUrl}>Audio</h2>
{#if !audioReady}<Spinner />{/if}
<div id="audioPlayer" bind:this={audioEl} />
<a class="broken" href="javascript:;" on:click={() => onBroken(audioId)}
>broken?</a
>
</div>
<Link class="link" to="/">BACK</Link>
@ -166,6 +185,12 @@
height: 128px;
}
a {
color: red;
opacity: 0.5;
font-size: 80%;
}
.disabled {
color: gray;
cursor: not-allowed;