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.
		
		
		
		
		
			
		
			
				
					
					
						
							91 lines
						
					
					
						
							2.8 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							91 lines
						
					
					
						
							2.8 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								use App\Enumerators\SessionElement;
							 | 
						|
								use App\Wrappers\DatabaseInteractions;
							 | 
						|
								use App\Wrappers\SessionWrapper;
							 | 
						|
								use App\Wrappers\SQLQueryBuilderWrapper;
							 | 
						|
								
							 | 
						|
								require_once __DIR__ . "/../../../vendor/autoload.php";
							 | 
						|
								
							 | 
						|
								if(!SessionWrapper::Get(target: SessionElement::IS_LOGGED_IN))
							 | 
						|
								{
							 | 
						|
								    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: SessionWrapper::Get(target: SessionElement::USER_ID))
							 | 
						|
								        ->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: SessionWrapper::Get(target: SessionElement::USER_ID))
							 | 
						|
								            ->bindValue(name: '__tune_id__', value: $_GET['tune-id'])
							 | 
						|
								            ->bindValue(name: '__rating__', value: $ratingValue)
							 | 
						|
								    );
							 | 
						|
								}
							 | 
						|
								elseif(sizeof($existingRating) == 1)
							 | 
						|
								{
							 | 
						|
								    if($existingRating[0]['Rating'] == $ratingValue)
							 | 
						|
								        $ratingValue = 0;
							 | 
						|
								    $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: SessionWrapper::Get(target: SessionElement::USER_ID))
							 | 
						|
								            ->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([
							 | 
						|
								    "NewVoteValue" => intval($ratingValue),
							 | 
						|
								    "LikeCount" => intval($tuneRatings[0]["Likes"]),
							 | 
						|
								    "DislikeCount" => intval($tuneRatings[0]["Dislikes"]),
							 | 
						|
								]);
							 | 
						|
								
							 | 
						|
								die();
							 | 
						|
								
							 |