55
66from ..data import DATA_SOURCES
77from ..models import LatestResponse , LocationResponse , LocationsResponse
8+ from app import data
89
910V2 = APIRouter ()
1011
12+ '''
13+ Builder Pattern
14+ '''
15+ class Data :
16+ def __init__ (self , locations ):
17+ self .__data = {"latest" : {}}
18+ self .__locations = locations
19+
20+ def build_confirmed (self ):
21+ self .__data ["latest" ]["confirmed" ] = sum (map (lambda location : location .confirmed , self .__locations ))
22+ return self
23+
24+ def build_deaths (self ):
25+ self .__data ["latest" ]["deaths" ] = sum (map (lambda location : location .deaths , self .__locations ))
26+ return self
27+
28+ def build_recovered (self ):
29+ self .__data ["latest" ]["recovered" ] = sum (map (lambda location : location .recovered , self .__locations ))
30+ return self
31+
32+ def build_generic (self , key , data ):
33+ self .__data [key ] = data
34+ return self
35+
36+ def get_return (self ):
37+ return self .__data
1138
1239class Sources (str , enum .Enum ):
1340 """
@@ -27,13 +54,13 @@ async def get_latest(
2754 Getting latest amount of total confirmed cases, deaths, and recoveries.
2855 """
2956 locations = await request .state .source .get_all ()
30- return {
31- "latest" : {
32- "confirmed" : sum ( map ( lambda location : location . confirmed , locations )),
33- "deaths" : sum ( map ( lambda location : location . deaths , locations )),
34- "recovered" : sum ( map ( lambda location : location . recovered , locations )),
35- }
36- }
57+
58+
59+ return Data ( locations )\
60+ . build_deaths ()\
61+ . build_recovered ()\
62+ . build_confirmed ()\
63+ . get_return ()
3764
3865
3966# pylint: disable=unused-argument,too-many-arguments,redefined-builtin
@@ -80,15 +107,13 @@ async def get_locations(
80107 )
81108
82109 # Return final serialized data.
83- return {
84- "latest" : {
85- "confirmed" : sum (map (lambda location : location .confirmed , locations )),
86- "deaths" : sum (map (lambda location : location .deaths , locations )),
87- "recovered" : sum (map (lambda location : location .recovered , locations )),
88- },
89- "locations" : [location .serialize (timelines ) for location in locations ],
90- }
91110
111+ return Data (locations )\
112+ .build_deaths ()\
113+ .build_recovered ()\
114+ .build_confirmed ()\
115+ .build_generic ("locations" , [location .serialize (timelines ) for location in locations ])\
116+ .get_return ()
92117
93118# pylint: disable=invalid-name
94119@V2 .get ("/locations/{id}" , response_model = LocationResponse )
0 commit comments