Skip to content

Commit

Permalink
feat: ajout endpoint /zones/all/categories pour avoir la liste des ca…
Browse files Browse the repository at this point in the history
…tégories disponibles
  • Loading branch information
herve.le-bars committed Jun 7, 2024
1 parent 16f0615 commit 2897a06
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
16 changes: 16 additions & 0 deletions backend/bloom/infra/repositories/repository_zone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import Any, List, Union

from bloom.domain.zone import Zone
from bloom.domain.zone_category import ZoneCategory
from bloom.infra.database import sql_model
from dependency_injector.providers import Callable
from geoalchemy2.shape import from_shape, to_shape
Expand All @@ -20,10 +21,19 @@ def get_zone_by_id(self, session: Session, zone_id: int) -> Union[Zone, None]:

def get_all_zones(self, session: Session) -> List[Zone]:
q = session.query(sql_model.Zone)
q=session.execute(q).scalars()
if not q:
return []
return [ZoneRepository.map_to_domain(entity) for entity in q]

def get_all_zone_categories(self, session: Session) -> List[Zone]:
q = session.query(sql_model.Zone.category,
sql_model.Zone.sub_category).distinct()
q=session.execute(q)
if not q:
return []
return [ZoneRepository.map_to_domain(ZoneCategory(category=cat,sub_category=sub)) for cat,sub in q]

def get_all_zones_by_category(self, session: Session,category:str) -> List[Zone]:
q = session.query(sql_model.Zone).where(sql_model.Zone.category == category)
if not q:
Expand Down Expand Up @@ -60,3 +70,9 @@ def map_to_domain(zone: sql_model.Zone) -> Zone:
json_data=zone.json_data,
created_at=zone.created_at,
)
@staticmethod
def map_to_domain(category: ZoneCategory) -> Zone:
return ZoneCategory(
category=category.category,
sub_category=category.sub_category
)
37 changes: 30 additions & 7 deletions backend/bloom/services/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,13 @@ async def list_vessel_positions(vessel_id: int, date:datetime=datetime.now()):

@app.get("/zones")
async def list_zones(request:Request,nocache:bool=0):
cache= rd.get(app.url_path_for('list_zones'))
endpoint=f"/zones"
cache= rd.get(endpoint)
start = time.time()
if cache and not nocache:
logger.debug(f"{app.url_path_for('list_zones')} cached ({settings.redis_cache_expiration})s")
logger.debug(f"{endpoint} cached ({settings.redis_cache_expiration})s")
payload=json.loads(cache)
logger.debug(f"{app.url_path_for('list_zones')} elapsed Time: {time.time()-start}")
logger.debug(f"{endpoint} elapsed Time: {time.time()-start}")
return payload
else:
use_cases = UseCases()
Expand All @@ -150,11 +151,33 @@ async def list_zones(request:Request,nocache:bool=0):
with db.session() as session:
json_data = [z.model_dump_json()
for z in zone_repository.get_all_zones(session)]
rd.set(app.url_path_for('list_zones'), json.dumps(json_data))
rd.expire(app.url_path_for('list_zones'),settings.redis_cache_expiration)
logger.debug(f"{app.url_path_for('list_zones')} elapsed Time: {time.time()-start}")
rd.set(endpoint, json.dumps(json_data))
rd.expire(endpoint,settings.redis_cache_expiration)
logger.debug(f"{endpoint} elapsed Time: {time.time()-start}")
return json_data


@app.get("/zones/all/categories")
async def list_zone_categories(request:Request,nocache:bool=0):
endpoint=f"/zones/all/categories"
cache= rd.get(endpoint)
start = time.time()
if cache and not nocache:
logger.debug(f"{endpoint} cached ({settings.redis_cache_expiration})s")
payload=json.loads(cache)
logger.debug(f"{endpoint} elapsed Time: {time.time()-start}")
return payload
else:
use_cases = UseCases()
zone_repository = use_cases.zone_repository()
db = use_cases.db()
with db.session() as session:
json_data = [z.model_dump_json()
for z in zone_repository.get_all_zone_categories(session)]
rd.set(endpoint, json.dumps(json_data))
rd.expire(endpoint,settings.redis_cache_expiration)
logger.debug(f"{endpoint} elapsed Time: {time.time()-start}")
return json_data

@app.get("/zones/by-category/{category}")
async def get_zone_all_by_category(category:str="amp",nocache:bool=0):
endpoint=f"/zones/by-category/{category}"
Expand Down

0 comments on commit 2897a06

Please sign in to comment.