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