chain list
This commit is contained in:
parent
ece53f00db
commit
c6e2dd3641
|
|
@ -23,3 +23,14 @@ async def chain_upsert_controller(
|
|||
except Exception:
|
||||
logger.exception(f"Error during chain upsert {chain.model_dump_json()}")
|
||||
return HTTPException(status_code=500, detail="Error during chain upsert")
|
||||
|
||||
|
||||
@router.get("/list")
|
||||
async def chain_list_controller(chain_repository: ChainRepositoryDependency):
|
||||
try:
|
||||
chains = await chain_repository.get_list()
|
||||
return chains
|
||||
|
||||
except Exception:
|
||||
logger.exception(f"Error during chain list")
|
||||
return HTTPException(status_code=500, detail="Error during chain list")
|
||||
|
|
|
|||
|
|
@ -1,3 +1,7 @@
|
|||
import pymongo
|
||||
from typing import List
|
||||
from pydantic import TypeAdapter
|
||||
|
||||
from chain_service.database.database import Database
|
||||
from chain_service.database.models.chain import Chain
|
||||
|
||||
|
|
@ -8,8 +12,13 @@ class ChainRepository:
|
|||
self.collection = database.get_collection("chains")
|
||||
|
||||
async def upsert(self, chain: Chain) -> Chain:
|
||||
await self.collection.replace_one(
|
||||
{"_id": chain.id}, chain.model_dump(by_alias=True), upsert=True
|
||||
)
|
||||
query = {"_id": chain.id}
|
||||
payload = chain.model_dump(by_alias=True)
|
||||
|
||||
await self.collection.replace_one(query, payload, upsert=True)
|
||||
return chain
|
||||
|
||||
async def get_list(self) -> List[Chain]:
|
||||
sort_order = [("last_modified", pymongo.DESCENDING)]
|
||||
chains = [chain async for chain in self.collection.find().sort(sort_order)]
|
||||
return TypeAdapter(List[Chain]).validate_python(chains)
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user