Complain if doc exists

This commit is contained in:
Cyber MacGeddon 2025-05-04 19:52:13 +01:00
parent b0613fe0de
commit 4120829dd8
2 changed files with 35 additions and 5 deletions

View file

@ -37,6 +37,12 @@ class Librarian:
"Invalid document kind: " + request.document_metadata.kind
)
if self.table_store.document_exists(
request.document_metadata.user,
request.document_metadata.id
):
raise RuntimeError("Document already exists")
# Create object ID for blob
object_id = uuid.uuid4()

View file

@ -179,6 +179,20 @@ class TableStore:
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""")
self.get_document_stmt = self.cassandra.prepare("""
SELECT user, time, kind, title, comments,
metadata, tags, object_id
FROM document
WHERE user = ? AND id = ?
""")
self.test_document_exists_stmt = self.cassandra.prepare("""
SELECT id
FROM document
WHERE user = ? AND id = ?
LIMIT 1
""")
self.list_document_stmt = self.cassandra.prepare("""
SELECT
id, time, kind, title, comments, metadata, tags, object_id
@ -223,12 +237,22 @@ class TableStore:
VALUES (?, ?, ?, ?, ?, ?, ?)
""")
def add_document(self, document, object_id):
def document_exists(self, user, id):
# if document.kind not in (
# "text/plain", "application/pdf"
# ):
# raise RequestError("Invalid document kind: " + document.kind)
resp = self.cassandra.execute(
self.test_document_exists_stmt,
( user, id )
)
# If a row exists, document exists. It's a cursor, can't just
# count the length
for row in resp:
return True
return False
def add_document(self, document, object_id):
# Create timestamp
when = int(time.time() * 1000)