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