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