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