/** * Get items for display * * @param Request $request * @param CacheHandlerInterface $cacheHandler * @param ItemRepositoryInterface $itemRepo * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function getAll(Request $request, CacheHandlerInterface $cacheHandler, ItemRepositoryInterface $itemRepo) { $pageRequested = $request->input('page'); $isMainPage = (is_null($pageRequested) or $pageRequested === 1); if ($isMainPage) { // Load from cache if available $items = $cacheHandler->get(CacheHandlerInterface::MAINPAGE); } else { $items = $itemRepo->getItemsPaginated(20, Auth::user()); } // Main page data not available in cache if (!$items) { // Get paginated items for user $items = $itemRepo->getItemsPaginated(20, Auth::user()); // Save in cache for next request $cacheHandler->set(CacheHandlerInterface::MAINPAGE, $items); } $title = "List"; return view('all', ['items' => $items, 'title' => $title]); }
/** * Determine if the user is authorized to make this request. * * @param ItemRepositoryInterface $itemRepo * @return bool */ public function authorize(ItemRepositoryInterface $itemRepo) { $itemId = $this->input('itemId'); $item = $itemRepo->get($itemId, ['user']); return $item->user->id == Auth::user()->id; }