-
Notifications
You must be signed in to change notification settings - Fork 6
[DRAFT] OPTIMIZE API CALL ON MULTIPLE ADD: As Frances, I want the API to be optimized, so that my interactions are faster when I add a record to multiple stories #273
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
Open
richardmatthewsdev
wants to merge
10
commits into
main
Choose a base branch
from
rm/stories-multiple-add
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a8d2d02
Allow adding multiple items to multiple stories
richardmatthewsdev 572a8ea
Update to authorize and find all stories before continuing
richardmatthewsdev aa10dae
rename add_to_stories to multiple_add
richardmatthewsdev ed981d6
Multiple delete
richardmatthewsdev 1231882
Remove extra line
richardmatthewsdev ed1f35b
rubocoppping
richardmatthewsdev 9fd4a4f
Move multiple actions to their own concern
richardmatthewsdev 7748891
rubocopping
richardmatthewsdev 21f3636
rename for clarity
richardmatthewsdev 7c1982d
Add specs for multiple_add? and multiple_remove? policies
richardmatthewsdev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
app/controllers/supplejack_api/concerns/stories/multiple.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module SupplejackApi | ||
| module Concerns | ||
| module Stories | ||
| module Multiple | ||
| def multiple_add | ||
| stories = multiple_params['stories'].each_with_object([]) do |story, stories_array| | ||
| set = SupplejackApi::UserSet.custom_find(story['id']) | ||
| return render_error_with(I18n.t('errors.story_not_found', id: story['id']), :not_found) unless set | ||
|
|
||
| authorize(set) | ||
| stories_array.push(set) | ||
| end | ||
|
|
||
| changes = multiple_params['stories'].each_with_object([]) do |story_params, changes_array| | ||
| set = stories.find { |s| s.id.to_s == story_params['id'] } | ||
|
|
||
| item_ids = story_params['items'].each_with_object([]) do |item, ids| | ||
| item = set.set_items.build(item) | ||
|
|
||
| return render_error_with(item.errors.messages.values.join(', '), :bad_request) unless item.valid? | ||
|
|
||
| ids.push(item.id) | ||
| end | ||
|
|
||
| set.save! | ||
|
|
||
| changes_array.push({ | ||
| story_id: story_params['id'], | ||
| item_ids: item_ids | ||
| }) | ||
| end | ||
|
|
||
| render json: changes | ||
| end | ||
|
|
||
| def multiple_remove | ||
| stories = multiple_params['stories'].each_with_object([]) do |story, stories_array| | ||
| set = SupplejackApi::UserSet.custom_find(story['id']) | ||
| return render_error_with(I18n.t('errors.story_not_found', id: story['id']), :not_found) unless set | ||
|
|
||
| authorize(set) | ||
| stories_array.push(set) | ||
| end | ||
|
|
||
| multiple_params['stories'].each do |story_params| | ||
| set = stories.find { |s| s.id.to_s == story_params['id'] } | ||
|
|
||
| story_params['items'].each do |item| | ||
| set_item = set.set_items.find_by_id(item[:id]) | ||
|
|
||
| return render json: { errors: I18n.t('errors.record_not_found', id: item[:id]) }, | ||
| status: :not_found unless set_item | ||
|
|
||
| set_item.destroy! | ||
| end | ||
|
|
||
| set.save! | ||
| end | ||
|
|
||
| head :no_content | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def multiple_params | ||
| params.permit(:api_key, | ||
| stories: [:id, | ||
| { items: [:id, :position, :type, :sub_type, :image_url, | ||
| :display_collection, :category, :meta, :record_id, | ||
| { content: %i[value image_url display_collection category] }] }]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specs for this new policies?