added functional groups api + started pictures

This commit is contained in:
2025-07-30 19:10:10 +03:00
parent c203a890dc
commit 3341d68c7e
20 changed files with 1103 additions and 120 deletions

97
src/db/pictures.py Normal file
View File

@ -0,0 +1,97 @@
import json
import psycopg2.extras
from psycopg2._psycopg import connection
# picture create and delete
def create_picture(
conn: connection,
source: str,
external_id: str,
url: str,
metadata: dict
):
with conn.cursor() as cur:
cur.execute(
"""
insert into picrinth.pictures
(source, external_id, url, metadata, created_at)
values (%s, %s, %s, %s, now())
returning id
""",
(source, external_id, url, json.dumps(metadata)),
)
result = cur.fetchone()
conn.commit()
if result is None:
return None
return result[0]
def delete_picture_by_url(
conn: connection,
url: str
):
with conn.cursor() as cur:
cur.execute(
"""
delete from picrinth.pictures
where url = %s
""",
(url,),
)
conn.commit()
return cur.rowcount > 0
def delete_picture_by_id(
conn: connection,
id: int
):
with conn.cursor() as cur:
cur.execute(
"""
delete from picrinth.pictures
where id = %s
""",
(id,),
)
conn.commit()
return cur.rowcount > 0
# picture receiving
def get_picture_by_url(
conn: connection,
url: str
):
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute(
"""
select id, source,
external_id, url,
metadata, created_at
from picrinth.pictures
where url = %s
""",
(url,),
)
return cur.fetchone()
def get_picture_by_id(
conn: connection,
id: str
):
with conn.cursor(cursor_factory=psycopg2.extras.DictCursor) as cur:
cur.execute(
"""
select id, source,
external_id, url,
metadata, created_at
from picrinth.pictures
where id = %s
""",
(id,),
)
return cur.fetchone()