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