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