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