feed WIP
This commit is contained in:
80
src/db/feeds.py
Normal file
80
src/db/feeds.py
Normal file
@ -0,0 +1,80 @@
|
||||
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
|
||||
@ -243,6 +243,7 @@ def get_groupname_by_invite_code(
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
|
||||
def get_group_invite_code(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
@ -260,3 +261,21 @@ def get_group_invite_code(
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
def get_group_last_feed_id(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select last_feed_id
|
||||
from picrinth.groups
|
||||
where groupname = %s
|
||||
""",
|
||||
(groupname,),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0] # type: ignore
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# membership create and delete
|
||||
@ -65,7 +64,7 @@ def get_memberships_by_username(
|
||||
conn: connection,
|
||||
username: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
@ -81,7 +80,7 @@ def get_memberships_by_groupname(
|
||||
conn: connection,
|
||||
groupname: str
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
|
||||
97
src/db/swipes.py
Normal file
97
src/db/swipes.py
Normal file
@ -0,0 +1,97 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
|
||||
# swipe create and delete
|
||||
|
||||
def create_swipe(
|
||||
conn: connection,
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int,
|
||||
value: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
insert into picrinth.swipes
|
||||
(username, feed_id, picture_id, value, created_at)
|
||||
values (%s, %s, %s, %s, now())
|
||||
returning id
|
||||
""",
|
||||
(username, feed_id, picture_id, value),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
def delete_swipe(
|
||||
conn: connection,
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
delete from picrinth.swipes
|
||||
where username = %s and picture_id = %s and feed_id = %s
|
||||
""",
|
||||
(username, picture_id, feed_id),
|
||||
)
|
||||
conn.commit()
|
||||
return cur.rowcount > 0
|
||||
|
||||
|
||||
# swipe receiving
|
||||
|
||||
def get_swipe(
|
||||
conn: connection,
|
||||
username: str,
|
||||
feed_id: int,
|
||||
picture_id: int
|
||||
):
|
||||
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select username,
|
||||
feed_id, picture_id,
|
||||
value, created_at
|
||||
from picrinth.swipes
|
||||
where username = %s and feed_id = %s and picture_id = %s
|
||||
""",
|
||||
(username, feed_id, picture_id),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
|
||||
def get_swipes_by_picture_id(
|
||||
conn: connection,
|
||||
feed_id: int,
|
||||
picture_id: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
from picrinth.swipes
|
||||
where feed_id = %s and picture_id = %s
|
||||
""",
|
||||
(feed_id, picture_id),
|
||||
)
|
||||
return cur.fetchone()
|
||||
|
||||
def get_swipes_by_user(
|
||||
conn: connection,
|
||||
username: str,
|
||||
feed_id: int
|
||||
):
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(
|
||||
"""
|
||||
select *
|
||||
from picrinth.swipes
|
||||
where username = %s and feed_id = %s
|
||||
""",
|
||||
(username, feed_id),
|
||||
)
|
||||
return cur.fetchone()
|
||||
Reference in New Issue
Block a user