diff --git a/server.js b/server.js index c30f5ab..be1553f 100644 --- a/server.js +++ b/server.js @@ -43,5 +43,55 @@ app.get('/status', async (req, res)=>{ }) }) +app.get('/show-category/:id', async (req, res) => { + + const query = `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`; + + const results = await mysqlConnection.awaitQuery(query, [req.params.id]) + + const entries = []; + let previousSessionId = 0; + + results.forEach(row => { + if (row.id !== previousSessionId) { + entries.push({ + id:row.id, + description:row.description, + seconds:row.seconds, + comments:row.comments, + created_at:row.created_at, + category:row.category, + instrument:row.instrument, + songs:[] + }) + previousSessionId = row.id + } + if (row.song_id !== null) { + entries[entries.length-1].songs.push({ + id: row.song_id, + title: row.title + }); + } + + }); + + res.json(entries); +}); app.listen(18080)