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