Feature: Welcome-Dashboard für eingeloggte User — Hundefoto-Hero, Stats-Chips, Feature-Karten — SW by-v475, APP_VER 452

This commit is contained in:
rene 2026-04-29 06:35:42 +02:00
parent c8ae514c01
commit db386da2c0
5 changed files with 359 additions and 27 deletions

View file

@ -8,7 +8,7 @@ from typing import Optional
from database import db
from auth import get_current_user
from routes.push import send_push_to_user
from media_utils import safe_media_path
from media_utils import safe_media_path, preview_url_from
router = APIRouter()
MEDIA_DIR = os.getenv("MEDIA_DIR", "/data/media")
@ -96,6 +96,74 @@ async def create_dog(data: DogCreate, user=Depends(get_current_user)):
return dict(dog)
@router.get("/{dog_id}/welcome-dashboard")
async def get_welcome_dashboard(dog_id: int, user=Depends(get_current_user)):
"""Liefert kompakte Dashboard-Daten für die Welcome-Ansicht eines Hundes."""
import random as _random
with db() as conn:
# Besitz prüfen
dog = conn.execute(
"SELECT id FROM dogs WHERE id=? AND user_id=?", (dog_id, user["id"])
).fetchone()
if not dog:
raise HTTPException(404, "Hund nicht gefunden.")
# Zufälliges Foto aus den letzten 100 Tagebuchbildern
photos = conn.execute(
"""SELECT dm.url FROM diary_media dm
JOIN diary d ON d.id = dm.diary_id
WHERE d.dog_id=? AND dm.media_type='image'
ORDER BY d.datum DESC LIMIT 100""",
(dog_id,)
).fetchall()
random_photo = None
if photos:
chosen_url = _random.choice(photos)["url"]
random_photo = {
"url": chosen_url,
"preview_url": preview_url_from(chosen_url),
}
# Neuester Tagebucheintrag
last_diary_row = conn.execute(
"SELECT titel, datum FROM diary WHERE dog_id=? ORDER BY datum DESC LIMIT 1",
(dog_id,)
).fetchone()
last_diary = dict(last_diary_row) if last_diary_row else None
# Nächster Termin (kein Gewicht)
next_appt_row = conn.execute(
"""SELECT bezeichnung, naechstes, typ FROM health
WHERE dog_id=? AND naechstes IS NOT NULL AND naechstes >= date('now')
AND typ != 'gewicht'
ORDER BY naechstes ASC LIMIT 1""",
(dog_id,)
).fetchone()
next_appointment = dict(next_appt_row) if next_appt_row else None
# Letztes Gewicht
last_weight_row = conn.execute(
"""SELECT wert, einheit, datum FROM health
WHERE dog_id=? AND typ='gewicht'
ORDER BY datum DESC LIMIT 1""",
(dog_id,)
).fetchone()
last_weight = dict(last_weight_row) if last_weight_row else None
# Anzahl Tagebucheinträge
diary_count = conn.execute(
"SELECT COUNT(*) AS n FROM diary WHERE dog_id=?", (dog_id,)
).fetchone()["n"]
return {
"random_photo": random_photo,
"last_diary": last_diary,
"next_appointment": next_appointment,
"last_weight": last_weight,
"diary_count": diary_count,
}
@router.get("/{dog_id}")
async def get_dog(dog_id: int, user=Depends(get_current_user)):
with db() as conn: