84 lines
1.7 KiB
Python
84 lines
1.7 KiB
Python
import psycopg2.extras
|
|
from psycopg2._psycopg import connection
|
|
|
|
# feed create and delete
|
|
|
|
def create_feed(
|
|
conn: connection,
|
|
groupname: str,
|
|
image_ids: list[int]
|
|
):
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
insert into picrinth.feeds
|
|
(groupname, image_ids, created_at)
|
|
values (%s, %s, now())
|
|
returning id
|
|
""",
|
|
(groupname, image_ids),
|
|
)
|
|
result = cur.fetchone()
|
|
conn.commit()
|
|
if result is None:
|
|
return None
|
|
return result[0]
|
|
|
|
|
|
def delete_feed(
|
|
conn: connection,
|
|
feed_id: int
|
|
):
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
"""
|
|
delete from picrinth.feeds
|
|
where 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 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 id = %s
|
|
""",
|
|
(feed_id,),
|
|
)
|
|
result = cur.fetchone()
|
|
if result is None:
|
|
return None
|
|
return cur.fetchone()[0] # type: ignore
|