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