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

[Issue #3295] Create GET /users/:userId/saved-opportunities API schema and stub endpoint #3355

Merged
merged 16 commits into from
Jan 3, 2025

Conversation

mikehgrantsgov
Copy link
Collaborator

Summary

Fixes #3295

Time to review: 25 mins

Changes proposed

Create GET /users/:userId/saved-opportunities API schema

Context for reviewers

Overlaps with the implementation ticket, as it was easier to add the service and implementation vs. stub + implement in 2 different PRs

Additional information

See attached unit tests.

api/src/api/opportunities_v1/opportunity_schemas.py Outdated Show resolved Hide resolved
api/src/services/users/get_saved_opportunities.py Outdated Show resolved Hide resolved
api/src/services/users/get_saved_opportunities.py Outdated Show resolved Hide resolved
}
)

return {"message": "Success", "data": opportunities_data}
Copy link
Collaborator

Choose a reason for hiding this comment

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

We shouldn't be returning a dict, return an ApiResponse object

Comment on lines 209 to 212
opportunities_data = []
for saved in saved_opportunities:
opp = saved.opportunity
summary = opp.summary
Copy link
Collaborator

Choose a reason for hiding this comment

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

This loop shouldn't be necessary, it should be fine to return the ORM objects and Marshmallow will handle iterating over/making them into JSON for you. If you're hitting an error about objects being detached from a session, then that's because SQLAlchemy lazy-loads all relationships (this loop effectively emits several SQL queries). Easiest way around that is to load in the query itself. It's why in an endpoint like GET opportunity we do .options(selectinload("*")): https://github.com/HHS/simpler-grants-gov/blob/main/api/src/services/opportunities_v1/get_opportunity.py#L21

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yep that makes sense. I thought we had to do more work here than needed. I did update the utility to return pure Opportunity objects so it will fit in the shape of the response.

Copy link
Collaborator

@chouinar chouinar left a comment

Choose a reason for hiding this comment

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

LGTM!

@mikehgrantsgov mikehgrantsgov merged commit 57aae5c into main Jan 3, 2025
2 checks passed
@mikehgrantsgov mikehgrantsgov deleted the mikehgrantsgov/3295-create-get-user-saved-opps branch January 3, 2025 14:55
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.

Create GET /users/:userId/saved-opportunities API schema and stub endpoint
3 participants