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