Test meme voting

This commit is contained in:
Bruno Rybársky 2024-04-27 10:43:58 +02:00
parent 9fe7eb14e9
commit f251d18bcb

@ -28,8 +28,8 @@ function renderMeme(int $id, int $authorId, string $title, string $textContent,
$meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', (isModerator() || $_SESSION['ID'] == $authorId) ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_DELETE_BUTTON__', (isModerator() || $_SESSION['ID'] == $authorId) ? "<button onclick=\"deleteMeme($id);\"><i class='ri-delete-bin-line'></i></button>" : '', $meme_out);
$meme_votes = calculateNetVotes($id); $meme_votes = calculateNetVotes($id);
$meme_upvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, true);\"> <i class=\"ri-arrow-up-line\"></i></button>" : ''; $meme_upvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 1);\"> <i class=\"ri-arrow-up-line\"></i></button>" : '';
$meme_downvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, false);\"> <i class=\"ri-arrow-down-line\"></i></button>" : ''; $meme_downvote = isLoggedIn() ? "<button onclick=\"voteMeme($id, 0);\"> <i class=\"ri-arrow-down-line\"></i></button>" : '';
$meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', $meme_votes, $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_VOTES_NUMBER__', $meme_votes, $meme_out);
$meme_out = str_replace('__TEMPLATE_MEME_UPVOTE__', $meme_upvote, $meme_out); $meme_out = str_replace('__TEMPLATE_MEME_UPVOTE__', $meme_upvote, $meme_out);
@ -99,13 +99,15 @@ function deleteMeme(int $memeID): array
return $out; return $out;
} }
function voteMeme(int $memeID, bool $isUpvote): array function voteMeme(int $memeID, int $isUpvote): array
{ {
global $mysqli; global $mysqli;
$out = ["Status" => "Fail"]; $out = ["Status" => "Fail"];
$vote = $isUpvote ? 1 : 0; if ($isUpvote != 1) {
$isUpvote = 0;
}
$memeVoteConn = $mysqli->prepare('INSERT INTO MemeVotes (MemeID, UserID, isUpvote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE isUpvote = VALUES(isUpvote)'); $memeVoteConn = $mysqli->prepare('INSERT INTO MemeVotes (MemeID, UserID, isUpvote) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE isUpvote = VALUES(isUpvote)');
$memeVoteConn->bind_param('iii', $memeID, $_SESSION['ID'], $vote); $memeVoteConn->bind_param('iii', $memeID, $_SESSION['ID'], $isUpvote);
$memeVoteConn->execute(); $memeVoteConn->execute();
if ($memeVoteConn->affected_rows > 0) { if ($memeVoteConn->affected_rows > 0) {
$out['Status'] = 'Success'; $out['Status'] = 'Success';