Skip to content

Commit

Permalink
feaat: top 5 links can now filter by multiple links
Browse files Browse the repository at this point in the history
  • Loading branch information
noahdurbin committed Sep 13, 2024
1 parent d1311d2 commit 0ff94f8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion app/controllers/api/v1/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def show
def top_links
query = Link.where(private: false).order(click_count: :desc).limit(5)

query = query.joins(:tags).where(tags: { name: params[:tag] }) if params[:tag].present?
if params[:tag].present?
tags = params[:tag].split(',').map(&:strip)
query = query.joins(:tags).where(tags: { name: tags })
query = query.group('links.id').having('COUNT(DISTINCT tags.id) = ?', tags.size)
end

links = query.distinct

Expand Down
Binary file modified dump.rdb
Binary file not shown.
2 changes: 1 addition & 1 deletion spec/requests/api/v1/links_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@
links = JSON.parse(response.body, symbolize_names: true)[:data]

expect(links.count).to eq(2)
expect(links[0][:attributes][:click_count]).to eq)
expect(links[0][:attributes][:click_count]).to eq(100)
expect(links.last[:attributes][:click_count]).to eq(50)
end
end
Expand Down

0 comments on commit 0ff94f8

Please sign in to comment.