Feature: HdM Community-Vote — alle öffentlichen Hunde wählbar, eigene ausgenommen, SW by-v597

This commit is contained in:
rene 2026-05-02 08:44:59 +02:00
parent 83958cbb0b
commit 031c6028ac
5 changed files with 127 additions and 51 deletions

View file

@ -386,6 +386,34 @@ async def get_hund_des_monats(user=Depends(get_current_user_optional)):
return {"monat": monat, "top": [dict(r) for r in rows], "user_vote": user_vote}
@router.get("/hund-des-monats/kandidaten")
async def get_hdm_kandidaten(user=Depends(get_current_user)):
"""Alle öffentlichen Hunde anderer User, mit aktuellem Stimmenstand."""
monat = datetime.now().strftime("%Y-%m")
with db() as conn:
rows = conn.execute("""
SELECT d.id, d.name, d.rasse, d.foto_url,
u.name AS besitzer_name,
COALESCE(v.stimmen, 0) AS stimmen
FROM dogs d
JOIN users u ON u.id = d.user_id
LEFT JOIN (
SELECT dog_id, COUNT(*) AS stimmen
FROM hund_des_monats_votes
WHERE monat = ?
GROUP BY dog_id
) v ON v.dog_id = d.id
WHERE d.is_public = 1
AND d.user_id != ?
ORDER BY
CASE WHEN d.foto_url IS NOT NULL THEN 0 ELSE 1 END,
stimmen DESC,
d.name ASC
LIMIT 60
""", (monat, user["id"])).fetchall()
return [dict(r) for r in rows]
@router.post("/hund-des-monats/vote")
async def vote_hund_des_monats(data: HundDesMonatsVoteRequest, user=Depends(get_current_user)):
monat = datetime.now().strftime("%Y-%m")
@ -393,7 +421,9 @@ async def vote_hund_des_monats(data: HundDesMonatsVoteRequest, user=Depends(get_
dog = conn.execute("SELECT id, user_id, is_public FROM dogs WHERE id=?", (data.dog_id,)).fetchone()
if not dog:
raise HTTPException(404, "Hund nicht gefunden.")
if dog["user_id"] != user["id"] and not dog["is_public"]:
if dog["user_id"] == user["id"]:
raise HTTPException(403, "Du kannst nicht für deinen eigenen Hund abstimmen.")
if not dog["is_public"]:
raise HTTPException(403, "Dieser Hund ist nicht öffentlich.")
conn.execute("""
INSERT INTO hund_des_monats_votes (user_id, dog_id, monat)