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