From e77bce71dac9cd503175d172378575df2006fed6 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 2 Jul 2025 21:56:52 -0400 Subject: [PATCH 1/7] show category creates additional rows for each song practiced in a session --- server.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server.cpp b/server.cpp index 33c0a87..7fd6b5f 100644 --- a/server.cpp +++ b/server.cpp @@ -166,7 +166,11 @@ int main() connect(); crow::json::wvalue entries; - ResultSet *res = stmt->executeQuery("call show_category("+to_string(category_id)+");"); + //prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, practice_category_id) VALUES (?, ?, ?)"); + //ResultSet *res = stmt->executeQuery("call show_category("+to_string(category_id)+");"); + prep_stmt = con->prepareStatement("SELECT practice_sessions.id, practice_sessions.description, seconds, songs.id AS song_id, songs.title, comments, created_at, practice_categories.name AS category, instruments.name AS instrument FROM practice_sessions JOIN practice_categories ON practice_category_id = practice_categories.id JOIN instruments ON instrument_id = instruments.id LEFT JOIN songs_practiced ON practice_sessions.id = songs_practiced.practice_session_id LEFT JOIN songs ON songs_practiced.song_id = songs.id WHERE practice_category_id = ? ORDER BY practice_sessions.id DESC;"); + prep_stmt->setInt(1, category_id); + ResultSet *res = prep_stmt->executeQuery(); int i = 0; res->afterLast(); @@ -176,7 +180,6 @@ int main() int seconds = res->getInt("seconds"); string comments = res->getString("comments"); string created_at = res->getString("created_at"); - int category_id = res->getInt("category_id"); string category = res->getString("category"); string instrument = res->getString("instrument"); entries[i] = { @@ -185,7 +188,6 @@ int main() {"seconds", seconds}, {"comments", comments}, {"created_at", created_at}, - {"category_id", category_id}, {"category", category}, {"instrument", instrument} }; From a085fddef0d1e57d2053916064c5acc3c8120763 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 2 Jul 2025 22:04:55 -0400 Subject: [PATCH 2/7] cleanup --- server.cpp | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/server.cpp b/server.cpp index 7fd6b5f..971cf5d 100644 --- a/server.cpp +++ b/server.cpp @@ -166,9 +166,11 @@ int main() connect(); crow::json::wvalue entries; - //prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, practice_category_id) VALUES (?, ?, ?)"); - //ResultSet *res = stmt->executeQuery("call show_category("+to_string(category_id)+");"); - prep_stmt = con->prepareStatement("SELECT practice_sessions.id, practice_sessions.description, seconds, songs.id AS song_id, songs.title, comments, created_at, practice_categories.name AS category, instruments.name AS instrument FROM practice_sessions JOIN practice_categories ON practice_category_id = practice_categories.id JOIN instruments ON instrument_id = instruments.id LEFT JOIN songs_practiced ON practice_sessions.id = songs_practiced.practice_session_id LEFT JOIN songs ON songs_practiced.song_id = songs.id WHERE practice_category_id = ? ORDER BY practice_sessions.id DESC;"); + prep_stmt = con->prepareStatement("SELECT practice_sessions.id, practice_sessions.description, seconds, songs.id AS song_id, songs.title, comments, created_at, practice_categories.name AS category, instruments.name AS instrument " + "FROM practice_sessions JOIN practice_categories ON practice_category_id = practice_categories.id " + "JOIN instruments ON instrument_id = instruments.id " + "LEFT JOIN songs_practiced ON practice_sessions.id = songs_practiced.practice_session_id " + "LEFT JOIN songs ON songs_practiced.song_id = songs.id WHERE practice_category_id = ? ORDER BY practice_sessions.id DESC;"); prep_stmt->setInt(1, category_id); ResultSet *res = prep_stmt->executeQuery(); From 52c8b2d67c4a7720ab0fea25a422d337459cd080 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 2 Jul 2025 23:27:09 -0400 Subject: [PATCH 3/7] don't reverse down results. handled in sql --- server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.cpp b/server.cpp index 971cf5d..393bec4 100644 --- a/server.cpp +++ b/server.cpp @@ -175,8 +175,8 @@ int main() ResultSet *res = prep_stmt->executeQuery(); int i = 0; - res->afterLast(); - while (res->previous()) { + while (res->next()) { + int id = res->getInt("id"); string description = res->getString("description"); int seconds = res->getInt("seconds"); From 34b1fabe12f38c4266884361eaa063f643788a38 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Wed, 2 Jul 2025 23:30:28 -0400 Subject: [PATCH 4/7] starting practiced_songs --- server.cpp | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/server.cpp b/server.cpp index 393bec4..165e8c1 100644 --- a/server.cpp +++ b/server.cpp @@ -184,6 +184,26 @@ 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}, @@ -191,7 +211,8 @@ int main() {"comments", comments}, {"created_at", created_at}, {"category", category}, - {"instrument", instrument} + {"instrument", instrument}, + {"songs", songs} }; i++; } From 06281b26546b0604bf22b1835e4aa5f9a8304e8d Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Fri, 4 Jul 2025 21:29:01 -0400 Subject: [PATCH 5/7] 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; }); From 8cc7fb638ea3f6d6d38f63b38a9c4314e8eb9236 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 5 Jul 2025 18:46:59 -0400 Subject: [PATCH 6/7] adds songs to session 5377 --- server.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/server.cpp b/server.cpp index e89b819..812f0d3 100644 --- a/server.cpp +++ b/server.cpp @@ -65,6 +65,15 @@ int main() } + if(request_body.has("songs")){ + for(const auto& song : request_body["songs"]){ + prep_stmt = con->prepareStatement("INSERT INTO songs_practiced (practice_session_id, song_id) VALUES (?, ?)"); + prep_stmt->setInt(1, 5377); + prep_stmt->setInt(2, int(song["id"])); + prep_stmt->execute(); + } + } + status = {{"status", "success"}}; return status; }); From 9b565f21ecb5e824b531a41f1b9b5766d8c9a174 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 5 Jul 2025 23:02:08 -0400 Subject: [PATCH 7/7] use id of session just created for session id of song_practiced --- server.cpp | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/server.cpp b/server.cpp index 812f0d3..ff4f6f0 100644 --- a/server.cpp +++ b/server.cpp @@ -34,10 +34,10 @@ int main() connect(); crow::json::wvalue status; auto request_body = crow::json::load(req.body); - + int new_session_id; if(request_body.has("comments")){ - prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, comments, practice_category_id) VALUES (?, ?, ?, ?)"); + prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, comments, practice_category_id) VALUES (?, ?, ?, ?) RETURNING id"); string description = request_body["description"].s(); int seconds = request_body["seconds"].i(); @@ -48,11 +48,13 @@ int main() prep_stmt->setInt(2, seconds); prep_stmt->setString(3, comments); prep_stmt->setInt(4, practice_category_id); - prep_stmt->execute(); + ResultSet *res = prep_stmt->executeQuery(); + res->next(); + new_session_id = res->getInt("id"); } else { - prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, practice_category_id) VALUES (?, ?, ?)"); + prep_stmt = con->prepareStatement("INSERT INTO practice_sessions (description, seconds, practice_category_id) VALUES (?, ?, ?) RETURNING id"); string description = request_body["description"].s(); int seconds = request_body["seconds"].i(); @@ -61,14 +63,16 @@ int main() prep_stmt->setString(1, description); prep_stmt->setInt(2, seconds); prep_stmt->setInt(3, practice_category_id); - prep_stmt->execute(); + ResultSet *res = prep_stmt->executeQuery(); + res->next(); + new_session_id = res->getInt("id"); } if(request_body.has("songs")){ for(const auto& song : request_body["songs"]){ prep_stmt = con->prepareStatement("INSERT INTO songs_practiced (practice_session_id, song_id) VALUES (?, ?)"); - prep_stmt->setInt(1, 5377); + prep_stmt->setInt(1, new_session_id); prep_stmt->setInt(2, int(song["id"])); prep_stmt->execute(); }