From 06281b26546b0604bf22b1835e4aa5f9a8304e8d Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 4 Jul 2025 21:29:01 -0400 Subject: [PATCH] put all songs for a session into an array --- server.cpp | 65 +++++++++++++++++++++++++++++------------------------- 1 file changed, 35 insertions(+), 30 deletions(-) diff --git a/server.cpp b/server.cpp index 165e8c1..e89b819 100644 --- a/server.cpp +++ b/server.cpp @@ -174,7 +174,9 @@ int main() prep_stmt->setInt(1, category_id); ResultSet *res = prep_stmt->executeQuery(); - int i = 0; + int i = -1; + int previous_session_id = 0; + int current_song_index; while (res->next()) { int id = res->getInt("id"); @@ -184,39 +186,42 @@ int main() string created_at = res->getString("created_at"); string category = res->getString("category"); string instrument = res->getString("instrument"); - - crow::json::wvalue songs; int song_id = res->getInt("song_id"); string song_title = res->getString("title"); - songs[0] = { - {"id", song_id}, - {"title", song_title} - }; - - //crow::json::wvalue currentSong; - //currentSong["id"] = res->getInt("song_id"); - //currentSong["title"] = res->getString("title"); - - //crow::json::wvalue currentSong = { - //{"id", res->getInt("song_id")}, - //{"title", res->getString("title")} - //}; - //songs.push_back(std::move(currentSong)); - - entries[i] = { - {"id", id}, - {"description", description}, - {"seconds", seconds}, - {"comments", comments}, - {"created_at", created_at}, - {"category", category}, - {"instrument", instrument}, - {"songs", songs} - }; - i++; - } + if(id != previous_session_id){ + i++; + + previous_session_id = id; + current_song_index = 0; + crow::json::wvalue songs = crow::json::wvalue::list(); + if(song_id != 0){ + songs[current_song_index] = { + {"id", song_id}, + {"title", song_title} + }; + } + + entries[i] = { + {"id", id}, + {"description", description}, + {"seconds", seconds}, + {"comments", comments}, + {"created_at", created_at}, + {"category", category}, + {"instrument", instrument}, + {"songs", songs} + }; + current_song_index++; + } else { + entries[i]["songs"][current_song_index] = { + {"id", song_id}, + {"title", song_title} + }; + current_song_index++; + } + } delete res; return entries; });