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