From 6db9461437114a059c7bae87ec9c8b0a42961018 Mon Sep 17 00:00:00 2001 From: armiejean Date: Fri, 11 Apr 2025 10:51:24 +0800 Subject: [PATCH] converting store files done --- app/Livewire/FetchData.php | 20 +++++--- app/Livewire/Logout.php | 18 ++++--- app/Providers/AppServiceProvider.php | 19 +++----- app/Services/ApiService.php | 73 ++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 25 deletions(-) create mode 100644 app/Services/ApiService.php diff --git a/app/Livewire/FetchData.php b/app/Livewire/FetchData.php index 8f135b9..998fcd9 100644 --- a/app/Livewire/FetchData.php +++ b/app/Livewire/FetchData.php @@ -3,7 +3,7 @@ namespace App\Livewire; use Livewire\Component; -use Illuminate\Support\Facades\Http; +use App\Services\ApiService; use Illuminate\Support\Facades\Session; class FetchData extends Component @@ -12,6 +12,13 @@ class FetchData extends Component public $loading = false; public $url; + protected $apiService; + + public function boot(ApiService $apiService) + { + $this->apiService = $apiService; + } + public function mount($url) { $this->url = $url; @@ -23,15 +30,12 @@ class FetchData extends Component $this->loading = true; try { - $response = Http::get(config('app.api_base_url') . $this->url); - - if ($response->successful()) { - $this->data = $response->json(); - } else { - Session::flash('error', 'Failed to fetch data: ' . $response->body()); + $data = $this->apiService->fetchData($this->url); + if ($data) { + $this->data = $data; } } catch (\Exception $e) { - Session::flash('error', 'An error occurred while fetching data.'); + Session::flash('error', $e->getMessage()); } finally { $this->loading = false; } diff --git a/app/Livewire/Logout.php b/app/Livewire/Logout.php index 9d1403c..8c14ede 100644 --- a/app/Livewire/Logout.php +++ b/app/Livewire/Logout.php @@ -3,20 +3,26 @@ namespace App\Livewire; use Livewire\Component; -use Illuminate\Support\Facades\Http; +use App\Services\ApiService; use Illuminate\Support\Facades\Session; class Logout extends Component { + protected $apiService; + + public function boot(ApiService $apiService) + { + $this->apiService = $apiService; + } + public function logout() { try { - Http::withHeaders(['Authorization' => 'Bearer ' . Session::get('token')])->get(config('app.api_base_url') . '/logout'); - - Session::forget('token'); - return redirect()->route('login')->with('success', 'Logged out successfully.'); + if ($this->apiService->logout()) { + return redirect()->route('login')->with('success', 'Logged out successfully.'); + } } catch (\Exception $e) { - return redirect()->back()->with('error', 'Logout failed: ' . $e->getMessage()); + return redirect()->back()->with('error', $e->getMessage()); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index 452e6b6..f89452e 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -2,23 +2,20 @@ namespace App\Providers; +use App\Services\ApiService; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { - /** - * Register any application services. - */ - public function register(): void + public function register() { - // + $this->app->singleton(ApiService::class, function () { + return new ApiService(); + }); } - /** - * Bootstrap any application services. - */ - public function boot(): void + public function boot() { - // + // ... } -} +} \ No newline at end of file diff --git a/app/Services/ApiService.php b/app/Services/ApiService.php new file mode 100644 index 0000000..45641b3 --- /dev/null +++ b/app/Services/ApiService.php @@ -0,0 +1,73 @@ +baseUrl = config('app.api_base_url'); + } + + public function fetchData($url, $token = null) + { + try { + $response = Http::withHeaders([ + 'Authorization' => 'Bearer ' . ($token ?: Session::get('token', '')), + ])->get($this->baseUrl . $url); + + if ($response->successful()) { + return $response->json(); + } else { + $this->handleError($response); + return null; + } + } catch (\Exception $e) { + Log::error('API Fetch Error: ' . $e->getMessage()); + Session::flash('error', 'Failed to fetch data: ' . $e->getMessage()); + return null; + } + } + + public function logout($token = null) + { + try { + $response = Http::withHeaders([ + 'Authorization' => 'Bearer ' . ($token ?: Session::get('token', '')), + ])->get($this->baseUrl . '/logout'); + + if ($response->successful()) { + Session::forget('token'); + Session::flash('success', 'Logged out successfully.'); + return true; + } else { + $this->handleError($response); + return false; + } + } catch (\Exception $e) { + Log::error('Logout Error: ' . $e->getMessage()); + Session::flash('error', 'Logout failed: ' . $e->getMessage()); + return false; + } + } + + public function handleError($response) + { + $status = $response->status(); + + if ($status === 401) { + Session::forget('token'); + throw new \Exception('Session expired. Please login again.'); + } elseif ($status === 404) { + throw new \Exception('API resource not found.'); + } else { + throw new \Exception('API error: ' . ($response->body() ?: 'Unknown error')); + } + } +} \ No newline at end of file