From a43f6714de81e1a625eab9441f58553a333ef96b Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 27 Oct 2021 14:20:27 +0800 Subject: [PATCH] add: project explore api --- .../project_categories_controller.rb | 4 +++ app/controllers/projects_controller.rb | 10 ++++-- app/models/project.rb | 5 ++- app/models/project_category.rb | 6 ++++ app/queries/projects/list_query.rb | 3 +- .../pinned_index.json.jbuilder | 1 + .../projects/banner_recommend.json.jbuilder | 35 +++++++++++++++++++ app/views/projects/index.json.jbuilder | 2 +- config/routes.rb | 2 ++ ..._some_columns_to_project_detail_feature.rb | 7 ++++ 10 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 app/views/project_categories/pinned_index.json.jbuilder create mode 100644 app/views/projects/banner_recommend.json.jbuilder create mode 100644 db/migrate/20211027032626_add_some_columns_to_project_detail_feature.rb diff --git a/app/controllers/project_categories_controller.rb b/app/controllers/project_categories_controller.rb index 106ff7f22..67a040fef 100644 --- a/app/controllers/project_categories_controller.rb +++ b/app/controllers/project_categories_controller.rb @@ -5,6 +5,10 @@ class ProjectCategoriesController < ApplicationController @project_categories = q.result(distinct: true) end + def pinned_index + @project_categories = ProjectCategory.where.not(pinned_index: 0).order(pinned_index: :desc) + end + def group_list @project_categories = ProjectCategory.where('projects_count > 0').order(projects_count: :desc) # projects = Project.no_anomory_projects.visible diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index 4f48e53dc..fc0bc41e1 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -6,7 +6,7 @@ class ProjectsController < ApplicationController before_action :require_login, except: %i[index branches branches_slice group_type_list simple show fork_users praise_users watch_users recommend about menu_list] before_action :require_profile_completed, only: [:create, :migrate] - before_action :load_repository, except: %i[index group_type_list migrate create recommend] + before_action :load_repository, except: %i[index group_type_list migrate create recommend banner_recommend] before_action :authorizate_user_can_edit_project!, only: %i[update] before_action :project_public?, only: %i[fork_users praise_users watch_users] @@ -30,8 +30,8 @@ class ProjectsController < ApplicationController def index scope = current_user.logged? ? Projects::ListQuery.call(params, current_user.id) : Projects::ListQuery.call(params) - # @projects = kaminari_paginate(scope) - @projects = paginate scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units) + @projects = kaminari_paginate(scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units)) + # @projects = paginate scope.includes(:project_category, :project_language, :repository, :project_educoder, :owner, :project_units) category_id = params[:category_id] @total_count = @@ -199,6 +199,10 @@ class ProjectsController < ApplicationController @projects = Project.recommend.includes(:repository, :project_category, :owner).order(visits: :desc) end + def banner_recommend + @projects = Project.recommend.where.not(recommend_index: 0).includes(:project_category, :owner, :project_language).order(recommend_index: :desc) + end + def about @project_detail = @project.project_detail @attachments = Array(@project_detail&.attachments) if request.get? diff --git a/app/models/project.rb b/app/models/project.rb index 03cad7ca7..d36c13efc 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -55,8 +55,9 @@ # platform :integer default("0") # default_branch :string(255) default("master") # website :string(255) -# order_index :integer default("0") # lesson_url :string(255) +# is_pinned :boolean default("0") +# recommend_index :integer default("0") # # Indexes # @@ -79,6 +80,7 @@ + class Project < ApplicationRecord include Matchable include Publicable @@ -134,6 +136,7 @@ class Project < ApplicationRecord scope :project_statics_select, -> {select(:id,:name, :is_public, :identifier, :status, :project_type, :user_id, :forked_count, :visits, :project_category_id, :project_language_id, :license_id, :ignore_id, :watchers_count, :created_on)} scope :no_anomory_projects, -> {where("projects.user_id is not null and projects.user_id != ?", 2)} scope :recommend, -> { visible.project_statics_select.where(recommend: true) } + scope :pinned, -> {where(is_pinned: true)} delegate :content, to: :project_detail, allow_nil: true delegate :name, to: :license, prefix: true, allow_nil: true diff --git a/app/models/project_category.rb b/app/models/project_category.rb index 67b802998..df8962e00 100644 --- a/app/models/project_category.rb +++ b/app/models/project_category.rb @@ -8,6 +8,12 @@ # projects_count :integer default("0") # created_at :datetime not null # updated_at :datetime not null +# ancestry :string(255) +# pinned_index :integer default("0") +# +# Indexes +# +# index_project_categories_on_ancestry (ancestry) # class ProjectCategory < ApplicationRecord diff --git a/app/queries/projects/list_query.rb b/app/queries/projects/list_query.rb index 4658408d2..4f514b610 100644 --- a/app/queries/projects/list_query.rb +++ b/app/queries/projects/list_query.rb @@ -11,7 +11,8 @@ class Projects::ListQuery < ApplicationQuery end def call - q = Project.visible.by_name_or_identifier(params[:search]) + q = params[:pinned].present? ? Project.pinned : Project + q = q.visible.by_name_or_identifier(params[:search]) scope = q .with_project_type(params[:project_type]) diff --git a/app/views/project_categories/pinned_index.json.jbuilder b/app/views/project_categories/pinned_index.json.jbuilder new file mode 100644 index 000000000..c2f2e024c --- /dev/null +++ b/app/views/project_categories/pinned_index.json.jbuilder @@ -0,0 +1 @@ +json.project_categories @project_categories, :id, :name diff --git a/app/views/projects/banner_recommend.json.jbuilder b/app/views/projects/banner_recommend.json.jbuilder new file mode 100644 index 000000000..ca672373a --- /dev/null +++ b/app/views/projects/banner_recommend.json.jbuilder @@ -0,0 +1,35 @@ +json.partial! "commons/success" +json.projects do + json.array! @projects do |project| + owner = project.owner + json.id project.id + json.identifier project.identifier + json.name project.name + json.visits project.visits + json.author do + json.name owner.try(:show_real_name) + json.type owner.type + json.login owner.login + json.image_url url_to_avatar(owner) + end + + json.category do + if project.project_category.blank? + json.nil! + else + json.id project.project_category.id + json.name project.project_category.name + end + end + + json.language do + if project.project_language.blank? + json.nil! + else + json.id project.project_language.id + json.name project.project_language.name + end + end + end +end + diff --git a/app/views/projects/index.json.jbuilder b/app/views/projects/index.json.jbuilder index 96ab89c00..874f60974 100644 --- a/app/views/projects/index.json.jbuilder +++ b/app/views/projects/index.json.jbuilder @@ -1,4 +1,4 @@ -json.total_count @total_count +json.total_count @projects.total_count json.projects @projects do |project| # json.partial! "/projects/project_detail", project: project json.id project.id diff --git a/config/routes.rb b/config/routes.rb index 7f78cc8dd..c4afbe4c3 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -159,6 +159,7 @@ Rails.application.routes.draw do resources :project_categories, only: [:index, :show] do get :group_list, on: :collection + get :pinned_index, on: :collection end resources :project_languages, only: [:index, :show] resources :ignores, only: [:index, :show] @@ -184,6 +185,7 @@ Rails.application.routes.draw do post :migrate get :group_type_list get :recommend + get :banner_recommend end end diff --git a/db/migrate/20211027032626_add_some_columns_to_project_detail_feature.rb b/db/migrate/20211027032626_add_some_columns_to_project_detail_feature.rb new file mode 100644 index 000000000..4a4aef328 --- /dev/null +++ b/db/migrate/20211027032626_add_some_columns_to_project_detail_feature.rb @@ -0,0 +1,7 @@ +class AddSomeColumnsToProjectDetailFeature < ActiveRecord::Migration[5.2] + def change + add_column :project_categories, :pinned_index, :integer, default: 0 + add_column :projects, :is_pinned, :boolean, default: false + add_column :projects, :recommend_index, :integer, default: 0 + end +end