Merge pull request 'practiced_songs' (#1) from practiced_songs into master

Reviewed-on: #1
master
mahuntington 6 months ago
commit ccd9fb8081

@ -34,10 +34,10 @@ int main()
connect(); connect();
crow::json::wvalue status; crow::json::wvalue status;
auto request_body = crow::json::load(req.body); auto request_body = crow::json::load(req.body);
int new_session_id;
if(request_body.has("comments")){ 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(); string description = request_body["description"].s();
int seconds = request_body["seconds"].i(); int seconds = request_body["seconds"].i();
@ -48,11 +48,13 @@ int main()
prep_stmt->setInt(2, seconds); prep_stmt->setInt(2, seconds);
prep_stmt->setString(3, comments); prep_stmt->setString(3, comments);
prep_stmt->setInt(4, practice_category_id); prep_stmt->setInt(4, practice_category_id);
prep_stmt->execute(); ResultSet *res = prep_stmt->executeQuery();
res->next();
new_session_id = res->getInt("id");
} else { } 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(); string description = request_body["description"].s();
int seconds = request_body["seconds"].i(); int seconds = request_body["seconds"].i();
@ -61,8 +63,19 @@ int main()
prep_stmt->setString(1, description); prep_stmt->setString(1, description);
prep_stmt->setInt(2, seconds); prep_stmt->setInt(2, seconds);
prep_stmt->setInt(3, practice_category_id); 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"}}; status = {{"status", "success"}};
@ -166,32 +179,62 @@ int main()
connect(); connect();
crow::json::wvalue entries; 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"); int id = res->getInt("id");
string description = res->getString("description"); string description = res->getString("description");
int seconds = res->getInt("seconds"); int seconds = res->getInt("seconds");
string comments = res->getString("comments"); string comments = res->getString("comments");
string created_at = res->getString("created_at"); string created_at = res->getString("created_at");
int category_id = res->getInt("category_id");
string category = res->getString("category"); string category = res->getString("category");
string instrument = res->getString("instrument"); string instrument = res->getString("instrument");
entries[i] = { int song_id = res->getInt("song_id");
{"id", id}, string song_title = res->getString("title");
{"description", description},
{"seconds", seconds}, if(id != previous_session_id){
{"comments", comments}, i++;
{"created_at", created_at},
{"category_id", category_id}, previous_session_id = id;
{"category", category}, current_song_index = 0;
{"instrument", instrument} crow::json::wvalue songs = crow::json::wvalue::list();
}; if(song_id != 0){
i++; 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; delete res;
return entries; return entries;
}); });

Loading…
Cancel
Save