Add collection ids to Api (reading + filtering) (#4180)
* Add collection ids to Api (reading + filtering) - refactored controller - modifyied views - added tests * Add collection ids to Api (reading + filtering): requested changes - changed order in service - proper test in filtering
This commit is contained in:
parent
432d3348f6
commit
82cca496fd
5 changed files with 23 additions and 1 deletions
|
|
@ -28,6 +28,7 @@ module Api
|
|||
params[:username],
|
||||
params[:signature],
|
||||
params[:state],
|
||||
params[:collection_id],
|
||||
]
|
||||
set_surrogate_key_header key_headers.join("_")
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
class ArticleApiIndexService
|
||||
attr_accessor :tag, :username, :page, :state, :top
|
||||
attr_accessor :tag, :username, :page, :state, :top, :collection_id
|
||||
|
||||
def initialize(params)
|
||||
@page = params[:page]
|
||||
|
|
@ -7,6 +7,7 @@ class ArticleApiIndexService
|
|||
@username = params[:username]
|
||||
@state = params[:state]
|
||||
@top = params[:top]
|
||||
@collection_id = params[:collection_id]
|
||||
end
|
||||
|
||||
def get
|
||||
|
|
@ -18,6 +19,8 @@ class ArticleApiIndexService
|
|||
state_articles(state)
|
||||
elsif top.present?
|
||||
top_articles
|
||||
elsif collection_id.present?
|
||||
collection_articles(collection_id)
|
||||
else
|
||||
base_articles
|
||||
end
|
||||
|
|
@ -81,6 +84,15 @@ class ArticleApiIndexService
|
|||
end
|
||||
end
|
||||
|
||||
def collection_articles(collection_id)
|
||||
Article.published.
|
||||
where(collection_id: collection_id).
|
||||
includes(:user, :organization).
|
||||
order("published_at").
|
||||
page(page).
|
||||
per(30)
|
||||
end
|
||||
|
||||
def base_articles
|
||||
Article.published.
|
||||
where(featured: true).
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ json.array! @articles do |article|
|
|||
json.comments_count article.comments_count
|
||||
json.positive_reactions_count article.positive_reactions_count
|
||||
json.published_timestamp article.published_timestamp
|
||||
json.collection_id article.collection_id
|
||||
|
||||
json.partial! "api/v0/shared/user", user: article.user
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,7 @@ json.url @article.url
|
|||
json.canonical_url @article.processed_canonical_url
|
||||
json.comments_count @article.comments_count
|
||||
json.positive_reactions_count @article.positive_reactions_count
|
||||
json.collection_id @article.collection_id
|
||||
|
||||
json.created_at @article.created_at.utc.iso8601
|
||||
json.edited_at @article.edited_at&.utc&.iso8601
|
||||
|
|
|
|||
|
|
@ -71,6 +71,13 @@ RSpec.describe "Api::V0::Articles", type: :request do
|
|||
expect(response_article["flare_tag"].keys).to eq(%w[name bg_color_hex text_color_hex])
|
||||
expect(response_article["flare_tag"]["name"]).to eq("discuss")
|
||||
end
|
||||
|
||||
it "returns a collection id" do
|
||||
collection = create(:collection, user: article.user)
|
||||
article.update_columns(collection_id: collection.id)
|
||||
get api_articles_path(collection_id: collection.id)
|
||||
expect(json_response[0]["collection_id"]).to eq collection.id
|
||||
end
|
||||
end
|
||||
|
||||
describe "GET /api/articles/:id" do
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue