diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index 1f8be3ad7..aba385c83 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -8,13 +8,22 @@ class Public::Api::V1::Portals::ArticlesController < Public::Api::V1::Portals::B def index @articles = @portal.articles @articles = @articles.search(list_params) if list_params.present? - @articles.order(position: :asc) + order_by_sort_param + @articles.page(list_params[:page]) if list_params[:page].present? end def show; end private + def order_by_sort_param + @articles = if list_params[:sort].present? && list_params[:sort] == 'views' + @articles.order_by_views + else + @articles.order_by_position + end + end + def set_article @article = @portal.articles.find_by(slug: permitted_params[:article_slug]) @article.increment_view_count @@ -35,7 +44,7 @@ class Public::Api::V1::Portals::ArticlesController < Public::Api::V1::Portals::B end def list_params - params.permit(:query, :locale) + params.permit(:query, :locale, :sort) end def permitted_params diff --git a/app/models/article.rb b/app/models/article.rb index 44fdc8ab4..c830c7a1f 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -65,6 +65,7 @@ class Article < ApplicationRecord scope :search_by_status, ->(status) { where(status: status) if status.present? } scope :order_by_updated_at, -> { reorder(updated_at: :desc) } scope :order_by_position, -> { reorder(position: :asc) } + scope :order_by_views, -> { reorder(views: :desc) } # TODO: if text search slows down https://www.postgresql.org/docs/current/textsearch-features.html#TEXTSEARCH-UPDATE-TRIGGERS pg_search_scope( diff --git a/app/views/public/api/v1/models/hc/_associated_article.json.jbuilder b/app/views/public/api/v1/models/hc/_associated_article.json.jbuilder index dde37e5be..a2dac8044 100644 --- a/app/views/public/api/v1/models/hc/_associated_article.json.jbuilder +++ b/app/views/public/api/v1/models/hc/_associated_article.json.jbuilder @@ -10,6 +10,6 @@ json.views article.views if article.author.present? json.author do - json.partial! 'public/api/v1/models/portal/author', formats: [:json], resource: article.author + json.partial! 'public/api/v1/models/hc/author', formats: [:json], resource: article.author end end diff --git a/app/views/public/api/v1/portals/index.json.jbuilder b/app/views/public/api/v1/portals/index.json.jbuilder index 4f90a4b8c..c4d40a961 100644 --- a/app/views/public/api/v1/portals/index.json.jbuilder +++ b/app/views/public/api/v1/portals/index.json.jbuilder @@ -1 +1 @@ -json.array! @portals, partial: 'public/api/v1/models/portal', formats: [:json], as: :portal +json.array! @portals, partial: 'public/api/v1/models/hc/portal', formats: [:json], as: :portal diff --git a/app/views/public/api/v1/portals/show.json.jbuilder b/app/views/public/api/v1/portals/show.json.jbuilder index ab58f858f..fea8d56cc 100644 --- a/app/views/public/api/v1/portals/show.json.jbuilder +++ b/app/views/public/api/v1/portals/show.json.jbuilder @@ -1 +1 @@ -json.partial! 'public/api/v1/models/portal', formats: [:json], portal: @portal +json.partial! 'public/api/v1/models/hc/portal', formats: [:json], portal: @portal diff --git a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb index 86651fee1..0f0164cd3 100644 --- a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb @@ -8,15 +8,15 @@ RSpec.describe 'Public Articles API', type: :request do let!(:category_2) { create(:category, name: 'category', portal: portal, account_id: account.id, locale: 'es', slug: 'category_slug') } let!(:article) do create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, - content: 'This is a *test* content with ^markdown^') + content: 'This is a *test* content with ^markdown^', views: 0) end before do ENV['HELPCENTER_URL'] = ENV.fetch('FRONTEND_URL', nil) - create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id) - create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id) - create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id) - create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id) + create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, views: 15) + create(:article, category: category, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id, views: 1) + create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, associated_article_id: article.id, views: 5) + create(:article, category: category_2, portal: portal, account_id: account.id, author_id: agent.id, views: 4) end describe 'GET /public/api/v1/portals/:slug/articles' do @@ -35,10 +35,21 @@ RSpec.describe 'Public Articles API', type: :request do content: 'this is some test and funny content') expect(article2.id).not_to be_nil - get "/hc/#{portal.slug}/#{category.locale}/categories/#{category.slug}/articles", - headers: agent.create_new_auth_token, - params: { query: 'funny' } + get "/hc/#{portal.slug}/#{category.locale}/categories/#{category.slug}/articles.json", params: { query: 'funny' } expect(response).to have_http_status(:success) + response_data = JSON.parse(response.body, symbolize_names: true)[:payload] + expect(response_data.length).to eq(1) + end + + it 'get all popular articles if sort params is passed' do + get "/hc/#{portal.slug}/#{category.locale}/articles.json", params: { sort: 'views' } + + expect(response).to have_http_status(:success) + response_data = JSON.parse(response.body, symbolize_names: true)[:payload] + expect(response_data.length).to eq(3) + expect(response_data[0][:views]).to eq(15) + expect(response_data[1][:views]).to eq(1) + expect(response_data.last[:id]).to eq(article.id) end end