From 16c293562ac284892c17b9b1a6fabfa2c7c2e0cf Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Wed, 2 Jun 2021 16:40:40 +0800 Subject: [PATCH] add: pinned projects order by position --- .../users/is_pinned_projects_controller.rb | 19 +++- app/docs/slate/source/includes/_users.md | 44 ++++++++- .../is_pinned_projects/index.json.jbuilder | 4 +- config/routes.rb | 2 +- public/docs/api.html | 91 ++++++++++++++----- 5 files changed, 134 insertions(+), 26 deletions(-) diff --git a/app/controllers/users/is_pinned_projects_controller.rb b/app/controllers/users/is_pinned_projects_controller.rb index 4f3f4e37..1ddadd27 100644 --- a/app/controllers/users/is_pinned_projects_controller.rb +++ b/app/controllers/users/is_pinned_projects_controller.rb @@ -1,7 +1,7 @@ class Users::IsPinnedProjectsController < Users::BaseController before_action :private_user_resources!, only: [:pin] def index - @is_pinned_projects = observed_user.is_pinned_projects.includes(:project_category, :project_language, :repository).order(position: :desc) + @is_pinned_projects = observed_user.pinned_projects.order(position: :desc, created_at: :asc).includes(project: [:project_category, :project_language, :repository]).order(position: :desc) @is_pinned_projects = kaminari_paginate(@is_pinned_projects) end @@ -15,6 +15,19 @@ class Users::IsPinnedProjectsController < Users::BaseController tip_exception(e.message) end + def update + @pinned_project = PinnedProject.find_by_id(params[:id]) + @pinned_project.attributes = pinned_project_params + if @pinned_project.save + render_ok + else + render_error + end + rescue Exception => e + uid_logger_error(e.message) + tip_exception(e.message) + end + private def is_pinned_project_ids if params[:is_pinned_project_ids].present? @@ -25,4 +38,8 @@ class Users::IsPinnedProjectsController < Users::BaseController return observed_user.is_pinned_project_ids.include?(params[:is_pinned_project_id].to_i) ? observed_user.is_pinned_project_ids : observed_user.is_pinned_project_ids.push(params[:is_pinned_project_id].to_i) end end + + def pinned_project_params + params.require(:pinned_project).permit(:position) + end end \ No newline at end of file diff --git a/app/docs/slate/source/includes/_users.md b/app/docs/slate/source/includes/_users.md index b72ee0c1..5771f757 100644 --- a/app/docs/slate/source/includes/_users.md +++ b/app/docs/slate/source/includes/_users.md @@ -1,7 +1,7 @@ # Users @@ -53,7 +53,7 @@ await octokit.request('GET /api/users/me.json') > 示例: ```shell -curl -X POST http://localhost:3000/api/users/yystopf.json +curl -X PATCH/PUT http://localhost:3000/api/users/yystopf.json ``` ```javascript @@ -141,6 +141,7 @@ await octokit.request('GET /api/users/:login/is_pinned_projects.json') |author.image_url |string |项目拥有者头像| |category.name |string |项目分类名称| |language.name |string |项目语言名称| +|position |int |项目排序| > 返回的JSON示例: @@ -228,6 +229,45 @@ await octokit.request('GET /api/users/:login/is_pinned_projects/pin.json') ``` +## 星标项目展示排序 +星标项目展示排序 + +> 示例: + +```shell +curl -X PATCH http://localhost:3000/api/users/yystopf/is_pinned_projects/11.json +``` + +```javascript +await octokit.request('PATCH/PUT /api/users/:login/is_pinned_projects/:id.json') +``` + +### HTTP 请求 +`PATCH/PUT /api/users/:login/is_pinned_projects/:id.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|pinned_projects.position |int |排序,数字越大排名越前 | + +> 请求的JSON示例: + +```json +{ + "pinned_project": { + "position": 1 + } +} +``` + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` ## 用户近期活动统计 用户近期活动统计, 默认显示近一周的数据 diff --git a/app/views/users/is_pinned_projects/index.json.jbuilder b/app/views/users/is_pinned_projects/index.json.jbuilder index facc158b..a96ecf98 100644 --- a/app/views/users/is_pinned_projects/index.json.jbuilder +++ b/app/views/users/is_pinned_projects/index.json.jbuilder @@ -1,4 +1,6 @@ json.total_count @is_pinned_projects.total_count json.projects @is_pinned_projects.each do |project| - json.partial! "projects/project_detail", project: project + json.partial! "projects/project_detail", project: project&.project + json.id project.id + json.position project.position end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index cfadf494..1160a407 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -266,7 +266,7 @@ Rails.application.routes.draw do end end resources :headmaps, only: [:index] - resources :is_pinned_projects, only: [:index] do + resources :is_pinned_projects, only: [:index, :update] do collection do post :pin end diff --git a/public/docs/api.html b/public/docs/api.html index 71753c32..c8fa29fa 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -340,6 +340,9 @@
  • 用户添加星标项目
  • +
  • + 星标项目展示排序 +
  • 用户近期活动统计
  • @@ -614,7 +617,7 @@ Success — a happy kitten is an authenticated kitten!

    Users

    获取当前登陆用户信息

    @@ -682,7 +685,7 @@ Success — a happy kitten is an authenticated kitten!

    示例:

    -
    curl -X POST http://localhost:3000/api/users/yystopf.json
    +
    curl -X PATCH/PUT http://localhost:3000/api/users/yystopf.json
     
    await octokit.request('PATCH/PUT /api/users/:login.json')
     

    HTTP 请求

    PATCH/PUT /api/users/:login.json

    @@ -887,6 +890,11 @@ Success — a happy kitten is an authenticated kitten! string 项目语言名称 + +position +int +项目排序 +
    @@ -975,6 +983,47 @@ Success — a happy kitten is an authenticated kitten! +
    +

    返回的JSON示例:

    +
    +
    {
    +    "status": 0,
    +    "message": "success"
    +}
    +

    星标项目展示排序

    +

    星标项目展示排序

    + +
    +

    示例:

    +
    +
    curl -X PATCH http://localhost:3000/api/users/yystopf/is_pinned_projects/11.json
    +
    await octokit.request('PATCH/PUT /api/users/:login/is_pinned_projects/:id.json')
    +

    HTTP 请求

    +

    PATCH/PUT /api/users/:login/is_pinned_projects/:id.json

    +

    请求字段说明:

    + + + + + + + + + + + + +
    参数类型字段说明
    pinned_projects.positionint排序,数字越大排名越前
    + +
    +

    请求的JSON示例:

    +
    +
    {
    +    "pinned_project": {
    +        "position": 1
    +    }
    +}
    +

    返回的JSON示例:

    @@ -990,7 +1039,7 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/statistics/activity.json
     
    await octokit.request('GET /api/users/:login/statistics/activity.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/statistics/activity.json

    返回字段说明:

    @@ -1079,9 +1128,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/headmaps.json
     
    await octokit.request('GET /api/users/:login/headmaps.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET api/users/:login/headmaps.json

    -

    请求字段说明:

    +

    请求字段说明:

    @@ -1224,9 +1273,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/project_trends.json
     
    await octokit.request('GET /api/users/:login/project_trends.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET api/users/:login/project_trends.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1541,9 +1590,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/statistics/develop.json
     
    await octokit.request('GET /api/users/:login/statistics/develop.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/statistics/develop.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1684,9 +1733,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/statistics/role.json
     
    await octokit.request('GET /api/users/:login/statistics/role.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/statistics/role.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1766,9 +1815,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/statistics/major.json
     
    await octokit.request('GET /api/users/:login/statistics/major.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/statistics/major.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1827,9 +1876,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/applied_messages.json
     
    await octokit.request('GET /api/users/:login/applied_messages.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/applied_messages.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -2064,9 +2113,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X GET http://localhost:3000/api/users/yystopf/applied_transfer_projects.json
     
    await octokit.request('GET /api/users/:login/applied_transfer_projects.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/applied_transfer_projects.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -2256,9 +2305,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X POST http://localhost:3000/api/users/yystopf/applied_transfer_projects/2/accept.json
     
    await octokit.request('GET /api/users/:login/applied_transfer_projects/:id/accept.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/applied_transfer_projects/:id/accept.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -2447,9 +2496,9 @@ Success — a happy kitten is an authenticated kitten!
    curl -X POST http://localhost:3000/api/users/yystopf/applied_transfer_projects/2/refuse.json
     
    await octokit.request('GET /api/users/:login/applied_transfer_projects/:id/refuse.json')
    -

    HTTP 请求

    +

    HTTP 请求

    GET /api/users/:login/applied_transfer_projects/:id/refuse.json

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    参数