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