feature: now using uv + moved to asyncpg from psycopg2
This commit is contained in:
+57
-69
@@ -1,100 +1,88 @@
|
||||
import psycopg2.extras
|
||||
from psycopg2._psycopg import connection
|
||||
from asyncpg import Connection
|
||||
|
||||
# swipe create and delete
|
||||
|
||||
def create_swipe(
|
||||
conn: connection,
|
||||
async 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),
|
||||
)
|
||||
result = cur.fetchone()
|
||||
conn.commit()
|
||||
if result is None:
|
||||
return None
|
||||
return result[0]
|
||||
result = await conn.fetchrow(
|
||||
"""
|
||||
insert into picrinth.swipes
|
||||
(username, feed_id, picture_id, value, created_at)
|
||||
values ($1, $2, $3, $4, now())
|
||||
returning id
|
||||
""",
|
||||
username, feed_id, picture_id, value,
|
||||
)
|
||||
if result is None:
|
||||
return None
|
||||
return result["id"]
|
||||
|
||||
|
||||
def delete_swipe(
|
||||
conn: connection,
|
||||
async 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
|
||||
result = await conn.execute(
|
||||
"""
|
||||
delete from picrinth.swipes
|
||||
where username = $1 and picture_id = $2 and feed_id = $3
|
||||
""",
|
||||
username, picture_id, feed_id,
|
||||
)
|
||||
return result != "DELETE 0"
|
||||
|
||||
|
||||
# swipe receiving
|
||||
|
||||
def get_swipe(
|
||||
conn: connection,
|
||||
async 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()
|
||||
return await conn.fetchrow(
|
||||
"""
|
||||
select username,
|
||||
feed_id, picture_id,
|
||||
value, created_at
|
||||
from picrinth.swipes
|
||||
where username = $1 and feed_id = $2 and picture_id = $3
|
||||
""",
|
||||
username, feed_id, picture_id,
|
||||
)
|
||||
|
||||
|
||||
def get_swipes_by_picture_id(
|
||||
conn: connection,
|
||||
async 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()
|
||||
return await conn.fetchrow(
|
||||
"""
|
||||
select *
|
||||
from picrinth.swipes
|
||||
where feed_id = $1 and picture_id = $2
|
||||
""",
|
||||
feed_id, picture_id,
|
||||
)
|
||||
|
||||
def get_swipes_by_user(
|
||||
conn: connection,
|
||||
async 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()
|
||||
return await conn.fetchrow(
|
||||
"""
|
||||
select *
|
||||
from picrinth.swipes
|
||||
where username = $1 and feed_id = $2
|
||||
""",
|
||||
username, feed_id,
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user