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