10 changed files with 199 additions and 5 deletions
@ -0,0 +1,87 @@ |
|||||
|
<?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(); |
@ -0,0 +1,29 @@ |
|||||
|
|
||||
|
|
||||
|
.RatingButtons { |
||||
|
display: flex; |
||||
|
gap: 1rem; |
||||
|
justify-content: center; |
||||
|
margin-top: 1rem; |
||||
|
} |
||||
|
#RatingContainer button { |
||||
|
background: none; |
||||
|
border: none; |
||||
|
cursor: pointer; |
||||
|
font-size: 2rem; |
||||
|
transition: transform 0.2s, color 0.2s; |
||||
|
} |
||||
|
#RatingContainer button:hover { |
||||
|
transform: scale(1.2); |
||||
|
} |
||||
|
#ThumbUp { |
||||
|
color: green; |
||||
|
} |
||||
|
#ThumbDown { |
||||
|
color: red; |
||||
|
} |
||||
|
.Counts { |
||||
|
margin-top: 1rem; |
||||
|
font-size: 1.2rem; |
||||
|
} |
||||
|
|
Loading…
Reference in new issue