repository = $catalog; parent::__construct(); } /** * Display a list of catalog. * * @return Response */ public function index(CatalogAdminWebRequest $request) { if ($request->wantsJson()) { $catalogs = $this->repository->setPresenter('\\Dmitry\\Catalog\\Repositories\\Presenter\\CatalogListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->all(); return response()->json($catalogs, 200); } $this ->theme->prependTitle(trans('catalog::catalog.names').' :: '); return $this->theme->of('catalog::admin.catalog.index')->render(); } /** * Display catalog. * * @param Request $request * @param Model $catalog * * @return Response */ public function show(CatalogAdminWebRequest $request, Catalog $catalog) { if (!$catalog->exists) { return response()->view('catalog::admin.catalog.new', compact('catalog')); } Form::populate($catalog); return response()->view('catalog::admin.catalog.show', compact('catalog')); } /** * Show the form for creating a new catalog. * * @param Request $request * * @return Response */ public function create(CatalogAdminWebRequest $request) { $catalog = $this->repository->newInstance([]); Form::populate($catalog); return response()->view('catalog::admin.catalog.create', compact('catalog')); } /** * Create new catalog. * * @param Request $request * * @return Response */ public function store(CatalogAdminWebRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.web'); $catalog = $this->repository->create($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('catalog::catalog.name')]), 'code' => 204, 'redirect' => trans_url('/admin/catalog/catalog/'.$catalog->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, ], 400); } } /** * Show catalog for editing. * * @param Request $request * @param Model $catalog * * @return Response */ public function edit(CatalogAdminWebRequest $request, Catalog $catalog) { Form::populate($catalog); return response()->view('catalog::admin.catalog.edit', compact('catalog')); } /** * Update the catalog. * * @param Request $request * @param Model $catalog * * @return Response */ public function update(CatalogAdminWebRequest $request, Catalog $catalog) { try { $attributes = $request->all(); $catalog->update($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('catalog::catalog.name')]), 'code' => 204, 'redirect' => trans_url('/admin/catalog/catalog/'.$catalog->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/catalog/catalog/'.$catalog->getRouteKey()), ], 400); } } /** * Remove the catalog. * * @param Model $catalog * * @return Response */ public function destroy(CatalogAdminWebRequest $request, Catalog $catalog) { try { $t = $catalog->delete(); return response()->json([ 'message' => trans('messages.success.deleted', ['Module' => trans('catalog::catalog.name')]), 'code' => 202, 'redirect' => trans_url('/admin/catalog/catalog/0'), ], 202); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/catalog/catalog/'.$catalog->getRouteKey()), ], 400); } } }