From 18bbbce7b37ae7a659235dc2b34a8d211e368f30 Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 3 Nov 2021 10:19:01 +0800 Subject: [PATCH] add: home index api --- .../topic/activity_forums_controller.rb | 6 ----- app/controllers/topic/banners_controller.rb | 6 ----- app/controllers/topic/base_controller.rb | 3 --- app/controllers/topic/cards_controller.rb | 6 ----- .../topic/cooperators_controller.rb | 6 ----- .../topic/excellent_projects_controller.rb | 6 ----- .../topic/experience_forums_controller.rb | 6 ----- .../topic/pinned_forums_controller.rb | 6 ----- app/controllers/topics_controller.rb | 9 +++++++ app/models/topic.rb | 21 ++++++++++++++++ .../topics/_activity_forum.json.jbuilder | 3 +++ app/views/topics/_banner.json.jbuilder | 1 + app/views/topics/_card.json.jbuilder | 1 + app/views/topics/_cooperator.json.jbuilder | 1 + .../topics/_excellent_project.json.jbuilder | 3 +++ .../topics/_experience_forum.json.jbuilder | 3 +++ app/views/topics/_pinned_forum.json.jbuilder | 3 +++ app/views/topics/index.json.jbuilder | 24 +++++++++++++++++++ config/routes.rb | 11 +-------- 19 files changed, 70 insertions(+), 55 deletions(-) delete mode 100644 app/controllers/topic/activity_forums_controller.rb delete mode 100644 app/controllers/topic/banners_controller.rb delete mode 100644 app/controllers/topic/base_controller.rb delete mode 100644 app/controllers/topic/cards_controller.rb delete mode 100644 app/controllers/topic/cooperators_controller.rb delete mode 100644 app/controllers/topic/excellent_projects_controller.rb delete mode 100644 app/controllers/topic/experience_forums_controller.rb delete mode 100644 app/controllers/topic/pinned_forums_controller.rb create mode 100644 app/controllers/topics_controller.rb create mode 100644 app/views/topics/_activity_forum.json.jbuilder create mode 100644 app/views/topics/_banner.json.jbuilder create mode 100644 app/views/topics/_card.json.jbuilder create mode 100644 app/views/topics/_cooperator.json.jbuilder create mode 100644 app/views/topics/_excellent_project.json.jbuilder create mode 100644 app/views/topics/_experience_forum.json.jbuilder create mode 100644 app/views/topics/_pinned_forum.json.jbuilder create mode 100644 app/views/topics/index.json.jbuilder diff --git a/app/controllers/topic/activity_forums_controller.rb b/app/controllers/topic/activity_forums_controller.rb deleted file mode 100644 index e1b6f3fa6..000000000 --- a/app/controllers/topic/activity_forums_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::ActivityForumsController < Topic::BaseController - - def index - @activity_forums = kaminari_paginate(Topic::ActivityForum) - end -end \ No newline at end of file diff --git a/app/controllers/topic/banners_controller.rb b/app/controllers/topic/banners_controller.rb deleted file mode 100644 index 9843ad202..000000000 --- a/app/controllers/topic/banners_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::BannersController < Topic::BaseController - - def index - @banners = kaminari_paginate(Topic::Banner) - end -end \ No newline at end of file diff --git a/app/controllers/topic/base_controller.rb b/app/controllers/topic/base_controller.rb deleted file mode 100644 index 0654a7e95..000000000 --- a/app/controllers/topic/base_controller.rb +++ /dev/null @@ -1,3 +0,0 @@ -class Topic::BaseController < ApplicationController - -end \ No newline at end of file diff --git a/app/controllers/topic/cards_controller.rb b/app/controllers/topic/cards_controller.rb deleted file mode 100644 index a61031bf7..000000000 --- a/app/controllers/topic/cards_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::CardsController < Topic::BaseController - - def index - @cards = kaminari_paginate(Topic::Card) - end -end \ No newline at end of file diff --git a/app/controllers/topic/cooperators_controller.rb b/app/controllers/topic/cooperators_controller.rb deleted file mode 100644 index c1f4ad60b..000000000 --- a/app/controllers/topic/cooperators_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::CooperatorsController < Topic::BaseController - - def index - @cooperators = kaminari_paginate(Topic::Cooperator) - end -end \ No newline at end of file diff --git a/app/controllers/topic/excellent_projects_controller.rb b/app/controllers/topic/excellent_projects_controller.rb deleted file mode 100644 index fb57b0541..000000000 --- a/app/controllers/topic/excellent_projects_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::ExcellentProjectsController < Topic::BaseController - - def index - @excellent_projects = kaminari_paginate(Topic::ExcellentProject) - end -end \ No newline at end of file diff --git a/app/controllers/topic/experience_forums_controller.rb b/app/controllers/topic/experience_forums_controller.rb deleted file mode 100644 index 43a555a94..000000000 --- a/app/controllers/topic/experience_forums_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::ExperienceForumsController < Topic::BaseController - - def index - @experience_forums = kaminari_paginate(Topic::ExperienceForum) - end -end \ No newline at end of file diff --git a/app/controllers/topic/pinned_forums_controller.rb b/app/controllers/topic/pinned_forums_controller.rb deleted file mode 100644 index 9717058e7..000000000 --- a/app/controllers/topic/pinned_forums_controller.rb +++ /dev/null @@ -1,6 +0,0 @@ -class Topic::PinnedForumsController < Topic::BaseController - - def index - @pinned_forums = kaminari_paginate(Topic::PinnedForum) - end -end \ No newline at end of file diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb new file mode 100644 index 000000000..207b45870 --- /dev/null +++ b/app/controllers/topics_controller.rb @@ -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 \ No newline at end of file diff --git a/app/models/topic.rb b/app/models/topic.rb index 117912bd0..13bf7b5bd 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -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) diff --git a/app/views/topics/_activity_forum.json.jbuilder b/app/views/topics/_activity_forum.json.jbuilder new file mode 100644 index 000000000..92254a645 --- /dev/null +++ b/app/views/topics/_activity_forum.json.jbuilder @@ -0,0 +1,3 @@ +json.(activity_forum, :id, :title, :url) +json.visits 0 +json.created_time format_time(Time.now) \ No newline at end of file diff --git a/app/views/topics/_banner.json.jbuilder b/app/views/topics/_banner.json.jbuilder new file mode 100644 index 000000000..01fa7c9a2 --- /dev/null +++ b/app/views/topics/_banner.json.jbuilder @@ -0,0 +1 @@ +json.(banner, :id, :title, :image) \ No newline at end of file diff --git a/app/views/topics/_card.json.jbuilder b/app/views/topics/_card.json.jbuilder new file mode 100644 index 000000000..9f4617a3a --- /dev/null +++ b/app/views/topics/_card.json.jbuilder @@ -0,0 +1 @@ +json.(card, :id, :title, :url) \ No newline at end of file diff --git a/app/views/topics/_cooperator.json.jbuilder b/app/views/topics/_cooperator.json.jbuilder new file mode 100644 index 000000000..ef4fc5b9a --- /dev/null +++ b/app/views/topics/_cooperator.json.jbuilder @@ -0,0 +1 @@ +json.(cooperator, :id, :title, :image, :url) diff --git a/app/views/topics/_excellent_project.json.jbuilder b/app/views/topics/_excellent_project.json.jbuilder new file mode 100644 index 000000000..341694add --- /dev/null +++ b/app/views/topics/_excellent_project.json.jbuilder @@ -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 diff --git a/app/views/topics/_experience_forum.json.jbuilder b/app/views/topics/_experience_forum.json.jbuilder new file mode 100644 index 000000000..d52703c32 --- /dev/null +++ b/app/views/topics/_experience_forum.json.jbuilder @@ -0,0 +1,3 @@ +json.(experience_forum, :id, :title, :url) +json.visits 0 +json.created_time format_time(Time.now) \ No newline at end of file diff --git a/app/views/topics/_pinned_forum.json.jbuilder b/app/views/topics/_pinned_forum.json.jbuilder new file mode 100644 index 000000000..18a5fdd16 --- /dev/null +++ b/app/views/topics/_pinned_forum.json.jbuilder @@ -0,0 +1,3 @@ +json.(pinned_forum, :id, :title, :url) +json.visits 0 +json.created_time format_time(Time.now) \ No newline at end of file diff --git a/app/views/topics/index.json.jbuilder b/app/views/topics/index.json.jbuilder new file mode 100644 index 000000000..eb14988a7 --- /dev/null +++ b/app/views/topics/index.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 146cb4913..215e2f0d4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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