Browse Source

Add featured_articles table

jherve 1 year ago
parent
commit
43b4d7f899
1 changed files with 45 additions and 0 deletions
  1. 45 0
      src/de_quoi_parle_le_monde/storage.py

+ 45 - 0
src/de_quoi_parle_le_monde/storage.py

@@ -49,6 +49,22 @@ class Storage:
                 """
             )
 
+            await conn.execute(
+                """
+                CREATE TABLE IF NOT EXISTS featured_articles (
+                    id INTEGER PRIMARY KEY AUTOINCREMENT,
+                    title TEXT,
+                    url TEXT
+                );
+                """
+            )
+            await conn.execute(
+                """
+                CREATE UNIQUE INDEX IF NOT EXISTS featured_articles_unique_idx_title_url
+                ON featured_articles (title, url);
+                """
+            )
+
             await conn.execute(
                 """
                 CREATE TABLE IF NOT EXISTS main_articles (
@@ -166,6 +182,26 @@ class Storage:
             await conn.commit()
             return id_
 
+    async def add_featured_article(self, url, title):
+        async with aiosqlite.connect(self.conn_str) as conn:
+            (id_,) = await conn.execute_insert(
+                self._insert_stmt("featured_articles", ["title", "url"]),
+                [title, url],
+            )
+
+            if id_ == 0:
+                [(id_,)] = await conn.execute_fetchall(
+                    """
+                    SELECT id
+                    FROM featured_articles
+                    WHERE title = ? AND url = ?
+                    """,
+                    [title, url],
+                )
+
+            await conn.commit()
+            return id_
+
     async def add_main_article(self, snapshot_id: int, article: MainArticle):
         async with aiosqlite.connect(self.conn_str) as conn:
             await conn.execute_insert(
@@ -184,6 +220,15 @@ class Storage:
             )
             await conn.commit()
 
+    async def select_from(self, table):
+        async with aiosqlite.connect(self.conn_str) as conn:
+            return await conn.execute_fetchall(
+                f"""
+                    SELECT *
+                    FROM {table}
+                """,
+            )
+
     @staticmethod
     def _insert_stmt(table, cols):
         cols_str = ", ".join(cols)