first commit
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
#[Signature('cache:clear-cities')]
|
||||
#[Description('Clear all cached city and attraction data')]
|
||||
class ClearCitiesCache extends Command
|
||||
{
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
$this->info('Clearing cities cache...');
|
||||
|
||||
try {
|
||||
// Get the Redis connection used by cache
|
||||
$redis = Cache::getStore()->getRedis();
|
||||
$prefix = config('cache.prefix');
|
||||
|
||||
// Get all cache keys that match our patterns
|
||||
$patterns = [
|
||||
'wikipedia:*',
|
||||
'opentripmap:*',
|
||||
'overpass:*',
|
||||
];
|
||||
|
||||
$clearedCount = 0;
|
||||
|
||||
foreach ($patterns as $pattern) {
|
||||
$fullPattern = $prefix . $pattern;
|
||||
$keys = $redis->keys($fullPattern);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
// Remove the prefix to get the actual cache key
|
||||
$cacheKey = str_replace($prefix, '', $key);
|
||||
Cache::forget($cacheKey);
|
||||
$clearedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if ($clearedCount === 0) {
|
||||
$this->comment('No cache entries found to clear.');
|
||||
} else {
|
||||
$this->info("Successfully cleared {$clearedCount} cache entries.");
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
} catch (\Exception $e) {
|
||||
$this->error("Error clearing cache: " . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Jobs\FetchAttractionsJob;
|
||||
use App\Models\City;
|
||||
use App\Services\OpenTripMapService;
|
||||
use App\Services\OverpassService;
|
||||
use Illuminate\Console\Attributes\Argument;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('attractions:refresh {cityId}')]
|
||||
#[Description('Refresh attraction data for a specific city')]
|
||||
#[Argument('cityId', description: 'The ID of the city to refresh attractions for')]
|
||||
class RefreshAttractions extends Command
|
||||
{
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(
|
||||
OpenTripMapService $openTripMapService,
|
||||
OverpassService $overpassService
|
||||
): int {
|
||||
$cityId = $this->argument('cityId');
|
||||
|
||||
// Find the city
|
||||
$city = City::find($cityId);
|
||||
|
||||
if (!$city) {
|
||||
$this->error("City with ID {$cityId} not found.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->info("Refreshing attractions for {$city->name}, {$city->country}...");
|
||||
|
||||
if (!$city->latitude || !$city->longitude) {
|
||||
$this->error("City coordinates not set. Cannot fetch attractions.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
try {
|
||||
// Clear cache for this city's coordinates
|
||||
$this->info("Clearing cached data...");
|
||||
$openTripMapService->clearCache($city->latitude, $city->longitude);
|
||||
$overpassService->clearCache($city->latitude, $city->longitude);
|
||||
|
||||
// Delete existing attractions for this city
|
||||
$deletedCount = $city->attractions()->delete();
|
||||
$this->info("Deleted {$deletedCount} existing attractions.");
|
||||
|
||||
// Dispatch job to fetch new attractions
|
||||
$this->info("Fetching fresh attraction data...");
|
||||
FetchAttractionsJob::dispatch($city, true);
|
||||
|
||||
$this->info("Attractions refresh job dispatched successfully.");
|
||||
$this->comment("The job will process in the background. Check logs for completion status.");
|
||||
|
||||
return Command::SUCCESS;
|
||||
} catch (\Exception $e) {
|
||||
$this->error("Error refreshing attractions: " . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Services\WikipediaService;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('test:wikipedia')]
|
||||
#[Description('Test Wikipedia API integration')]
|
||||
class TestWikipediaCommand extends Command
|
||||
{
|
||||
public function __construct(
|
||||
private WikipediaService $wikipediaService
|
||||
) {
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->info('Testing Wikipedia API integration...');
|
||||
$this->newLine();
|
||||
|
||||
// Test 1: Search for cities
|
||||
$this->info('Test 1: Searching for "Paris"');
|
||||
try {
|
||||
$results = $this->wikipediaService->searchCities('Paris');
|
||||
$this->line('Found ' . count($results) . ' results:');
|
||||
foreach (array_slice($results, 0, 3) as $result) {
|
||||
$this->line(' - ' . $result['title'] . ': ' . $result['description']);
|
||||
}
|
||||
$this->newLine();
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Search failed: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Test 2: Get city summary
|
||||
$this->info('Test 2: Getting summary for "Paris"');
|
||||
try {
|
||||
$cityData = $this->wikipediaService->getCitySummary('Paris');
|
||||
if ($cityData) {
|
||||
$this->line('Title: ' . $cityData['title']);
|
||||
$this->line('Summary: ' . substr($cityData['summary'], 0, 200) . '...');
|
||||
$this->line('Coordinates: ' . $cityData['latitude'] . ', ' . $cityData['longitude']);
|
||||
$this->line('URL: ' . $cityData['url']);
|
||||
} else {
|
||||
$this->warn('No data found for Paris');
|
||||
}
|
||||
$this->newLine();
|
||||
} catch (\Exception $e) {
|
||||
$this->error('Summary fetch failed: ' . $e->getMessage());
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Test 3: Test caching
|
||||
$this->info('Test 3: Testing cache (fetching Paris again)');
|
||||
$start = microtime(true);
|
||||
$this->wikipediaService->getCitySummary('Paris');
|
||||
$elapsed = (microtime(true) - $start) * 1000;
|
||||
$this->line('Fetch time: ' . round($elapsed, 2) . 'ms (should be fast if cached)');
|
||||
$this->newLine();
|
||||
|
||||
$this->info('✓ All tests completed successfully!');
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user