Sfoglia il codice sorgente

Add featured_articles table

jherve 1 anno fa
parent
commit
f1747d6bd3
1 ha cambiato i file con 36 aggiunte e 1 eliminazioni
  1. 36 1
      src/de_quoi_parle_le_monde/storage.py

+ 36 - 1
src/de_quoi_parle_le_monde/storage.py

@@ -2,7 +2,7 @@ import aiosqlite
 import asyncio
 from datetime import datetime
 
-from de_quoi_parle_le_monde.article import MainArticle, TopArticle, FeaturedArticleSnapshot
+from de_quoi_parle_le_monde.article import MainArticle, TopArticle, FeaturedArticleSnapshot, FeaturedArticle
 from de_quoi_parle_le_monde.internet_archive import InternetArchiveSnapshotId
 
 
@@ -79,6 +79,21 @@ class Storage:
                 """
             )
 
+            await conn.execute(
+                """
+                CREATE TABLE IF NOT EXISTS featured_articles (
+                    id INTEGER PRIMARY KEY AUTOINCREMENT,
+                    url TEXT
+                );
+                """
+            )
+            await conn.execute(
+                """
+                CREATE UNIQUE INDEX IF NOT EXISTS featured_articles_unique_url
+                ON featured_articles (url);
+                """
+            )
+
             await conn.execute(
                 """
                 CREATE TABLE IF NOT EXISTS featured_article_snapshots (
@@ -217,6 +232,26 @@ class Storage:
             await conn.commit()
             return id_
 
+    async def add_featured_article(self, article: FeaturedArticle):
+        async with self.conn as conn:
+            (id_,) = await conn.execute_insert(
+                self._insert_stmt("featured_articles", ["url"]),
+                [str(article.url)],
+            )
+
+            if id_ == 0:
+                [(id_,)] = await conn.execute_fetchall(
+                    """
+                    SELECT id
+                    FROM featured_articles
+                    WHERE url = ?
+                    """,
+                    [str(article.url)],
+                )
+
+            await conn.commit()
+            return id_
+
     async def add_featured_article_snapshot(self, article: FeaturedArticleSnapshot):
         async with self.conn as conn:
             (id_,) = await conn.execute_insert(