-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Helpers; | ||
|
||
use Illuminate\Support\Facades\DB; | ||
|
||
class CollectionLikeChecker { | ||
protected $collectionType; | ||
protected $collection; | ||
protected $userID; | ||
protected $queryData; | ||
|
||
/** | ||
* Instantiates a CollectionLikeChecker and fetches the data. | ||
* | ||
* @param $userID | ||
* @param $collection | ||
* @return CollectionLikeChecker | ||
*/ | ||
static function retrieve($userID, $collection) { | ||
// Get the type of collection | ||
$collectionType = $collection->getQueueableClass(); | ||
|
||
// Create the instance | ||
$instance = new CollectionLikeChecker(); | ||
$instance->collectionType = $collectionType; | ||
$instance->collection = $collection; | ||
$instance->userID = $userID; | ||
|
||
// Fetch the data for the instance | ||
$instance->fetchData(); | ||
|
||
// Return the instance | ||
return $instance; | ||
} | ||
|
||
/** | ||
* Returns an array of IDs of the items in the collection. | ||
* | ||
* @return array | ||
*/ | ||
protected function getCollectionIDs() { | ||
$IDs = []; | ||
|
||
foreach($this->collection as $item) | ||
$IDs[] = $item->id; | ||
|
||
return $IDs; | ||
} | ||
|
||
/** | ||
* Fetches the data by making a query and stores it in the instance. | ||
*/ | ||
protected function fetchData() { | ||
$this->queryData = DB::table('love_likes') | ||
->where('user_id', $this->userID) | ||
->where('likeable_type', $this->collectionType) | ||
->whereIn('likeable_id', $this->getCollectionIDs()) | ||
->get(); | ||
} | ||
|
||
/** | ||
* Checks if an item has been liked. | ||
* | ||
* @param $item | ||
* @return bool | ||
*/ | ||
function hasLiked($item) { | ||
foreach($this->queryData as $queryItem) | ||
if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'LIKE') | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Checks if an item has been disliked. | ||
* | ||
* @param $item | ||
* @return bool | ||
*/ | ||
function hasDisliked($item) { | ||
foreach($this->queryData as $queryItem) | ||
if($queryItem->likeable_id == $item->id && $queryItem->type_id == 'DISLIKE') | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
/** | ||
* Returns the current like action for an item. | ||
* -1 = disliked | ||
* 0 = neutral | ||
* 1 = liked | ||
* | ||
* @param $item | ||
* @return int | ||
*/ | ||
function getCurrentLikeAction($item) { | ||
if($this->hasLiked($item)) | ||
return 1; | ||
else if($this->hasDisliked($item)) | ||
return -1; | ||
else | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class ForumReplyResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'posted_at' => $this->created_at->format('Y-m-d H:i:s'), | ||
'poster' => [ | ||
'id' => $this->user->id, | ||
'username' => $this->user->username, | ||
'avatar' => $this->user->getAvatarURL() | ||
], | ||
'score' => $this->likesDiffDislikesCount, | ||
'content' => $this->content | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters