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