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