]> uap-core.de Git - rssreader.git/commitdiff
add feed list from database
authorOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 12 Aug 2025 10:59:36 +0000 (12:59 +0200)
committerOlaf Wintermann <olaf.wintermann@gmail.com>
Tue, 12 Aug 2025 10:59:36 +0000 (12:59 +0200)
rss-application/src/main/kotlin/de/unixwork/rssreader/Database.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedGroup.kt
rss-application/src/main/kotlin/de/unixwork/rssreader/FeedSourceList.kt

index af002faa106dc493a79d1991fc5d3cc1d50cf4f0..410e21d4addd27478d00ad8ec6f9ea21a1cf3020 100644 (file)
@@ -30,6 +30,7 @@ object Database {
                 createStmt.addBatch("""
                     CREATE TABLE groups (
                         group_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
+                        pos INT default 0,
                         name VARCHAR
                     )
                 """.trimIndent())
@@ -38,6 +39,7 @@ object Database {
                     CREATE TABLE feedcollections (
                         feedcollection_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
                         group_id INT NOT NULL REFERENCES groups(group_id) ON DELETE CASCADE,
+                        pos INT default 0,
                         name VARCHAR,
                         update_interval INT
                     )
@@ -72,4 +74,39 @@ object Database {
             println("Database schema already exists.")
         }
     }
+
+    public fun getFeedTree() : List<FeedGroup> {
+        val groups = mutableListOf<FeedGroup>()
+
+        connection.createStatement().use { stmt ->
+            val rs = stmt.executeQuery("""
+                select
+                    g.group_id,
+                    g.name as group_name,
+                    f.feedcollection_id,
+                    f.name as feed_name
+                from groups g
+                left join feedcollections f on g.group_id = f.group_id
+                order by g.pos, f.pos
+            """.trimIndent())
+
+            var currentGroup: FeedGroup? = null
+            while(rs.next()) {
+                val groupId = rs.getInt("group_id")
+                val groupName = rs.getString("group_name")
+                val feedId = rs.getInt("feedcollection_id")
+                val feedName = rs.getString("feed_name")
+
+                if(currentGroup == null || currentGroup.id != groupId) {
+                    currentGroup = FeedGroup(groupId, groupName)
+                    groups.add(currentGroup)
+                }
+
+                currentGroup.feeds.add(FeedCollection(feedId, feedName))
+            }
+            rs.close()
+        }
+
+        return groups
+    }
 }
index dc597f3d66d9f23744e439c835314410d70c299f..7ed60629909d579840543b5e4603c0d20fef346d 100644 (file)
@@ -3,4 +3,6 @@ package de.unixwork.rssreader
 class FeedGroup(id: Int, name: String) {
     val id = id
     val name: String = name
+
+    val feeds = mutableListOf<FeedCollection>()
 }
\ No newline at end of file
index fb5c2da9532bf0e7fef2519541c3f0608f2ea5e8..10b468c0a1e32cec909c6dca59f38f7720472917 100644 (file)
@@ -9,13 +9,16 @@ class FeedSourceList : Document() {
     init {
         val db = Database
 
-        // testcode
-        val sublist1 = SubList<FeedCollection>()
-        sublist1.header = "Test 1"
-        val sublist1data = list<FeedCollection>("sublist1data")
-        val col1 = FeedCollection("Test Feed")
-        sublist1data.add(col1)
-        sublist1.value = sublist1data
-        feeds.add(sublist1)
+        val groups = db.getFeedTree()
+        groups.forEach {
+            val sublist = SubList<FeedCollection>()
+            sublist.header = it.name
+            val feedlist = list<FeedCollection>()
+            it.feeds.forEach { fc ->
+                feedlist.add(fc)
+            }
+            sublist.value = feedlist
+            feeds.add(sublist)
+        }
     }
 }
\ No newline at end of file