Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions app/routers/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import enum

from fastapi import APIRouter, HTTPException, Request

from ..services import ServiceRoot
from ..data import DATA_SOURCES
from ..models import LatestResponse, LocationResponse, LocationsResponse

Expand All @@ -26,7 +26,11 @@ async def get_latest(
"""
Getting latest amount of total confirmed cases, deaths, and recoveries.
"""
locations = await request.state.source.get_all()

#service root for aggregate pattern
service = ServiceRoot(source)

locations = await service.get_all()
return {
"latest": {
"confirmed": sum(map(lambda location: location.confirmed, locations)),
Expand Down Expand Up @@ -56,8 +60,10 @@ async def get_locations(
params.pop("source", None)
params.pop("timelines", None)

service = ServiceRoot(source)

# Retrieve all the locations.
locations = await request.state.source.get_all()
locations = await service.get_all()

# Attempt to filter out locations with properties matching the provided query params.
for key, value in params.items():
Expand Down Expand Up @@ -98,7 +104,12 @@ async def get_location_by_id(
"""
Getting specific location by id.
"""
location = await request.state.source.get(id)

#aggregate root for service layer
service = ServiceRoot(source)

# Retrieve location.
location = await service.get(id)
return {"location": location.serialize(timelines)}


Expand Down
26 changes: 26 additions & 0 deletions app/services/location/service_root.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from jhu import JHULocationService
from nyt import NYTLocationService
from csbs import CSBSLocationService

class ServiceRoot:

def __init__(self, source, service):
if source == '' or source == None or source == 'jhu':
self.source = 'jhu'
self.service = JHULocationService()
elif source == 'nyt':
self.source = source
self.service = NYTLocationService()
elif source == 'csbs':
self.source = source
self.service = CSBSLocationService()
else
self.source = None

async def get_all(self):
locations = await self.service.get_all()
return locations

async def get(self, id):
location = await self.service.get(id)
return location