diff --git a/chain_service/controllers/chain.py b/chain_service/controllers/chain.py index c34d3d8..2ac9747 100644 --- a/chain_service/controllers/chain.py +++ b/chain_service/controllers/chain.py @@ -53,3 +53,16 @@ async def chain_get_controller( except Exception: logger.exception("Error during chain get") return HTTPException(status_code=500, detail="Error during chain get") + + +@router.delete("/delete/{chain_id}") +async def chain_delete_controller( + chain_id: str, chain_repository: ChainRepositoryDependency +): + try: + await chain_repository.delete_by_id(chain_id) + return {} + + except Exception: + logger.exception("Error during chain deletion") + return HTTPException(status_code=500, detail="Error during chain deletion") diff --git a/chain_service/repositories/chain.py b/chain_service/repositories/chain.py index ac3af11..08f571a 100644 --- a/chain_service/repositories/chain.py +++ b/chain_service/repositories/chain.py @@ -1,6 +1,5 @@ from loguru import logger -import pymongo from uuid import UUID from typing import List from pydantic import TypeAdapter @@ -21,8 +20,8 @@ class ChainRepository: return chain async def get_list(self) -> List[Chain]: - sort_order = ("lastModified", pymongo.DESCENDING) - chains = [chain async for chain in self.collection.find().sort(*sort_order)] + # sort_order = ("lastModified", pymongo.DESCENDING) + chains = [chain async for chain in self.collection.find()] return TypeAdapter(List[Chain]).validate_python(chains) async def get_by_id(self, chain_id: str) -> Chain | None: @@ -33,3 +32,11 @@ class ChainRepository: except ValueError: logger.error(f"Cannot convert {chain_id} to UUID") + + async def delete_by_id(self, chain_id: str): + try: + query = {"_id": UUID(chain_id)} + await self.collection.delete_one(query) + + except ValueError: + logger.error(f"Cannot convert {chain_id} to UUID")