-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sort on issues endpoint per sort params
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 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,33 @@ | ||
class IssuesFeedBuilder | ||
attr_reader :product, :sort_params, :issues | ||
|
||
def initialize(product:, sort_params:) | ||
@product = product | ||
@sort_params = sort_params | ||
end | ||
|
||
def call | ||
@issues = Issue.includes(:issue_category, :user, :issue_upvotes).latest_first | ||
@issues = filtered_by_product if product.present? | ||
@issues = sorted if sort_params.present? | ||
|
||
issues | ||
end | ||
|
||
private | ||
|
||
def filtered_by_product | ||
issues.where(product:) | ||
end | ||
|
||
def sorted | ||
byebug | ||
Check warning on line 24 in api/app/services/issues_feed_builder.rb GitHub Actions / rubocop
|
||
if sort_params[:sort_by] == 'upvotes' | ||
issues.sort_by_upvotes(sort_params[:sort_direction] == 'desc' ? :desc : :asc) | ||
elsif sort_params[:sort_by] == 'comments' | ||
issues.sort_by_comments(sort_params[:sort_direction] == 'desc' ? :desc : :asc) | ||
else | ||
raise StandardError, 'Invalid sort_by param' | ||
end | ||
end | ||
end |
This file contains 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 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,40 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe IssuesFeedBuilder do | ||
subject(:service) { described_class.new(product:, sort_params:) } | ||
|
||
let(:product) { create(:product) } | ||
|
||
describe '#call' do | ||
subject(:result) { service.call } | ||
|
||
before do | ||
create(:issue, upvotes_count: 10, comments_count: 5, product: product) | ||
create(:issue, upvotes_count: 5, comments_count: 10, product: product) | ||
end | ||
|
||
context 'when sort params are not present' do | ||
let(:sort_params) { nil } | ||
|
||
it 'returns issues sorted by created_at desc' do | ||
expect(result.pluck(:created_at)).to eq result.pluck(:created_at).sort.reverse | ||
end | ||
end | ||
|
||
context 'when sorting by upvotes' do | ||
let(:sort_params) { { sort_by: 'upvotes', sort_direction: 'desc' } } | ||
|
||
it 'returns issues sorted by upvotes desc' do | ||
expect(result.pluck(:upvotes_count)).to eq [10, 5] | ||
end | ||
end | ||
|
||
context 'when sorting by comments' do | ||
let(:sort_params) { { sort_by: 'comments', sort_direction: 'desc' } } | ||
|
||
it 'returns issues sorted by comments desc' do | ||
expect(result.pluck(:comments_count)).to eq [10, 5] | ||
end | ||
end | ||
end | ||
end |