You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.5 KiB
88 lines
2.5 KiB
4 weeks ago
|
<?php
|
||
|
|
||
|
use App\Enumerators\SessionElement;
|
||
|
use App\Wrappers\DatabaseInteractions;
|
||
|
use App\Wrappers\SQLQueryBuilderWrapper;
|
||
|
|
||
|
require_once __DIR__ . "/../../../vendor/autoload.php";
|
||
|
|
||
|
if(!$_SESSION[SessionElement::IS_LOGGED_IN->value])
|
||
|
{
|
||
|
die();
|
||
|
}
|
||
|
|
||
|
$ratingValue = 0;
|
||
|
if($_GET['type'] == "LIKE")
|
||
|
$ratingValue = 1;
|
||
|
elseif($_GET['type'] == "DISLIKE")
|
||
|
$ratingValue = -1;
|
||
|
else
|
||
|
die();
|
||
|
|
||
|
$db = new DatabaseInteractions();
|
||
|
|
||
|
$tuneDetails = $db->RunOneSelect(
|
||
|
queryBuilder: SQLQueryBuilderWrapper::SELECT_ONE(
|
||
|
table: 'Tunes',
|
||
|
id: $_GET['tune-id']
|
||
|
)
|
||
|
);
|
||
|
|
||
|
$existingRating = $db->RunSelect(
|
||
|
queryBuilder: SQLQueryBuilderWrapper::SELECT(
|
||
|
table: 'TuneRatings',
|
||
|
)
|
||
|
->where(cond: 'CreatedBy=:__user_id__')
|
||
|
->where(cond: 'TuneID=:__tune_id__')
|
||
|
->bindValue(name: '__user_id__', value: $_SESSION[SessionElement::USER_ID->value])
|
||
|
->bindValue(name: '__tune_id__', value: $_GET['tune-id'])
|
||
|
);
|
||
|
|
||
|
if(sizeof($existingRating) == 0)
|
||
|
{
|
||
|
$db->RunInsert(
|
||
|
queryBuilder: SQLQueryBuilderWrapper::INSERT(
|
||
|
table: 'TuneRatings'
|
||
|
)
|
||
|
->set(col: 'CreatedBy', value: ':__user_id__')
|
||
|
->set(col: 'TuneID', value: ':__tune_id__')
|
||
|
->set(col: 'Rating', value: ':__rating__')
|
||
|
->bindValue(name: '__user_id__', value: $_SESSION[SessionElement::USER_ID->value])
|
||
|
->bindValue(name: '__tune_id__', value: $_GET['tune-id'])
|
||
|
->bindValue(name: '__rating__', value: $ratingValue)
|
||
|
);
|
||
|
}
|
||
|
elseif(sizeof($existingRating) == 1)
|
||
|
{
|
||
|
$db->RunUpdate(
|
||
|
queryBuilder: SQLQueryBuilderWrapper::UPDATE(
|
||
|
table: 'TuneRatings'
|
||
|
)
|
||
|
->set(col: 'Rating', value: ':__rating__')
|
||
|
->where(cond: 'CreatedBy=:__user_id__')
|
||
|
->where(cond: 'TuneID=:__tune_id__')
|
||
|
->bindValue(name: '__user_id__', value: $_SESSION[SessionElement::USER_ID->value])
|
||
|
->bindValue(name: '__tune_id__', value: $_GET['tune-id'])
|
||
|
->bindValue(name: '__rating__', value: $ratingValue)
|
||
|
);
|
||
|
}
|
||
|
|
||
|
$tuneRatings = $db->RunSelect(
|
||
|
queryBuilder: SQLQueryBuilderWrapper::SELECT(
|
||
|
table: 'TuneRatings',
|
||
|
)
|
||
|
->cols(cols: [
|
||
|
'SUM(CASE WHEN T.Rating = 1 THEN 1 ELSE 0 END) AS Likes',
|
||
|
'SUM(CASE WHEN T.Rating = -1 THEN 1 ELSE 0 END) AS Dislikes',
|
||
|
])
|
||
|
->where(cond: 'TuneID=:__tune_id__')
|
||
|
->bindValue(name: '__tune_id__', value: $_GET['tune-id'])
|
||
|
);
|
||
|
|
||
|
echo json_encode([
|
||
|
"LikeCount" => $tuneRatings[0]["Likes"],
|
||
|
"DislikeCount" => $tuneRatings[0]["Dislikes"],
|
||
|
]);
|
||
|
|
||
|
die();
|