converting store files done

This commit is contained in:
armiejean 2025-04-11 10:51:24 +08:00
parent 6825ce3c30
commit 6db9461437
4 changed files with 105 additions and 25 deletions

View File

@ -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;
}

View File

@ -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');
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());
}
}

View File

@ -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()
{
//
// ...
}
}

View File

@ -0,0 +1,73 @@
<?php
namespace App\Services;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Log;
class ApiService
{
protected $baseUrl;
public function __construct()
{
$this->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'));
}
}
}