/** * @api {delete} /todo/:toDoId/likes Unlike A To Do * @apiGroup Likes * @apiDescription Unlike a users intention to complete a task or sticker. * @apiUse RequiresAuthentication * * @param ToDo $toDo * * @return \Illuminate\Http\Response */ public function destroy(ToDo $toDo) { $user = $this->requireAuthentication(); $toDoLikeManager = $toDo->getLikeManager(); $success = !!$toDoLikeManager->unlike($user); return $this->response(['success' => $success, 'toDo' => $toDo->fresh()]); }
/** * Handle the event. * * @param NewLike $event * * @return void */ public function handle(NewLike $event) { $nm = new NotificationManager(); if ($event->like->commentId && ($comment = $event->like->comment)) { $notif = $nm->createNotification($comment->userId, Notification::TYPE_LIKED_COMMENT); $notif->commentId = $comment->id; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } if ($event->like->postId && ($post = $event->like->post)) { $notif = $nm->createNotification($post->userId, Notification::TYPE_LIKED_POST); $notif->postId = $post->id; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } if ($event->like->earntId) { if ($earn = DB::selectOne('SELECT * FROM user_earnt_stickers WHERE id = ?', [$event->like->earntId])) { $notif = $nm->createNotification($earn->userId, Notification::TYPE_LIKED_EARNT); $notif->stickerId = $earn->stickerId; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } } if ($event->like->toDoId) { if ($toDo = ToDo::find($event->like->toDoId)) { /** @var ToDo $toDo */ $type = $toDo->taskId ? Notification::TYPE_LIKED_TODO_TASK : Notification::TYPE_LIKED_TODO_STICKER; $notif = $nm->createNotification($toDo->userId, $type); $notif->stickerId = $toDo->stickerId; $notif->taskId = $toDo->taskId; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } } if ($event->like->joinId) { $notif = $nm->createNotification($event->like->joinId, Notification::TYPE_LIKED_JOIN); $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } }
/** * @param User $user * * @return int * @throws Exception */ public function unlike(User $user) { if (!$this->userLikes($user)) { throw new Exception("User does not like that to do"); } $toDo = $this->objectOrId instanceof ToDo ? $this->objectOrId : ToDo::find($this->objectOrId); $likes = Like::where('userId', $user->id)->where('toDoId', $toDo->id)->get(); $deleted = 0; foreach ($likes as $like) { if ($like->delete()) { ++$deleted; } } if ($deleted) { $toDo->likeCount -= $deleted; $toDo->save(); } return $deleted; }
/** * Define your route model bindings, pattern filters, etc. * * @param \Illuminate\Routing\Router $router * * @return void */ public function boot(Router $router) { /** * Route model binding */ $router->bind('category', function ($slug) { return app('CategoryRepository')->findBySlugOrFail($slug); }); $router->bind('user', function ($username) { return app('UserRepository')->findBySlugOrFail($username); }); $router->bind('sticker', function ($slug) { return app('StickerRepository')->findBySlugOrFail($slug); }); $router->bind('task', function ($slug) { return app('TaskRepository')->findBySlugOrFail($slug); }); $router->bind('submission', function ($id) { return \Stickable\Models\Submission::findOrFail($id); }); $router->bind('post', function ($id) { return \Stickable\Models\Post::findOrFail($id); }); $router->bind('comment', function ($id) { return \Stickable\Models\Comment::findOrFail($id); }); $router->bind('notification', function ($id) { return \Stickable\Models\Notification::findOrFail($id); }); $router->bind('event', function ($id) { return \Stickable\Models\EventLog::findOrFail($id); }); $router->bind('todo', function ($id) { return \Stickable\Models\ToDo::findOrFail($id); }); parent::boot($router); }