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