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