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