repository = $allowance; $this->repository ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria')) ->pushCriteria(new \Payroll\Allowance\Repositories\Criteria\AllowanceUserCriteria()); parent::__construct(); } /** * Display a list of allowance. * * @return json */ public function index(AllowanceRequest $request) { $allowances = $this->repository ->setPresenter('\\Payroll\\Allowance\\Repositories\\Presenter\\AllowanceListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->all(); $allowances['code'] = 2000; return response()->json($allowances) ->setStatusCode(200, 'INDEX_SUCCESS'); } /** * Display allowance. * * @param Request $request * @param Model Allowance * * @return Json */ public function show(AllowanceRequest $request, Allowance $allowance) { if ($allowance->exists) { $allowance = $allowance->presenter(); $allowance['code'] = 2001; return response()->json($allowance) ->setStatusCode(200, 'SHOW_SUCCESS');; } else { return response()->json([]) ->setStatusCode(400, 'SHOW_ERROR'); } } /** * Show the form for creating a new allowance. * * @param Request $request * * @return json */ public function create(AllowanceRequest $request, Allowance $allowance) { $allowance = $allowance->presenter(); $allowance['code'] = 2002; return response()->json($allowance) ->setStatusCode(200, 'CREATE_SUCCESS'); } /** * Create new allowance. * * @param Request $request * * @return json */ public function store(AllowanceRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.api'); $allowance = $this->repository->create($attributes); $allowance = $allowance->presenter(); $allowance['code'] = 2004; return response()->json($allowance) ->setStatusCode(201, 'STORE_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4004, ])->setStatusCode(400, 'STORE_ERROR'); } } /** * Show allowance for editing. * * @param Request $request * @param Model $allowance * * @return json */ public function edit(AllowanceRequest $request, Allowance $allowance) { if ($allowance->exists) { $allowance = $allowance->presenter(); $allowance['code'] = 2003; return response()-> json($allowance) ->setStatusCode(200, 'EDIT_SUCCESS');; } else { return response()->json([]) ->setStatusCode(400, 'SHOW_ERROR'); } } /** * Update the allowance. * * @param Request $request * @param Model $allowance * * @return json */ public function update(AllowanceRequest $request, Allowance $allowance) { try { $attributes = $request->all(); $allowance->update($attributes); $allowance = $allowance->presenter(); $allowance['code'] = 2005; return response()->json($allowance) ->setStatusCode(201, 'UPDATE_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4005, ])->setStatusCode(400, 'UPDATE_ERROR'); } } /** * Remove the allowance. * * @param Request $request * @param Model $allowance * * @return json */ public function destroy(AllowanceRequest $request, Allowance $allowance) { try { $t = $allowance->delete(); return response()->json([ 'message' => trans('messages.success.delete', ['Module' => trans('allowance::allowance.name')]), 'code' => 2006 ])->setStatusCode(202, 'DESTROY_SUCCESS'); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 4006, ])->setStatusCode(400, 'DESTROY_ERROR'); } } }