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