From 41df70fb96c6a62b92b3556d332cbdc39320ee67 Mon Sep 17 00:00:00 2001 From: Tejaswini Chile Date: Thu, 28 Jul 2022 13:59:16 +0530 Subject: [PATCH] chore: Add articles metadata (#5125) --- .../api/v1/accounts/articles_controller.rb | 11 +++++++---- .../api/v1/accounts/categories_controller.rb | 5 +++++ app/controllers/api/v1/accounts/portals_controller.rb | 5 +++++ .../public/api/v1/portals/articles_controller.rb | 4 ++-- app/models/portal.rb | 7 ++++++- .../api/v1/accounts/articles/index.json.jbuilder | 5 +++++ .../v1/accounts/categories/_category.json.jbuilder | 4 +++- .../api/v1/accounts/categories/index.json.jbuilder | 5 +++++ .../api/v1/accounts/portals/_portal.json.jbuilder | 6 ++++++ app/views/api/v1/accounts/portals/index.json.jbuilder | 5 +++++ .../public/api/v1/models/_category.json.jbuilder | 4 +++- app/views/public/api/v1/models/_portal.json.jbuilder | 8 +++++++- .../api/v1/portals/articles/index.json.jbuilder | 4 ++++ .../api/v1/accounts/articles_controller_spec.rb | 7 ++++--- .../api/v1/accounts/portals_controller_spec.rb | 2 +- .../public/api/v1/portals/articles_controller_spec.rb | 4 +++- .../api/v1/portals/categories_controller_spec.rb | 1 + .../public/api/v1/portals_controller_spec.rb | 1 + spec/models/portal_spec.rb | 1 + 19 files changed, 74 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/v1/accounts/articles_controller.rb b/app/controllers/api/v1/accounts/articles_controller.rb index efe761501..8e3aadf5a 100644 --- a/app/controllers/api/v1/accounts/articles_controller.rb +++ b/app/controllers/api/v1/accounts/articles_controller.rb @@ -2,10 +2,11 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController before_action :portal before_action :check_authorization before_action :fetch_article, except: [:index, :create] + before_action :set_current_page, only: [:index] def index @articles = @portal.articles - @articles = @articles.search(list_params) if params[:payload].present? + @articles = @articles.search(list_params) if list_params.present? end def create @@ -45,8 +46,10 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController end def list_params - params.require(:payload).permit( - :category_slug, :locale, :query, :page - ) + params.permit(:locale, :query, :page, :category_slug) + end + + def set_current_page + @current_page = params[:page] || 1 end end diff --git a/app/controllers/api/v1/accounts/categories_controller.rb b/app/controllers/api/v1/accounts/categories_controller.rb index c4491b2e2..bc18b61b8 100644 --- a/app/controllers/api/v1/accounts/categories_controller.rb +++ b/app/controllers/api/v1/accounts/categories_controller.rb @@ -2,6 +2,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle before_action :portal before_action :check_authorization before_action :fetch_category, except: [:index, :create] + before_action :set_current_page, only: [:index] def index @categories = @portal.categories.search(params) @@ -49,4 +50,8 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle :name, :description, :position, :slug, :locale, :parent_category_id, :associated_category_id ) end + + def set_current_page + @current_page = params[:page] || 1 + end end diff --git a/app/controllers/api/v1/accounts/portals_controller.rb b/app/controllers/api/v1/accounts/portals_controller.rb index b081978b9..f994f6722 100644 --- a/app/controllers/api/v1/accounts/portals_controller.rb +++ b/app/controllers/api/v1/accounts/portals_controller.rb @@ -3,6 +3,7 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController before_action :fetch_portal, except: [:index, :create] before_action :check_authorization + before_action :set_current_page, only: [:index] def index @portals = Current.account.portals @@ -66,4 +67,8 @@ class Api::V1::Accounts::PortalsController < Api::V1::Accounts::BaseController def portal_member_params params.require(:portal).permit(:account_id, member_ids: []) end + + def set_current_page + @current_page = params[:page] || 1 + end end diff --git a/app/controllers/public/api/v1/portals/articles_controller.rb b/app/controllers/public/api/v1/portals/articles_controller.rb index 2a961b7a9..e861d30ed 100644 --- a/app/controllers/public/api/v1/portals/articles_controller.rb +++ b/app/controllers/public/api/v1/portals/articles_controller.rb @@ -4,7 +4,7 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController def index @articles = @portal.articles - @articles = @articles.search(list_params) if params[:payload].present? + @articles = @articles.search(list_params) if list_params.present? end def show; end @@ -20,6 +20,6 @@ class Public::Api::V1::Portals::ArticlesController < ApplicationController end def list_params - params.require(:payload).permit(:query) + params.permit(:query) end end diff --git a/app/models/portal.rb b/app/models/portal.rb index 19b6cfa31..e37a5992d 100644 --- a/app/models/portal.rb +++ b/app/models/portal.rb @@ -46,7 +46,7 @@ class Portal < ApplicationRecord scope :active, -> { where(archived: false) } - CONFIG_JSON_KEYS = %w[allowed_locales].freeze + CONFIG_JSON_KEYS = %w[allowed_locales default_locale].freeze def file_base_data { @@ -60,9 +60,14 @@ class Portal < ApplicationRecord } end + def default_locale + config['default_locale'] || 'en' + end + private def config_json_format + config['default_locale'] = 'en' denied_keys = config.keys - CONFIG_JSON_KEYS errors.add(:cofig, "in portal on #{denied_keys.join(',')} is not supported.") if denied_keys.any? end diff --git a/app/views/api/v1/accounts/articles/index.json.jbuilder b/app/views/api/v1/accounts/articles/index.json.jbuilder index 3e3364007..89634e32a 100644 --- a/app/views/api/v1/accounts/articles/index.json.jbuilder +++ b/app/views/api/v1/accounts/articles/index.json.jbuilder @@ -1,3 +1,8 @@ json.payload do json.array! @articles, partial: 'article', as: :article end + +json.meta do + json.current_page @current_page + json.articles_count @articles.size +end diff --git a/app/views/api/v1/accounts/categories/_category.json.jbuilder b/app/views/api/v1/accounts/categories/_category.json.jbuilder index 17aecacd2..dccf70bd6 100644 --- a/app/views/api/v1/accounts/categories/_category.json.jbuilder +++ b/app/views/api/v1/accounts/categories/_category.json.jbuilder @@ -26,4 +26,6 @@ if category.root_category.present? end end -json.articles_count category.articles.published.try(:count) +json.meta do + json.articles_count category.articles.size +end diff --git a/app/views/api/v1/accounts/categories/index.json.jbuilder b/app/views/api/v1/accounts/categories/index.json.jbuilder index 89fa05200..f73995c2c 100644 --- a/app/views/api/v1/accounts/categories/index.json.jbuilder +++ b/app/views/api/v1/accounts/categories/index.json.jbuilder @@ -1,3 +1,8 @@ json.payload do json.array! @categories, partial: 'category', as: :category end + +json.meta do + json.current_page @current_page + json.categories_count @categories.size +end diff --git a/app/views/api/v1/accounts/portals/_portal.json.jbuilder b/app/views/api/v1/accounts/portals/_portal.json.jbuilder index 12fcde593..bd0d0b127 100644 --- a/app/views/api/v1/accounts/portals/_portal.json.jbuilder +++ b/app/views/api/v1/accounts/portals/_portal.json.jbuilder @@ -17,3 +17,9 @@ json.portal_members do end end end + +json.meta do + json.articles_count portal.articles.size + json.categories_count portal.categories.size + json.default_locale portal.default_locale +end diff --git a/app/views/api/v1/accounts/portals/index.json.jbuilder b/app/views/api/v1/accounts/portals/index.json.jbuilder index 3e4553382..2fb93d918 100644 --- a/app/views/api/v1/accounts/portals/index.json.jbuilder +++ b/app/views/api/v1/accounts/portals/index.json.jbuilder @@ -1,3 +1,8 @@ json.payload do json.array! @portals, partial: 'portal', as: :portal end + +json.meta do + json.current_page @current_page + json.portals_count @portals.size +end diff --git a/app/views/public/api/v1/models/_category.json.jbuilder b/app/views/public/api/v1/models/_category.json.jbuilder index 6c611fc51..e257b0045 100644 --- a/app/views/public/api/v1/models/_category.json.jbuilder +++ b/app/views/public/api/v1/models/_category.json.jbuilder @@ -24,4 +24,6 @@ if category.root_category.present? end end -json.articles_count category.articles.published.try(:count) +json.meta do + json.articles_count category.articles.published.size +end diff --git a/app/views/public/api/v1/models/_portal.json.jbuilder b/app/views/public/api/v1/models/_portal.json.jbuilder index 405541a5b..edae5cf98 100644 --- a/app/views/public/api/v1/models/_portal.json.jbuilder +++ b/app/views/public/api/v1/models/_portal.json.jbuilder @@ -8,7 +8,13 @@ json.slug portal.slug json.categories do if portal.categories.any? json.array! portal.categories.each do |category| - json.partial! 'categories/category.json.jbuilder', category: category + json.partial! 'api/v1/models/category.json.jbuilder', category: category end end end + +json.meta do + json.articles_count portal.articles.published.size + json.categories_count portal.categories.size + json.default_locale portal.default_locale +end diff --git a/app/views/public/api/v1/portals/articles/index.json.jbuilder b/app/views/public/api/v1/portals/articles/index.json.jbuilder index 477662a36..00d271834 100644 --- a/app/views/public/api/v1/portals/articles/index.json.jbuilder +++ b/app/views/public/api/v1/portals/articles/index.json.jbuilder @@ -1,3 +1,7 @@ json.payload do json.array! @articles, partial: 'public/api/v1/models/article.json.jbuilder', as: :article end + +json.meta do + json.articles_count @articles.published.size +end diff --git a/spec/controllers/api/v1/accounts/articles_controller_spec.rb b/spec/controllers/api/v1/accounts/articles_controller_spec.rb index ce4aab778..57d4f8feb 100644 --- a/spec/controllers/api/v1/accounts/articles_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/articles_controller_spec.rb @@ -157,7 +157,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", headers: agent.create_new_auth_token, - params: { payload: {} } + params: {} expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['payload'].count).to be 2 @@ -169,7 +169,7 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", headers: agent.create_new_auth_token, - params: { payload: { category_slug: category.slug } } + params: { category_slug: category.slug } expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['payload'].count).to be 2 @@ -186,10 +186,11 @@ RSpec.describe 'Api::V1::Accounts::Articles', type: :request do get "/api/v1/accounts/#{account.id}/portals/#{portal.slug}/articles", headers: agent.create_new_auth_token, - params: { payload: { query: 'funny' } } + params: { query: 'funny' } expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['payload'].count).to be 1 + expect(json_response['meta']['articles_count']).to be json_response['payload'].size end end diff --git a/spec/controllers/api/v1/accounts/portals_controller_spec.rb b/spec/controllers/api/v1/accounts/portals_controller_spec.rb index e756edb30..8bc6c86a2 100644 --- a/spec/controllers/api/v1/accounts/portals_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/portals_controller_spec.rb @@ -115,7 +115,7 @@ RSpec.describe 'Api::V1::Accounts::Portals', type: :request do expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['name']).to eql(portal_params[:portal][:name]) - expect(json_response['config']).to eql({ 'allowed_locales' => %w[en es] }) + expect(json_response['config']).to eql({ 'allowed_locales' => %w[en es], 'default_locale' => 'en' }) end it 'archive portal' do 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 0e074da68..05a07aeae 100644 --- a/spec/controllers/public/api/v1/portals/articles_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/articles_controller_spec.rb @@ -23,6 +23,7 @@ RSpec.describe 'Public Articles API', type: :request do json_response = JSON.parse(response.body) expect(json_response['payload'].length).to eql portal.articles.count + expect(json_response['meta']['articles_count']).to be json_response['payload'].size end it 'get all articles with searched text query' do @@ -36,10 +37,11 @@ RSpec.describe 'Public Articles API', type: :request do get "/public/api/v1/portals/#{portal.slug}/articles", headers: agent.create_new_auth_token, - params: { payload: { query: 'funny' } } + params: { query: 'funny' } expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['payload'].count).to be 1 + expect(json_response['meta']['articles_count']).to be json_response['payload'].size end end diff --git a/spec/controllers/public/api/v1/portals/categories_controller_spec.rb b/spec/controllers/public/api/v1/portals/categories_controller_spec.rb index 524871447..7bff264d4 100644 --- a/spec/controllers/public/api/v1/portals/categories_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals/categories_controller_spec.rb @@ -31,6 +31,7 @@ RSpec.describe 'Public Categories API', type: :request do json_response = JSON.parse(response.body) expect(json_response['slug']).to eql category_slug + expect(json_response['meta']['articles_count']).to be 0 end end end diff --git a/spec/controllers/public/api/v1/portals_controller_spec.rb b/spec/controllers/public/api/v1/portals_controller_spec.rb index 4abf78c70..71a6cdcc0 100644 --- a/spec/controllers/public/api/v1/portals_controller_spec.rb +++ b/spec/controllers/public/api/v1/portals_controller_spec.rb @@ -16,6 +16,7 @@ RSpec.describe 'Public Portals API', type: :request do expect(response).to have_http_status(:success) json_response = JSON.parse(response.body) expect(json_response['slug']).to eql 'test-portal' + expect(json_response['meta']['articles_count']).to be 0 end end end diff --git a/spec/models/portal_spec.rb b/spec/models/portal_spec.rb index 20fc2baba..3e574ff3f 100644 --- a/spec/models/portal_spec.rb +++ b/spec/models/portal_spec.rb @@ -24,6 +24,7 @@ RSpec.describe Portal, type: :model do it 'Adds default allowed_locales en' do expect(portal.config).to be_present expect(portal.config['allowed_locales']).to eq(['en']) + expect(portal.config['default_locale']).to eq('en') end it 'Does not allow any other config than allowed_locales' do