repository = $favorite; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Classifieds\Advertising\Repositories\Criteria\FavoriteResourceCriteria::class); } /** * Display a list of favorite. * * @return Response */ public function index(FavoriteRequest $request) { $view = $this->response->theme->listView(); if ($this->response->typeIs('json')) { $function = camel_case('get-' . $view); return $this->repository ->setPresenter(\Classifieds\Advertising\Repositories\Presenter\FavoritePresenter::class) ->$function(); } $favorites = $this->repository->paginate(); return $this->response->setMetaTitle(trans('advertising::favorite.names')) ->view('advertising::favorite.index', true) ->data(compact('favorites', 'view')) ->output(); } /** * Display favorite. * * @param Request $request * @param Model $favorite * * @return Response */ public function show(FavoriteRequest $request, Favorite $favorite) { if ($favorite->exists) { $view = 'advertising::favorite.show'; } else { $view = 'advertising::favorite.new'; } return $this->response->setMetaTitle(trans('app.view') . ' ' . trans('advertising::favorite.name')) ->data(compact('favorite')) ->view($view, true) ->output(); } /** * Show the form for creating a new favorite. * * @param Request $request * * @return Response */ public function create(FavoriteRequest $request) { $favorite = $this->repository->newInstance([]); return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('advertising::favorite.name')) ->view('advertising::favorite.create', true) ->data(compact('favorite')) ->output(); } /** * Create new favorite. * * @param Request $request * * @return Response */ public function store(FavoriteRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $favorite = $this->repository->create($attributes); return $this->response->message(trans('messages.success.created', ['Module' => trans('advertising::favorite.name')])) ->code(204) ->status('success') ->url(guard_url('advertising/favorite/' . $favorite->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/advertising/favorite')) ->redirect(); } } /** * Show favorite for editing. * * @param Request $request * @param Model $favorite * * @return Response */ public function edit(FavoriteRequest $request, Favorite $favorite) { return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('advertising::favorite.name')) ->view('advertising::favorite.edit', true) ->data(compact('favorite')) ->output(); } /** * Update the favorite. * * @param Request $request * @param Model $favorite * * @return Response */ public function update(FavoriteRequest $request, Favorite $favorite) { try { $attributes = $request->all(); $favorite->update($attributes); return $this->response->message(trans('messages.success.updated', ['Module' => trans('advertising::favorite.name')])) ->code(204) ->status('success') ->url(guard_url('advertising/favorite/' . $favorite->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('advertising/favorite/' . $favorite->getRouteKey())) ->redirect(); } } /** * Remove the favorite. * * @param Model $favorite * * @return Response */ public function destroy(FavoriteRequest $request, Favorite $favorite) { try { $favorite->delete(); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('advertising::favorite.name')])) ->code(202) ->status('success') ->url(guard_url('advertising/favorite/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('advertising/favorite/' . $favorite->getRouteKey())) ->redirect(); } } /** * Remove multiple favorite. * * @param Model $favorite * * @return Response */ public function delete(FavoriteRequest $request, $type) { try { $ids = hashids_decode($request->input('ids')); if ($type == 'purge') { $this->repository->purge($ids); } else { $this->repository->delete($ids); } return $this->response->message(trans('messages.success.deleted', ['Module' => trans('advertising::favorite.name')])) ->status("success") ->code(202) ->url(guard_url('advertising/favorite')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/advertising/favorite')) ->redirect(); } } /** * Restore deleted favorites. * * @param Model $favorite * * @return Response */ public function restore(FavoriteRequest $request) { try { $ids = hashids_decode($request->input('ids')); $this->repository->restore($ids); return $this->response->message(trans('messages.success.restore', ['Module' => trans('advertising::favorite.name')])) ->status("success") ->code(202) ->url(guard_url('/advertising/favorite')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/advertising/favorite/')) ->redirect(); } } }