From ffe81cdf88a1e64482396ba9d5d78942d96f8bfe Mon Sep 17 00:00:00 2001 From: patrickzerhusen Date: Fri, 24 Apr 2026 16:33:03 +0200 Subject: [PATCH] migration for news table in database, news now read from database --- migrations/003_news_table.sql | 44 +++++++++++++++++++++++++++++++++++ public/index.php | 22 +++++++++++++----- 2 files changed, 60 insertions(+), 6 deletions(-) create mode 100644 migrations/003_news_table.sql diff --git a/migrations/003_news_table.sql b/migrations/003_news_table.sql new file mode 100644 index 0000000..dbbc3ad --- /dev/null +++ b/migrations/003_news_table.sql @@ -0,0 +1,44 @@ +-- ===================================================================== +-- Migration 004: Creates News Table for Municipality Announcements +-- ===================================================================== + + +-- --------------------------------------------------------------------- +-- Block 1: Creates Table "news" +-- --------------------------------------------------------------------- +CREATE TABLE IF NOT EXISTS news ( + news_id SERIAL PRIMARY KEY, + municipality_id INTEGER NOT NULL REFERENCES municipalities(municipality_id) ON DELETE CASCADE, + title VARCHAR(255) NOT NULL, + content TEXT NOT NULL, + author_name VARCHAR(100) NOT NULL DEFAULT 'Stadtverwaltung', + published_at TIMESTAMP NOT NULL DEFAULT NOW(), + created_at TIMESTAMP NOT NULL DEFAULT NOW(), + updated_at TIMESTAMP NOT NULL DEFAULT NOW() +); + + +-- --------------------------------------------------------------------- +-- Block 2: Trigger Functions +-- --------------------------------------------------------------------- + +-- Automatically Refresh updated_at on every UPDATE. +CREATE TRIGGER set_news_updated_at + BEFORE UPDATE ON news + FOR EACH ROW + EXECUTE FUNCTION set_updated_at(); + + +-- --------------------------------------------------------------------- +-- Block 4: Indexes for fast Queries +-- --------------------------------------------------------------------- +CREATE INDEX idx_news_municipality ON news(municipality_id); + + +-- --------------------------------------------------------------------- +-- Block 8: Seed Data — Initial News Article +-- --------------------------------------------------------------------- +INSERT INTO news (municipality_id, title, content) +SELECT municipality_id, 'Mitmachkarte gestartet', + 'Die Mitmachkarte als Bürgerbeteiligungsportal der Stadt Lohne (Oldenburg) wird nun freigeschaltet. Wir freuen uns auf Ihre Hinweise und Vorschläge!' +FROM municipalities WHERE slug = 'lohne'; \ No newline at end of file diff --git a/public/index.php b/public/index.php index 900a11d..b2a3d2d 100644 --- a/public/index.php +++ b/public/index.php @@ -34,6 +34,11 @@ if (!$municipality) { exit; } +// Loads News for Sidebar +$stmt = $pdo->prepare("SELECT * FROM news WHERE municipality_id = :mid ORDER BY published_at DESC LIMIT 10"); +$stmt->execute([':mid' => $municipality['municipality_id']]); +$news_items = $stmt->fetchAll(); + ?> @@ -207,12 +212,17 @@ if (!$municipality) {