Coverage for mpcforces_extractor\api\routes\mpcs.py: 61%

18 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-11-06 21:34 +0100

1from typing import List 

2from fastapi import APIRouter, Depends, HTTPException, status 

3from mpcforces_extractor.api.db.database import MPCDBModel 

4from mpcforces_extractor.api.dependencies import get_db 

5 

6router = APIRouter() 

7 

8 

9# API endpoint to get all MPCs 

10@router.get("", response_model=List[MPCDBModel]) 

11async def get_mpcs(db=Depends(get_db)) -> List[MPCDBModel]: 

12 """Get all MPCs""" 

13 

14 return await db.get_mpcs() 

15 

16 

17# API endpoint to get a specific MPC by ID 

18@router.get("/{mpc_id}", response_model=MPCDBModel) 

19async def get_mpc(mpc_id: int, db=Depends(get_db)) -> MPCDBModel: 

20 """Get info about a specific MPC""" 

21 

22 mpc = await db.get_mpc(mpc_id) 

23 if mpc is None: 

24 raise HTTPException( 

25 status_code=status.HTTP_404_NOT_FOUND, 

26 detail=f"MPC with id: {mpc_id} does not exist", 

27 ) 

28 

29 return mpc 

30 

31 

32# API endpoint to remove an MPC by ID 

33@router.delete("/{mpc_id}") 

34async def remove_mpc(mpc_id: int, db=Depends(get_db)): 

35 """Remove an MPC""" 

36 await db.remove_mpc(mpc_id) 

37 return {"message": f"MPC with id: {mpc_id} removed"}