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