diff --git a/db/migrate.php b/db/migrate.php new file mode 100644 index 0000000..2c88323 --- /dev/null +++ b/db/migrate.php @@ -0,0 +1,2 @@ +exec($sql); + echo "Migration successful: 'restaurants' table created or already exists.\n"; +} catch (PDOException $e) { + die("Migration failed: " . $e->getMessage() . "\n"); +} + diff --git a/db/seed.php b/db/seed.php new file mode 100644 index 0000000..9fc9976 --- /dev/null +++ b/db/seed.php @@ -0,0 +1,2 @@ + 'Bonito', + 'latitude' => 38.716842, + 'longitude' => -9.140232, + 'description' => 'A cozy place with great seafood.' + ], + [ + 'name' => 'D\'Bacalhau', + 'latitude' => 38.711963, + 'longitude' => -9.133382, + 'description' => 'The best codfish in town.' + ], + [ + 'name' => 'Tasca do Chico', + 'latitude' => 38.714244, + 'longitude' => -9.143634, + 'description' => 'Live Fado music and traditional food.' + ], + [ + 'name' => 'Solar dos Presuntos', + 'latitude' => 38.717889, + 'longitude' => -9.14299, + 'description' => 'Famous for its fresh seafood and lively atmosphere.' + ], + [ + 'name' => 'Cervejaria Ramiro', + 'latitude' => 38.719931, + 'longitude' => -9.139225, + 'description' => 'A legendary seafood restaurant.' + ] +]; + +try { + $pdo = db(); + $stmt = $pdo->prepare("INSERT INTO restaurants (name, latitude, longitude, description) VALUES (:name, :latitude, :longitude, :description)"); + + foreach ($restaurants as $restaurant) { + $stmt->execute($restaurant); + } + + echo "Seeding successful: " . count($restaurants) . " restaurants inserted.\n"; +} catch (PDOException $e) { + die("Seeding failed: " . $e->getMessage() . "\n"); +} diff --git a/index.php b/index.php index 19645e9..079291a 100644 --- a/index.php +++ b/index.php @@ -1,12 +1,23 @@ +query('SELECT name, latitude, longitude, description FROM restaurants'); + $restaurants = $stmt->fetchAll(); +} catch (PDOException $e) { + // For development, you might want to log this error. + // For production, you could show a generic error message. + error_log('Database error: ' . $e->getMessage()); + $restaurants = []; // Ensure restaurants is an empty array on error +} +?>