Skip to content

Commit

Permalink
Add ability to get source by id
Browse files Browse the repository at this point in the history
  • Loading branch information
CannonLock committed Dec 21, 2023
1 parent f85df51 commit 5773e7d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 22 additions & 2 deletions api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import uvicorn
from fastapi import FastAPI, HTTPException, Response, status, Depends
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import select
from sqlalchemy.exc import NoResultFound, NoSuchTableError
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
from starlette.authentication import requires

import dotenv
dotenv.load_dotenv()
Expand All @@ -29,6 +28,8 @@
from api.routes.security import TokenData, get_groups
from api.routes.object import router as object_router

import api.schemas as schemas

@asynccontextmanager
async def setup_engine(a: FastAPI):
"""Return database client instance."""
Expand Down Expand Up @@ -78,6 +79,25 @@ async def get_sources(response: Response, page: int = 0, page_size: int = 100, i
return sources


@app.get("/sources/{source_id}")
async def get_source(source_id: int, include_geom: bool = False) -> Sources:
"""Get a single object"""

engine = get_engine()
async_session = get_async_session(engine)

async with async_session() as session:

select_stmt = select(*[c for c in schemas.Sources.__table__.c if c.name not in ['rgeom', 'webgeom']]).where(schemas.Sources.source_id == source_id)

results = await session.execute(select_stmt)

if results is None:
raise HTTPException(status_code=404, detail=f"Object with id ({id}) not found")

return db.results_to_model(results, Sources)[0]


@app.get("/sources/{table_id}/polygons", response_model=List[PolygonModel])
async def get_sub_sources(
response: Response,
Expand Down
9 changes: 9 additions & 0 deletions api/tests/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,15 @@ def test_get_sources(self, api_client: TestClient):
assert response.status_code == 200


def test_get_source(self, api_client: TestClient):
response = api_client.get(f"/sources/{TEST_SOURCE_TABLE.source_id}")
assert response.status_code == 200

response_json = response.json()

assert response_json["source_id"] == TEST_SOURCE_TABLE.source_id


def test_get_sources_tables(self, api_client: TestClient):
response = api_client.get(f"/sources/{TEST_SOURCE_TABLE.source_id}/polygons")
assert response.status_code == 200
Expand Down

0 comments on commit 5773e7d

Please sign in to comment.