From 2f195abbb5c690607054a31e3a5bd96291336c07 Mon Sep 17 00:00:00 2001 From: Benjamin Bolte Date: Sun, 16 Jun 2024 10:21:41 -0700 Subject: [PATCH] fix tests --- linguaphoto/main.py | 4 -- linguaphoto/routers/part.py | 72 ------------------------------------ linguaphoto/routers/robot.py | 72 ------------------------------------ 3 files changed, 148 deletions(-) delete mode 100644 linguaphoto/routers/part.py delete mode 100644 linguaphoto/routers/robot.py diff --git a/linguaphoto/main.py b/linguaphoto/main.py index 0ad5a48..7f71822 100644 --- a/linguaphoto/main.py +++ b/linguaphoto/main.py @@ -4,8 +4,6 @@ from fastapi.middleware.cors import CORSMiddleware from fastapi.responses import JSONResponse -from linguaphoto.routers.part import parts_router -from linguaphoto.routers.robot import robots_router from linguaphoto.routers.users import users_router from linguaphoto.settings import settings @@ -30,5 +28,3 @@ async def value_error_exception_handler(request: Request, exc: ValueError) -> JS app.include_router(users_router, prefix="/users", tags=["users"]) -app.include_router(robots_router, prefix="/robots", tags=["robots"]) -app.include_router(parts_router, prefix="/parts", tags=["parts"]) diff --git a/linguaphoto/routers/part.py b/linguaphoto/routers/part.py deleted file mode 100644 index 4e05c23..0000000 --- a/linguaphoto/routers/part.py +++ /dev/null @@ -1,72 +0,0 @@ -"""Defines all part-related API endpoints.""" - -import logging -from typing import Annotated, List - -from fastapi import APIRouter, Depends, HTTPException - -from linguaphoto.crypto import get_new_user_id -from linguaphoto.db import Crud -from linguaphoto.model import Part -from linguaphoto.routers.users import ApiKeyData, get_api_key - -parts_router = APIRouter() - - -logger = logging.getLogger(__name__) - - -@parts_router.get("/") -async def list_parts(crud: Annotated[Crud, Depends(Crud.get)]) -> List[Part]: - return await crud.list_parts() - - -@parts_router.get("/your/") -async def list_your_parts( - crud: Annotated[Crud, Depends(Crud.get)], data: Annotated[ApiKeyData, Depends(get_api_key)] -) -> List[Part]: - try: - user_id = await crud.get_user_id_from_api_key(data.api_key) - if user_id is None: - raise HTTPException(status_code=401, detail="Must be logged in to view your robots") - total = await crud.list_parts() - user_parts = [part for part in total if str(part.owner) == str(user_id)] - return user_parts - except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) - - -@parts_router.get("/{part_id}") -async def get_part(part_id: str, crud: Annotated[Crud, Depends(Crud.get)]) -> Part | None: - return await crud.get_part(part_id) - - -@parts_router.post("/add/") -async def add_part( - part: Part, - data: Annotated[ApiKeyData, Depends(get_api_key)], - crud: Annotated[Crud, Depends(Crud.get)], -) -> bool: - user_id = await crud.get_user_id_from_api_key(data.api_key) - if user_id is None: - raise HTTPException(status_code=401, detail="Must be logged in to add a part") - part.owner = str(user_id) - part.part_id = str(get_new_user_id()) - await crud.add_part(part) - return True - - -@parts_router.delete("/delete/{part_id}") -async def delete_part( - part_id: str, - data: Annotated[ApiKeyData, Depends(get_api_key)], - crud: Annotated[Crud, Depends(Crud.get)], -) -> bool: - part = await crud.get_part(part_id) - if part is None: - raise HTTPException(status_code=404, detail="Part not found") - user_id = await crud.get_user_id_from_api_key(data.api_key) - if part.owner != user_id: - raise HTTPException(status_code=403, detail="You do not own this part") - await crud.delete_part(part_id) - return True diff --git a/linguaphoto/routers/robot.py b/linguaphoto/routers/robot.py deleted file mode 100644 index a492ceb..0000000 --- a/linguaphoto/routers/robot.py +++ /dev/null @@ -1,72 +0,0 @@ -"""Defines all robot related API endpoints.""" - -import logging -from typing import Annotated, List - -from fastapi import APIRouter, Depends, HTTPException - -from linguaphoto.crypto import get_new_user_id -from linguaphoto.db import Crud -from linguaphoto.model import Robot -from linguaphoto.routers.users import ApiKeyData, get_api_key - -robots_router = APIRouter() - - -logger = logging.getLogger(__name__) - - -@robots_router.get("/") -async def list_robots(crud: Annotated[Crud, Depends(Crud.get)]) -> List[Robot]: - return await crud.list_robots() - - -@robots_router.get("/your/") -async def list_your_robots( - crud: Annotated[Crud, Depends(Crud.get)], data: Annotated[ApiKeyData, Depends(get_api_key)] -) -> List[Robot]: - try: - user_id = await crud.get_user_id_from_api_key(data.api_key) - if user_id is None: - raise HTTPException(status_code=401, detail="Must be logged in to view your robots") - total = await crud.list_robots() - user_robots = [robot for robot in total if str(robot.owner) == str(user_id)] - return user_robots - except Exception as e: - raise HTTPException(status_code=500, detail=str(e)) - - -@robots_router.get("/{robot_id}") -async def get_robot(robot_id: str, crud: Annotated[Crud, Depends(Crud.get)]) -> Robot | None: - return await crud.get_robot(robot_id) - - -@robots_router.post("/add/") -async def add_robot( - robot: Robot, - data: Annotated[ApiKeyData, Depends(get_api_key)], - crud: Annotated[Crud, Depends(Crud.get)], -) -> bool: - user_id = await crud.get_user_id_from_api_key(data.api_key) - if user_id is None: - raise HTTPException(status_code=401, detail="Must be logged in to add a robot") - robot.owner = str(user_id) - robot.robot_id = str(get_new_user_id()) - await crud.add_robot(robot) - return True - - -@robots_router.delete("/delete/{robot_id}") -async def delete_robot( - robot_id: str, - data: Annotated[ApiKeyData, Depends(get_api_key)], - crud: Annotated[Crud, Depends(Crud.get)], -) -> bool: - robot = await crud.get_robot(robot_id) - if robot is None: - raise HTTPException(status_code=404, detail="Robot not found") - user_id = await crud.get_user_id_from_api_key(data.api_key) - if robot.owner != user_id: - raise HTTPException(status_code=403, detail="You do not own this robot") - await crud.delete_robot(robot_id) - return True