apiService = $apiService; } public function index() { try { $response = $this->apiService->get('/users'); if ($response->successful()) { return view('users.index', [ 'users' => $response->json()['data'] ]); } return back()->with('error', 'Unable to fetch users.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } public function store(Request $request) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|max:255', 'password' => 'required|min:8', ]); try { $response = $this->apiService->post('/users', $validated); if ($response->successful()) { return redirect()->route('users.index') ->with('success', 'User created successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to create user.'); } } public function update(Request $request, $id) { $validated = $request->validate([ 'name' => 'required|string|max:255', 'email' => 'required|email|max:255', ]); try { $response = $this->apiService->put("/users/{$id}", $validated); if ($response->successful()) { return redirect()->route('users.index') ->with('success', 'User updated successfully.'); } return back()->withErrors($response->json()['errors']); } catch (\Exception $e) { return back()->with('error', 'Unable to update user.'); } } public function destroy($id) { try { $response = $this->apiService->delete("/users/{$id}"); if ($response->successful()) { return redirect()->route('users.index') ->with('success', 'User deleted successfully.'); } return back()->with('error', 'Unable to delete user.'); } catch (\Exception $e) { return back()->with('error', 'Service unavailable.'); } } }