From e86173f7ecb3409aaa5c525f08cef32cc51dc4f5 Mon Sep 17 00:00:00 2001 From: Carsten Kvist Date: Mon, 13 Apr 2026 13:53:47 +0200 Subject: [PATCH] Sync playlister --- linedance-api/app/routers/sync.py | 32 +++++++++++++++++++--- linedance-app/local/sync_manager.py | 42 +++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 6 deletions(-) diff --git a/linedance-api/app/routers/sync.py b/linedance-api/app/routers/sync.py index c6bc80a0..0d4637df 100644 --- a/linedance-api/app/routers/sync.py +++ b/linedance-api/app/routers/sync.py @@ -266,9 +266,33 @@ def pull( ] }) + # Egne playlister + my_playlists = [] + for p in db.query(Project).filter_by(owner_id=me.id).all(): + songs_out = [] + for ps in p.project_songs: + song = db.query(Song).filter_by(id=ps.song_id).first() + if not song: + continue + songs_out.append({ + "title": song.title, + "artist": song.artist, + "position": ps.position, + "status": ps.status, + "is_workshop": ps.is_workshop, + "dance_override": ps.dance_override or "", + }) + my_playlists.append({ + "server_id": p.id, + "name": p.name, + "description": p.description or "", + "songs": sorted(songs_out, key=lambda x: x["position"]), + }) + return { - "levels": levels, - "dances": dances, - "community": community, - "shared": shared, + "levels": levels, + "dances": dances, + "community": community, + "shared": shared, + "my_playlists": my_playlists, } diff --git a/linedance-app/local/sync_manager.py b/linedance-app/local/sync_manager.py index 4d9a02fc..73e277f1 100644 --- a/linedance-app/local/sync_manager.py +++ b/linedance-app/local/sync_manager.py @@ -220,11 +220,11 @@ class SyncManager: # ── Anvend pull ─────────────────────────────────────────────────────────── def _apply_pull(self, data: dict): - """Gem server-data lokalt — opdaterer dans-info og community forslag.""" + """Gem server-data lokalt — opdaterer dans-info og importerer playlister.""" conn = sqlite3.connect(self._db_path) conn.row_factory = sqlite3.Row - # Opdater dans-info fra server (koreograf, links, noter) + # Opdater dans-info fra server for d in data.get("dances", []): if not d.get("name"): continue @@ -241,5 +241,43 @@ class SyncManager: """, (d.get("choreographer",""), d.get("video_url",""), d.get("stepsheet_url",""), existing["id"])) + # Importer egne playlister fra server hvis de ikke findes lokalt + for pl in data.get("my_playlists", []): + server_id = pl.get("server_id") + name = pl.get("name", "") + if not server_id or not name: + continue + + # Tjek om listen allerede eksisterer lokalt + existing = conn.execute( + "SELECT id FROM playlists WHERE api_project_id=?", (server_id,) + ).fetchone() + if existing: + continue # Allerede importeret — spring over + + # Opret liste + cur = conn.execute( + "INSERT INTO playlists (name, description, api_project_id, is_linked, server_permission) " + "VALUES (?,?,?,1,'edit')", + (name, pl.get("description",""), server_id) + ) + pl_id = cur.lastrowid + + # Indsæt sange med lokal matching + for song_data in pl.get("songs", []): + local = conn.execute( + "SELECT id FROM songs WHERE title=? AND artist=? AND file_missing=0 LIMIT 1", + (song_data["title"], song_data["artist"]) + ).fetchone() + if local: + conn.execute(""" + INSERT INTO playlist_songs + (playlist_id, song_id, position, status, is_workshop, dance_override) + VALUES (?,?,?,?,?,?) + """, (pl_id, local["id"], song_data["position"], + song_data.get("status","pending"), + 1 if song_data.get("is_workshop") else 0, + song_data.get("dance_override","") or "")) + conn.commit() conn.close()