diff --git a/src/routers/public_api.py b/src/routers/public_api.py index f315d0b..e515342 100644 --- a/src/routers/public_api.py +++ b/src/routers/public_api.py @@ -142,6 +142,30 @@ async def _build_lagebild_response(db, incident_ids: list, primary_id: int) -> d ) locations = [dict(r) for r in await cursor.fetchall()] + # Top-3-Artikel pro Location (neueste zuerst) + cursor = await db.execute( + f"""SELECT al.location_name_normalized as loc_name, + a.headline_de, a.headline, a.source, a.source_url + FROM article_locations al + JOIN articles a ON a.id = al.article_id + WHERE al.incident_id IN ({ids}) + ORDER BY a.published_at DESC""" + ) + loc_articles = {} + for r in await cursor.fetchall(): + r = dict(r) + name = r["loc_name"] + if name not in loc_articles: + loc_articles[name] = [] + if len(loc_articles[name]) < 3: + loc_articles[name].append({ + "headline": r["headline_de"] or r["headline"] or "", + "source": r["source"] or "", + "url": r["source_url"] or "", + }) + for loc in locations: + loc["top_articles"] = loc_articles.get(loc["name"], []) + return { "generated_at": datetime.now(TIMEZONE).isoformat(), "incident": {