update rate contest photos

This commit is contained in:
themohooks 2025-02-16 06:18:43 +03:00
parent 0581edcc3d
commit 4ee7f4b79b
2 changed files with 58 additions and 14 deletions

View file

@ -3,7 +3,7 @@
namespace App\Controllers\Api\Images;
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
use App\Models\{User, Vote};
use App\Models\{User, Vote, VoteContest};
class Rate
{
@ -14,28 +14,29 @@ class Rate
$photoId = $_GET['pid'];
$voteType = (int) $_GET['vote'];
$contest = (isset($_GET['action']) && $_GET['action'] === 'vote-konk') ? 1 : 0;
$contestId = $_GET['cid'];
if ($contest === 1) {
if (Vote::photoContest($userId, $photoId) === -1) {
if (VoteContest::photo($userId, $photoId, $contestId) === -1) {
DB::query(
'INSERT INTO photos_rates (id, user_id, photo_id, type, contest) VALUES (NULL, :id, :pid, :type, 1)',
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType]
'INSERT INTO photos_rates_contest (id, user_id, photo_id, type, contest_id) VALUES (NULL, :id, :pid, :type, :cid)',
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId]
);
if (Vote::photoContest($userId, $photoId) != $voteType) {
if (VoteContest::photo($userId, $photoId, $contestId) != $voteType) {
DB::query(
'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest=1',
[':id' => $userId, ':pid' => $photoId, ':type' => Vote::photo($userId, $photoId)]
'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND type=:type AND contest_id=:cid',
[':id' => $userId, ':pid' => $photoId, ':type' => VoteContest::photo($userId, $photoId, $contestId), ':cid'=>$contestId]
);
}
} elseif (Vote::photoContest($userId, $photoId) === $voteType) {
} elseif (VoteContest::photo($userId, $photoId, $contestId) === $voteType) {
DB::query(
'DELETE FROM photos_rates WHERE user_id=:id AND photo_id=:pid AND contest=1',
[':id' => $userId, ':pid' => $photoId]
'DELETE FROM photos_rates_contest WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid',
[':id' => $userId, ':pid' => $photoId, ':cid'=>$contestId]
);
} else {
DB::query(
'UPDATE photos_rates SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest=1',
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType]
'UPDATE photos_rates_contest SET type=:type WHERE user_id=:id AND photo_id=:pid AND contest_id=:cid',
[':id' => $userId, ':pid' => $photoId, ':type' => $voteType, ':cid'=>$contestId]
);
}
} else {
@ -77,7 +78,13 @@ class Rate
}
$currentVote = Vote::photo($userId, $photoId);
$contCurrentVote = Vote::photoContest($userId, $photoId);
$contCurrentVote = VoteContest::photo($userId, $photoId, $contestId);
if ($contest === 0) {
$count = Vote::count($photoId);
} else {
$count = VoteContest::count($photoId, $contestId);
}
$result = [
'buttons' => [
'negbtn' => $currentVote === 0,
@ -86,7 +93,7 @@ class Rate
'posbtn_contest' => $contCurrentVote === 1,
],
'errors' => '',
'rating' => Vote::count($photoId),
'rating' => $count,
'votes' => [
1 => $formattedVotesPos,
0 => $formattedVotesNeg

View file

@ -0,0 +1,37 @@
<?php
namespace App\Models;
use App\Services\{DB, GenerateRandomStr};
class VoteContest
{
public static function photo($user_id, $pid, $cid)
{
$result = DB::query('SELECT type FROM photos_rates_contest WHERE user_id=:uid AND photo_id=:pid AND contest_id=:id', array(':uid' => $user_id, ':pid' => $pid, ':id'=>$cid));
if (!empty($result)) {
$type = $result[0]['type'];
if ($type < 0) {
$type = -1;
}
return $type;
} else {
return -1;
}
}
public static function count($pid, $cid) {
$result = DB::query('SELECT * FROM photos_rates_contest WHERE photo_id=:pid AND contest_id=:id', array(':pid' => $pid, ':id'=>$cid));
$votes = 0;
foreach ($result as $r) {
if ($r['type'] === 1) {
$votes++;
} else {
$votes--;
}
}
return $votes;
}
}
?>