add: home index api

This commit is contained in:
yystopf 2021-11-03 10:19:01 +08:00
parent 35f04afe91
commit 18bbbce7b3
19 changed files with 70 additions and 55 deletions

View File

@ -1,6 +0,0 @@
class Topic::ActivityForumsController < Topic::BaseController
def index
@activity_forums = kaminari_paginate(Topic::ActivityForum)
end
end

View File

@ -1,6 +0,0 @@
class Topic::BannersController < Topic::BaseController
def index
@banners = kaminari_paginate(Topic::Banner)
end
end

View File

@ -1,3 +0,0 @@
class Topic::BaseController < ApplicationController
end

View File

@ -1,6 +0,0 @@
class Topic::CardsController < Topic::BaseController
def index
@cards = kaminari_paginate(Topic::Card)
end
end

View File

@ -1,6 +0,0 @@
class Topic::CooperatorsController < Topic::BaseController
def index
@cooperators = kaminari_paginate(Topic::Cooperator)
end
end

View File

@ -1,6 +0,0 @@
class Topic::ExcellentProjectsController < Topic::BaseController
def index
@excellent_projects = kaminari_paginate(Topic::ExcellentProject)
end
end

View File

@ -1,6 +0,0 @@
class Topic::ExperienceForumsController < Topic::BaseController
def index
@experience_forums = kaminari_paginate(Topic::ExperienceForum)
end
end

View File

@ -1,6 +0,0 @@
class Topic::PinnedForumsController < Topic::BaseController
def index
@pinned_forums = kaminari_paginate(Topic::PinnedForum)
end
end

View File

@ -0,0 +1,9 @@
class TopicsController < ApplicationController
def index
return render_not_found("请输入正确的数据类型") unless params[:topic_type].present?
scope = Topic.with_single_type(params[:topic_type])
@topics = kaminari_paginate(scope)
end
end

View File

@ -15,10 +15,31 @@ class Topic < ApplicationRecord
default_scope { order(order_index: :desc)}
scope :with_single_type, ->(type){where(type: trans_simpletype_to_classtype(type))}
def image
image_url('image')
end
def self.trans_simpletype_to_classtype(type)
case type
when 'activity_forum'
'Topic::ActivityForum'
when 'banner'
'Topic::Banner'
when 'card'
'Topic::Card'
when 'cooperator'
'Topic::Cooperator'
when 'excellent_project'
'Topic::ExcellentProject'
when 'experience_forum'
'Topic::ExperienceForum'
when 'pinned_forum'
'Topic::PinnedForum'
end
end
private
def image_url(type)

View File

@ -0,0 +1,3 @@
json.(activity_forum, :id, :title, :url)
json.visits 0
json.created_time format_time(Time.now)

View File

@ -0,0 +1 @@
json.(banner, :id, :title, :image)

View File

@ -0,0 +1 @@
json.(card, :id, :title, :url)

View File

@ -0,0 +1 @@
json.(cooperator, :id, :title, :image, :url)

View File

@ -0,0 +1,3 @@
json.(excellent_project, :id, :title, :url)
project_common = $redis_cache.hgetall("v2-project-common:#{excellent_project&.uuid}")
json.visits (project_common['visits'] || 0).to_i

View File

@ -0,0 +1,3 @@
json.(experience_forum, :id, :title, :url)
json.visits 0
json.created_time format_time(Time.now)

View File

@ -0,0 +1,3 @@
json.(pinned_forum, :id, :title, :url)
json.visits 0
json.created_time format_time(Time.now)

View File

@ -0,0 +1,24 @@
json.partial! "commons/success"
json.total_count @topics.total_count
json.topics do
json.array! @topics.each do |topic|
case topic.type
when "Topic::ActivityForum"
json.partial! "activity_forum", locals: {activity_forum: topic}
when "Topic::Banner"
json.partial! "banner", locals: {banner: topic}
when "Topic::Card"
json.partial! "card", locals: {card: topic}
when "Topic::Cooperator"
json.partial! "cooperator", locals: {cooperator: topic}
when "Topic::ExcellentProject"
json.partial! "excellent_project", locals: {excellent_project: topic}
when "Topic::ExperienceForum"
json.partial! "experience_forum", locals: {experience_forum: topic}
when "Topic::PinnedForum"
json.partial! "pinned_forum", locals: {pinned_forum: topic}
else
json.nil!
end
end
end

View File

@ -24,16 +24,7 @@ Rails.application.routes.draw do
resources :edu_settings
scope '/api' do
namespace :topic do
resources :activity_forums, only: [:index]
resources :banners, only: [:index]
resources :cards, only: [:index]
resources :cooperators, only: [:index]
resources :excellent_projects, only: [:index]
resources :experience_forums, only: [:index]
resources :pinned_forums, only: [:index]
end
resources :topics, only: [:index]
namespace :ci do
resources :languages, only: [:index, :show] do
collection do