🔧 refactor(store.py): remove duplicate fields list and use default_fields from StoreService class

🔧 refactor(service.py): move default_fields list to StoreService class and use it in query_components method
This commit is contained in:
Gabriel Luiz Freitas Almeida 2023-10-27 13:07:09 -03:00
commit 6f3172bc33
2 changed files with 17 additions and 34 deletions

View file

@ -92,23 +92,10 @@ def list_components(
store_api_Key: str = Depends(get_optional_user_store_api_key), store_api_Key: str = Depends(get_optional_user_store_api_key),
): ):
try: try:
fields = [
"id",
"name",
"description",
"user_created.first_name",
"is_component",
"tags.tags_id.name",
"tags.tags_id.id",
"count(liked_by)",
"count(downloads)",
"metadata",
]
result = store_service.query_components( result = store_service.query_components(
store_api_Key, api_key=store_api_Key,
page, page=page,
limit, limit=limit,
fields=fields,
filter_by_user=filter_by_user, filter_by_user=filter_by_user,
) )

View file

@ -30,6 +30,18 @@ class StoreService(Service):
self.download_webhook_url = self.settings_service.settings.DOWNLOAD_WEBHOOK_URL self.download_webhook_url = self.settings_service.settings.DOWNLOAD_WEBHOOK_URL
self.like_webhook_url = self.settings_service.settings.LIKE_WEBHOOK_URL self.like_webhook_url = self.settings_service.settings.LIKE_WEBHOOK_URL
self.components_url = f"{self.base_url}/items/components" self.components_url = f"{self.base_url}/items/components"
self.default_fields = [
"id",
"name",
"description",
"user_created.first_name",
"is_component",
"tags.tags_id.name",
"tags.tags_id.id",
"count(liked_by)",
"count(downloads)",
"metadata",
]
def _get( def _get(
self, url: str, api_key: str, params: Dict[str, Any] = None self, url: str, api_key: str, params: Dict[str, Any] = None
@ -149,24 +161,8 @@ class StoreService(Service):
) -> Union[List[ListComponentResponse], List[Dict[str, int]]]: ) -> Union[List[ListComponentResponse], List[Dict[str, int]]]:
params = {"page": page, "limit": limit} params = {"page": page, "limit": limit}
# ?aggregate[count]=likes # ?aggregate[count]=likes
params["fields"] = ( params["fields"] = ",".join(fields) if fields else ",".join(self.default_fields)
",".join(fields)
if fields
else ",".join(
[
"id",
"name",
"description",
"user_created.first_name",
"is_component",
"tags.tags_id.name",
"tags.tags_id.id",
"count(liked_by)",
"count(downloads)",
"metadata",
]
)
)
# Only public components or the ones created by the user # Only public components or the ones created by the user
# check for "public" or "Public" # check for "public" or "Public"