diff --git a/server.cpp b/server.cpp index 33c0a87..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,8 +63,19 @@ 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, new_session_id); + prep_stmt->setInt(2, int(song["id"])); + prep_stmt->execute(); + } } status = {{"status", "success"}}; @@ -166,32 +179,62 @@ int main() connect(); crow::json::wvalue entries; - 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 = -1; + int previous_session_id = 0; + int current_song_index; + while (res->next()) { - int i = 0; - res->afterLast(); - while (res->previous()) { int id = res->getInt("id"); string description = res->getString("description"); 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] = { - {"id", id}, - {"description", description}, - {"seconds", seconds}, - {"comments", comments}, - {"created_at", created_at}, - {"category_id", category_id}, - {"category", category}, - {"instrument", instrument} - }; - i++; - } + int song_id = res->getInt("song_id"); + string song_title = res->getString("title"); + + 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; });