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