repository = $favorite; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Classifieds\Advertisement\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\Advertisement\Repositories\Presenter\FavoritePresenter::class) ->$function(); } $favorites = $this->repository->paginate(); return $this->response->setMetaTitle(trans('advertisement::favorite.names')) ->view('advertisement::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 = 'advertisement::favorite.show'; } else { $view = 'advertisement::favorite.new'; } return $this->response->setMetaTitle(trans('app.view') . ' ' . trans('advertisement::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('advertisement::favorite.name')) ->view('advertisement::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('advertisement::favorite.name')])) ->code(204) ->status('success') ->url(guard_url('advertisement/favorite/' . $favorite->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/advertisement/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('advertisement::favorite.name')) ->view('advertisement::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('advertisement::favorite.name')])) ->code(204) ->status('success') ->url(guard_url('advertisement/favorite/' . $favorite->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('advertisement/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('advertisement::favorite.name')])) ->code(202) ->status('success') ->url(guard_url('advertisement/favorite/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('advertisement/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('advertisement::favorite.name')])) ->status("success") ->code(202) ->url(guard_url('advertisement/favorite')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/advertisement/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('advertisement::favorite.name')])) ->status("success") ->code(202) ->url(guard_url('/advertisement/favorite')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/advertisement/favorite/')) ->redirect(); } } }