From 477db758961a1fc5c60e776c03c1e40e0912433d Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Sat, 1 May 2021 15:51:02 -0600 Subject: [PATCH] Add LikeService --- app/Services/LikeService.php | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 app/Services/LikeService.php diff --git a/app/Services/LikeService.php b/app/Services/LikeService.php new file mode 100644 index 000000000..8ed2bfc3a --- /dev/null +++ b/app/Services/LikeService.php @@ -0,0 +1,65 @@ +whereProfileId($profile_id) + ->latest() + ->get() + ->each(function($like) use ($profile_id) { + self::set($profile_id, $like->status_id); + }); + } + + public static function liked($profileId, $statusId) + { + $key = self::CACHE_KEY . $profileId; + return (bool) Redis::zrank($key, $statusId); + } + + public static function likedBy($status) + { + if(!$status->likes_count) { + return [ + 'username' => null, + 'others' => false + ]; + } + $id = Like::whereStatusId($status->id)->first()->profile_id; + return [ + 'username' => ProfileService::get($id)['username'], + 'others' => $status->likes_count >= 5, + ]; + } +}