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