Skip to content

Commit 7770152

Browse files
committed
check collection exists
1 parent 20ac8d9 commit 7770152

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ def get_item_collection(
8282
search = search.query(collection_filter)
8383

8484
count = self.search_count(search)
85-
85+
8686
# search = search.sort({"id.keyword" : {"order" : "asc"}})
8787
search = search.query()[0:limit]
8888
collection_children = search.execute().to_dict()
@@ -230,10 +230,14 @@ def execute_search(self, search, limit: int, base_url: str) -> List:
230230

231231
# Transaction Logic
232232

233-
def prep_create_item(self, item: stac_types.Item, base_url: str):
233+
def check_collection_exists(self, collection_id: str):
234+
"""Database logic to check if a collection exists."""
235+
if not self.client.exists(index=COLLECTIONS_INDEX, id=collection_id):
236+
raise ForeignKeyError(f"Collection {collection_id} does not exist")
237+
238+
def prep_create_item(self, item: stac_types.Item, base_url: str) -> stac_types.Item:
234239
"""Database logic for prepping an item for insertion."""
235-
if not self.client.exists(index=COLLECTIONS_INDEX, id=item["collection"]):
236-
raise ForeignKeyError(f"Collection {item['collection']} does not exist")
240+
self.check_collection_exists(collection_id=item["collection"])
237241

238242
if self.client.exists(
239243
index=ITEMS_INDEX, id=mk_item_id(item["id"], item["collection"])
@@ -260,8 +264,7 @@ def create_item(self, item: stac_types.Item, base_url: str):
260264

261265
def prep_update_item(self, item: stac_types.Item):
262266
"""Database logic for prepping an item for updating."""
263-
if not self.client.exists(index=COLLECTIONS_INDEX, id=item["collection"]):
264-
raise ForeignKeyError(f"Collection {item['collection']} does not exist")
267+
self.check_collection_exists(collection_id=item["collection"])
265268

266269
def delete_item(self, item_id: str, collection_id: str):
267270
"""Database logic for deleting one item."""

0 commit comments

Comments
 (0)