Feat: Article public apis (#4955)

This commit is contained in:
Tejaswini Chile
2022-07-08 17:24:38 +05:30
committed by GitHub
parent 13a4e0e6d9
commit fdf449dc87
19 changed files with 205 additions and 46 deletions

View File

@@ -5,7 +5,7 @@ class Api::V1::Accounts::ArticlesController < Api::V1::Accounts::BaseController
def index
@articles = @portal.articles
@articles.search(list_params) if params[:payload].present?
@articles = @articles.search(list_params) if params[:payload].present?
end
def create

View File

@@ -46,7 +46,7 @@ class Api::V1::Accounts::CategoriesController < Api::V1::Accounts::BaseControlle
def category_params
params.require(:category).permit(
:name, :description, :position, :slug, :locale, :parent_category_id, :linked_category_id
:name, :description, :position, :slug, :locale, :parent_category_id, :associated_category_id
)
end
end

View File

@@ -0,0 +1,25 @@
class Public::Api::V1::Portals::ArticlesController < ApplicationController
before_action :set_portal
before_action :set_article, only: [:show]
def index
@articles = @portal.articles
@articles = @articles.search(list_params) if params[:payload].present?
end
def show; end
private
def set_article
@article = @portal.articles.find(params[:id])
end
def set_portal
@portal = ::Portal.find_by!(slug: params[:portal_slug], archived: false)
end
def list_params
params.require(:payload).permit(:query)
end
end

View File

@@ -77,7 +77,7 @@ class Article < ApplicationRecord
records = joins(
:category
).search_by_category_slug(params[:category_slug]).search_by_category_locale(params[:locale])
records.text_search(params[:query]) if params[:query].present?
records = records.text_search(params[:query]) if params[:query].present?
records.page(current_page(params))
end

View File

@@ -2,22 +2,22 @@
#
# Table name: categories
#
# id :bigint not null, primary key
# description :text
# locale :string default("en")
# name :string
# position :integer
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# linked_category_id :bigint
# parent_category_id :bigint
# portal_id :integer not null
# id :bigint not null, primary key
# description :text
# locale :string default("en")
# name :string
# position :integer
# slug :string not null
# created_at :datetime not null
# updated_at :datetime not null
# account_id :integer not null
# associated_category_id :bigint
# parent_category_id :bigint
# portal_id :integer not null
#
# Indexes
#
# index_categories_on_linked_category_id (linked_category_id)
# index_categories_on_associated_category_id (associated_category_id)
# index_categories_on_locale (locale)
# index_categories_on_locale_and_account_id (locale,account_id)
# index_categories_on_parent_category_id (parent_category_id)
@@ -25,7 +25,7 @@
#
# Foreign Keys
#
# fk_rails_... (linked_category_id => categories.id)
# fk_rails_... (associated_category_id => categories.id)
# fk_rails_... (parent_category_id => categories.id)
#
class Category < ApplicationRecord
@@ -45,13 +45,17 @@ class Category < ApplicationRecord
foreign_key: :parent_category_id,
dependent: :nullify,
inverse_of: 'parent_category'
has_many :linked_categories,
has_many :associated_categories,
class_name: :Category,
foreign_key: :linked_category_id,
foreign_key: :associated_category_id,
dependent: :nullify,
inverse_of: 'linked_category'
inverse_of: 'root_category'
belongs_to :parent_category, class_name: :Category, optional: true
belongs_to :linked_category, class_name: :Category, optional: true
belongs_to :root_category,
class_name: :Category,
foreign_key: :associated_category_id,
inverse_of: :associated_categories,
optional: true
before_validation :ensure_account_id
validates :account_id, presence: true

View File

@@ -20,8 +20,8 @@ if category.parent_category.present?
end
end
if category.linked_category.present?
json.linked_category do
json.partial! 'api/v1/accounts/categories/associated_category.json.jbuilder', category: category.linked_category
if category.root_category.present?
json.root_category do
json.partial! 'api/v1/accounts/categories/associated_category.json.jbuilder', category: category.root_category
end
end

View File

@@ -0,0 +1,30 @@
json.id article.id
json.category_id article.category_id
json.title article.title
json.content article.content
json.description article.description
json.status article.status
json.account_id article.account_id
json.last_updated_at article.updated_at
if article.portal.present?
json.portal do
json.partial! 'api/v1/accounts/portals/portal.json.jbuilder', portal: article.portal
end
end
json.views article.views
if article.author.present?
json.author do
json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author
end
end
json.associated_articles do
if article.associated_articles.any?
json.array! article.associated_articles.each do |associated_article|
json.partial! 'api/v1/accounts/articles/associated_article.json.jbuilder', article: associated_article
end
end
end

View File

@@ -0,0 +1,15 @@
json.id article.id
json.category_id article.category_id
json.title article.title
json.content article.content
json.description article.description
json.status article.status
json.account_id article.account_id
json.last_updated_at article.updated_at
json.views article.views
if article.author.present?
json.author do
json.partial! 'api/v1/models/agent.json.jbuilder', resource: article.author
end
end

View File

@@ -18,8 +18,8 @@ if category.parent_category.present?
end
end
if category.linked_category.present?
json.linked_category do
json.partial! partial: 'associated_category', category: category.linked_category
if category.root_category.present?
json.root_category do
json.partial! partial: 'associated_category', category: category.root_category
end
end

View File

@@ -0,0 +1,3 @@
json.payload do
json.array! @articles, partial: 'public/api/v1/models/article.json.jbuilder', as: :article
end

View File

@@ -0,0 +1 @@
json.partial! 'public/api/v1/models/article.json.jbuilder', article: @article