54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from loguru import logger
|
|
|
|
from chain_service.database.models.chain import Chain
|
|
from chain_service.repositories.chain import ChainRepository
|
|
from chain_service.dependencies.chain import get_chain_repository
|
|
|
|
from typing import Annotated
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
|
|
router = APIRouter(prefix="/chain")
|
|
ChainRepositoryDependency = Annotated[ChainRepository, Depends(get_chain_repository)]
|
|
|
|
|
|
@router.post("/")
|
|
async def chain_upsert_controller(
|
|
chain: Chain,
|
|
chain_repository: ChainRepositoryDependency,
|
|
):
|
|
try:
|
|
upserted_chain = await chain_repository.upsert(chain)
|
|
return upserted_chain
|
|
|
|
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")
|
|
|
|
|
|
@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")
|