Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Adds logic to fetch specific keys for a product #244

Merged

Conversation

kirtanchandak
Copy link
Contributor

What

Adds logic to fetch specific keys related to a product in a comma-separated manner
http://127.0.0.1:8000/product/3760256070970?keys=taste
http://127.0.0.1:8000/product/3760256070970?keys=taste,size

Screenshot

image
image

Fixes bug(s)

#236

@kirtanchandak kirtanchandak requested a review from a team as a code owner March 6, 2025 17:10
@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 94.76%. Comparing base (b9cda65) to head (fe0c52c).
Report is 75 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #244      +/-   ##
==========================================
- Coverage   95.06%   94.76%   -0.30%     
==========================================
  Files           5        5              
  Lines         324      382      +58     
==========================================
+ Hits          308      362      +54     
- Misses         16       20       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job, I just added a suggestion for readability.

Comment on lines 342 to 353
query = """
SELECT json_agg(j)::json FROM (
SELECT * FROM folksonomy WHERE product = %s AND owner = %s
"""
params = [product, owner]

if keys_list:
placeholders = ', '.join(['%s'] * len(keys_list))
query += f" AND k IN ({placeholders})"
params.extend(keys_list)

query += " ORDER BY k) as j;"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rewrite it to use f-string to improve readability

Suggested change
query = """
SELECT json_agg(j)::json FROM (
SELECT * FROM folksonomy WHERE product = %s AND owner = %s
"""
params = [product, owner]
if keys_list:
placeholders = ', '.join(['%s'] * len(keys_list))
query += f" AND k IN ({placeholders})"
params.extend(keys_list)
query += " ORDER BY k) as j;"
params = [product, owner]
if keys_list:
placeholders = ', '.join(['%s'] * len(keys_list))
key_clause = f"k IN ({placeholders})"
params.extend(keys_list)
else:
key_clause = f"TRUE"
query = f"""
SELECT json_agg(j)::json FROM (
SELECT * FROM folksonomy
WHERE product = %s AND owner = %s AND {key_clause}
ORDER BY k
) as j;"

Copy link
Contributor Author

@kirtanchandak kirtanchandak Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the required changes, ready to merge! thanks @alexgarel

@kirtanchandak kirtanchandak requested a review from alexgarel March 12, 2025 11:07
response: Response,
product: str,
owner: str = '',
keys: str = Query(None, description="Comma-separated list of keys to filter by"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can specify that without keys parameter, it returns all keys.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

made the change, added more clear description

Copy link
Member

@alexgarel alexgarel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I approve your PR @kirtanchandak still you have a conflict to resolve. I also added a comment.

@kirtanchandak
Copy link
Contributor Author

@alexgarel ready to merge resolved merge conflicts and made the description change

@kirtanchandak kirtanchandak requested a review from alexgarel March 17, 2025 17:37
@CharlesNepote
Copy link
Member

I have tested it, works fine. I'm going to merge. Thanks a lot!

@CharlesNepote CharlesNepote merged commit c3bc052 into openfoodfacts:main Mar 18, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants