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