Files
picrinth-server/src/db/feeds.py
2025-07-31 20:15:48 +03:00

81 lines
1.7 KiB
Python

import psycopg2.extras
from psycopg2._psycopg import connection
# feed create and delete
def create_feed(
conn: connection,
groupname: int,
image_ids: list[int]
):
with conn.cursor() as cur:
cur.execute(
"""
insert into picrinth.feeds
(groupname, image_ids, created_at)
values (%s, %s, %s, %s, now())
returning id
""",
(groupname, image_ids),
)
conn.commit()
return cur.rowcount > 0
def delete_feed(
conn: connection,
feed_id: int
):
with conn.cursor() as cur:
cur.execute(
"""
delete from picrinth.feeds
where feed_id = %s
""",
(feed_id,),
)
conn.commit()
return cur.rowcount > 0
# feed receiving
def get_feed(
conn: connection,
feed_id: int
):
with conn.cursor() as cur:
cur.execute(
"""
select groupname
from picrinth.feeds
where feed_id = %s
""",
(feed_id,),
)
result = cur.fetchone()
if result is None:
return None
return cur.fetchone()[0] # type: ignore
# additional
def get_groupname_by_feed_id(
conn: connection,
feed_id: int
):
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute(
"""
select groupname
from picrinth.feeds
where feed_id = %s
""",
(feed_id,),
)
result = cur.fetchone()
if result is None:
return None
return cur.fetchone()[0] # type: ignore