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