chain get
This commit is contained in:
parent
1cd490a199
commit
99cef43899
|
|
@ -34,3 +34,20 @@ async def chain_list_controller(chain_repository: ChainRepositoryDependency):
|
||||||
except Exception:
|
except Exception:
|
||||||
logger.exception(f"Error during chain list")
|
logger.exception(f"Error during chain list")
|
||||||
return HTTPException(status_code=500, detail="Error during chain list")
|
return HTTPException(status_code=500, detail="Error during chain list")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{chain_id}")
|
||||||
|
async def chain_get_controller(
|
||||||
|
chain_id: str, chain_repository: ChainRepositoryDependency
|
||||||
|
):
|
||||||
|
try:
|
||||||
|
assert (chain := await chain_repository.get_by_id(chain_id))
|
||||||
|
return chain
|
||||||
|
|
||||||
|
except AssertionError:
|
||||||
|
logger.info(f"Chain not found {chain_id}")
|
||||||
|
return HTTPException(status_code=404, detail="Chain not found")
|
||||||
|
|
||||||
|
except Exception:
|
||||||
|
logger.exception("Error during chain get")
|
||||||
|
return HTTPException(status_code=500, detail="Error during chain get")
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import pymongo
|
import pymongo
|
||||||
|
from uuid import UUID
|
||||||
from typing import List
|
from typing import List
|
||||||
from pydantic import TypeAdapter
|
from pydantic import TypeAdapter
|
||||||
|
|
||||||
|
|
@ -21,3 +22,8 @@ class ChainRepository:
|
||||||
sort_order = [("last_modified", pymongo.DESCENDING)]
|
sort_order = [("last_modified", pymongo.DESCENDING)]
|
||||||
chains = [chain async for chain in self.collection.find().sort(sort_order)]
|
chains = [chain async for chain in self.collection.find().sort(sort_order)]
|
||||||
return TypeAdapter(List[Chain]).validate_python(chains)
|
return TypeAdapter(List[Chain]).validate_python(chains)
|
||||||
|
|
||||||
|
async def get_by_id(self, chain_id: str) -> Chain | None:
|
||||||
|
query = {"_id": UUID(chain_id)}
|
||||||
|
chain = await self.collection.find_one(query)
|
||||||
|
return Chain.model_validate(chain) if chain else None
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user