diff --git a/README.md b/README.md index 8450de92..7d65e6f1 100644 --- a/README.md +++ b/README.md @@ -1364,6 +1364,150 @@ http://localhost:3000/api/projects | jq ``` --- +#### 推荐项目 +``` +GET api/projects/recommend +``` +*示例* +``` +curl -X GET \ +http://localhost:3000/api/projects/recommend | jq +``` + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|total_count |int |项目总条数 | +|id |string |项目id | +|name |string|项目名称| +|description |string|项目简介| +|visits |int|流量数| +|forked_count |int|被fork的数量| +|praises_count |int|star数量| +|is_public |boolean|是否公开, true:公开,false:未公开| +|mirror_url |string|镜像url| +|last_update_time|int|最后更新时间,为UNIX格式的时间戳| +|author |object|项目创建者| +|-- name |string|用户名,也是用户标识| +|category |object|项目类别| +|-- id |int|项目类型id| +|-- name |string|项目类型名称| +|language |object|项目语言| +|-- id |int|项目语言id| +|-- name |string|项目语言名称| + + +返回值 +``` +[ + { + "id": 20, + "repo_id": null, + "identifier": "PNAekinmH", + "name": "FNILL", + "visits": 13567, + "author": { + "name": "王一达", + "login": "wangyida", + "image_url": "avatars/User/b" + }, + "category": { + "id": 8, + "name": "其他" + } + }, + ... +] + +``` +--- + +#### 项目主页 +``` +GET api/:namespace_id/:id/about +``` + +*示例* +``` +curl -X GET \ +http://localhost:3000/api/:jason/forgeplus/about | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|namespace_id |是|string |用户登录名 | +|id |是|string |项目标识identifier | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|identifier |string|project's identifier| +|content |string|主页内容| +|attachments |array|附件| +|-- name |string|用户名,也是用户标识| + + +返回值 +``` +{ + "content": "", + "identifier": "forgeplus", + attachments: [ + + ] +} + +``` +--- + +#### 修改项目主页内容 +``` +POST api/:namespace_id/:id/about +``` + +*示例* +``` +curl -X POST \ +-d "content=内容" \ +-d "attachment_ids=[1, 2, 2]" \ +http://localhost:3000/api/:jasder/forgeplus/about | jq +``` + +*请求参数说明:* + +|参数名|必选|类型|说明| +|-|-|-|-| +|namespace_id |是|string |用户登录名 | +|id |是|string |项目标识identifier | +|content |是|string |内容信息 | +|attachment_ids |是|array |附件id | + +*返回参数说明:* + +|参数名|类型|说明| +|-|-|-| +|identifier |string|project's identifier| +|content |string|主页内容| +|attachments |array|附件| +|-- name |string|用户名,也是用户标识| + +返回值 +``` +{ + "content": "", + "identifier": "forgeplus", + attachments: [ + + ] +} + +``` +--- + ### 获取分支列表 ``` GET /api/:namespace_id/:id/branches diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b4fd3e3b..25362244 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -2,8 +2,8 @@ class ProjectsController < ApplicationController include ApplicationHelper include OperateProjectAbilityAble include ProjectsHelper - before_action :require_login, except: %i[index branches group_type_list simple show fork_users praise_users watch_users] - before_action :load_project, except: %i[index group_type_list migrate create] + before_action :require_login, except: %i[index branches group_type_list simple show fork_users praise_users watch_users recommend about] + before_action :load_project, except: %i[index group_type_list migrate create recommend] before_action :authorizate_user_can_edit_project!, only: %i[update] before_action :project_public?, only: %i[fork_users praise_users watch_users] @@ -103,6 +103,38 @@ class ProjectsController < ApplicationController json_response(@project) end + def recommend + @projects = Project.recommend.includes(:repository, :project_category, owner: :user_extension).limit(5) + end + + def about + @project_detail = @project.project_detail + @attachments = Array(@project_detail&.attachments) if request.get? + ActiveRecord::Base.transaction do + if request.post? + require_login + authorizate_user_can_edit_project! + unless @project_detail.present? + @project_detail = ProjectDetail.new( + content: params[:content], + project_id: @project.id) + else + @project_detail.content = params[:content] + end + if @project_detail.save! + attachment_ids = Array(params[:attachment_ids]) + logger.info "=============> #{Array(params[:attachment_ids])}" + @attachments = Attachment.where(id: attachment_ids) + @attachments.update_all( + container_id: @project_detail.id, + container_type: @project_detail.model_name.name, + author_id: current_user.id, + description: "") + end + end + end + end + private def project_params diff --git a/app/models/project.rb b/app/models/project.rb index 24f8a85d..49dc6409 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -32,10 +32,13 @@ class Project < ApplicationRecord has_many :versions, -> { order("versions.created_on DESC, versions.name DESC") }, dependent: :destroy has_many :praise_treads, as: :praise_tread_object, dependent: :destroy has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position" + has_one :project_detail, dependent: :destroy after_save :check_project_members 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) } + def self.search_project(search) diff --git a/app/models/project_detail.rb b/app/models/project_detail.rb new file mode 100644 index 00000000..a2d088ba --- /dev/null +++ b/app/models/project_detail.rb @@ -0,0 +1,4 @@ +class ProjectDetail < ApplicationRecord + belongs_to :project, optional: true + has_many :attachments, as: :container, dependent: :destroy +end diff --git a/app/views/projects/about.json.jbuilder b/app/views/projects/about.json.jbuilder new file mode 100644 index 00000000..72041ca6 --- /dev/null +++ b/app/views/projects/about.json.jbuilder @@ -0,0 +1,5 @@ +json.content @project_detail&.content +json.identifier @project.identifier +json.attachments @attachments do |attach| + json.partial! "attachments/attachment_simple", locals: {attachment: attach} +end diff --git a/app/views/projects/recommend.json.jbuilder b/app/views/projects/recommend.json.jbuilder new file mode 100644 index 00000000..1da5be40 --- /dev/null +++ b/app/views/projects/recommend.json.jbuilder @@ -0,0 +1,22 @@ +json.array! @projects do |project| + owner = project.owner + json.id project.id + json.repo_id project&.repository&.id + json.identifier project.identifier + json.name project.name + json.visits project.visits + json.author do + json.name owner.try(:show_real_name) + 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 +end diff --git a/config/routes.rb b/config/routes.rb index 7c28a012..663abb54 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -33,7 +33,7 @@ Rails.application.routes.draw do get 'attachments/entries/get_file', to: 'attachments#get_file' get 'attachments/download/:id', to: 'attachments#show' get 'attachments/download/:id/:filename', to: 'attachments#show' - + get 'auth/qq/callback', to: 'oauth/qq#create' get 'auth/failure', to: 'oauth/base#auth_failure' get 'auth/cas/callback', to: 'oauth/cas#create' @@ -41,7 +41,7 @@ Rails.application.routes.draw do get 'oauth/bind', to: 'oauth/educoder#bind' get 'oauth/register', to: 'oauth#register' post 'oauth/auto_register', to: 'oauth#auto_register' - + resources :edu_settings scope '/api' do @@ -100,7 +100,7 @@ Rails.application.routes.draw do resources :compose_projects, only: [:create, :destroy] end resources :attachments do - member do + member do post :preview_attachment end collection do @@ -156,6 +156,7 @@ Rails.application.routes.draw do collection do post :migrate get :group_type_list + get :recommend end end @@ -321,6 +322,7 @@ Rails.application.routes.draw do get :watchers, to: 'projects#watch_users' get :stargazers, to: 'projects#praise_users' get :members, to: 'projects#fork_users' + match :about, :via => [:get, :put, :post] end end diff --git a/db/migrate/20200929062837_add_index_to_users_type.rb b/db/migrate/20200929062837_add_index_to_users_type.rb new file mode 100644 index 00000000..d27ca9dd --- /dev/null +++ b/db/migrate/20200929062837_add_index_to_users_type.rb @@ -0,0 +1,5 @@ +class AddIndexToUsersType < ActiveRecord::Migration[5.2] + def change + add_index :users, :type + end +end diff --git a/db/migrate/20201004034434_add_recommend_to_projects.rb b/db/migrate/20201004034434_add_recommend_to_projects.rb new file mode 100644 index 00000000..d00b3eb6 --- /dev/null +++ b/db/migrate/20201004034434_add_recommend_to_projects.rb @@ -0,0 +1,5 @@ +class AddRecommendToProjects < ActiveRecord::Migration[5.2] + def change + add_column :projects, :recommend, :boolean, default: false + end +end diff --git a/db/migrate/20201004070431_create_project_details.rb b/db/migrate/20201004070431_create_project_details.rb new file mode 100644 index 00000000..09b63bd6 --- /dev/null +++ b/db/migrate/20201004070431_create_project_details.rb @@ -0,0 +1,12 @@ +class CreateProjectDetails < ActiveRecord::Migration[5.2] + def change + create_table :project_details do |t| + t.integer :project_id + t.longtext :content + + t.timestamps + end + + add_index :project_details, :project_id + end +end