delete chain logic

This commit is contained in:
Robert 2024-03-08 22:45:54 +07:00
parent 0806626f51
commit 3f11da083d
No known key found for this signature in database
GPG Key ID: F631C7FD957D5F22
2 changed files with 23 additions and 3 deletions

View File

@ -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")

View File

@ -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")