/** * Display a listing of the resource. * * @param User $user * @param StickerRepository $stickerRepository * * @return Response */ public function index(User $user, StickerRepository $stickerRepository) { $tasks = $user->todoTasks; $stickers = $user->toDoStickers()->with('tasks')->get(); $stickersArray = []; foreach ($stickers as $sticker) { $stickersArray[$sticker->id] = $sticker->toArray(); $pm = new ProgressManager($this->user, $sticker); $stickersArray[$sticker->id]['progress'] = $pm->getTaskProgress(); } foreach ($tasks as $task) { if (empty($stickersArray[$task->stickerId])) { if ($sticker = $stickerRepository->find($task->stickerId)) { $stickersArray[$sticker->id] = $sticker->toArray(); } else { continue; } } if (!array_key_exists('todoTasks', $stickersArray[$task->stickerId])) { $stickersArray[$task->stickerId]['todoTasks'] = []; } $stickersArray[$task->stickerId]['todoTasks'][] = $task->toArray(); } return $this->response(['user' => $user, 'stickers' => array_values($stickersArray)]); }
/** * @api {get} /stickers/:slug Get Sticker Info * @apiGroup Stickers * @apiDescription Returns information about a sticker, with a list of tasks. * * @param Sticker $sticker * * @return \Illuminate\Http\Response */ public function show(Sticker $sticker) { $sticker->load(['tasks', 'category', 'category.stickers']); $stickerArray = $sticker->toArray(); if ($this->user) { $pm = new ProgressManager($this->user, $sticker); $stickerArray['progress'] = $pm->getTaskProgress(); if ($stickerArray['hasCompleted']) { $earnt = $this->user->earntStickers()->where('stickerId', $sticker->id)->first(); $stickerArray['completedOn'] = (new DateTime($earnt->pivot->createdAt))->format(DateTime::ATOM); } usort($stickerArray['tasks'], function ($a, $b) { return $a['hasCompleted'] > $b['hasCompleted']; }); } // TODO: Cache category and category.stickers return $this->response(['sticker' => $stickerArray]); }