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