From c6a7719d4de3186256b7cc7afa4caef4049abbed Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Sun, 4 Oct 2020 12:01:26 +0800 Subject: [PATCH] ADD recommand projects api --- README.md | 59 ++++++++++++++++++++++++++ app/controllers/projects_controller.rb | 8 +++- app/models/project.rb | 2 + config/routes.rb | 7 +-- 4 files changed, 71 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8450de92..0cefd2a1 100644 --- a/README.md +++ b/README.md @@ -1364,6 +1364,65 @@ 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/branches diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b4fd3e3b..513a7736 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] + 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,10 @@ class ProjectsController < ApplicationController json_response(@project) end + def recommend + @projects = Project.recommend.includes(:repository, :project_category, owner: :user_extension).limit(5) + end + private def project_params diff --git a/app/models/project.rb b/app/models/project.rb index 24f8a85d..c268bd40 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -36,6 +36,8 @@ class Project < ApplicationRecord 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/config/routes.rb b/config/routes.rb index c9a277ad..81fc2cce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,7 +11,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' @@ -19,7 +19,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 @@ -33,7 +33,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 @@ -89,6 +89,7 @@ Rails.application.routes.draw do collection do post :migrate get :group_type_list + get :recommend end end