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