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