From 86f0f2051d357a466fcf485d0ceac8e20da82081 Mon Sep 17 00:00:00 2001 From: "vilet.yy" Date: Mon, 31 May 2021 18:39:49 +0800 Subject: [PATCH] add: update user_info --- app/controllers/users_controller.rb | 15 ++- app/docs/slate/source/includes/_users.md | 53 +++++++- app/models/user.rb | 2 +- app/models/user_extension.rb | 6 +- app/views/users/show.json.jbuilder | 7 +- ...531100250_add_fields_to_user_extensions.rb | 8 ++ public/docs/api.html | 124 ++++++++++++++---- 7 files changed, 180 insertions(+), 35 deletions(-) create mode 100644 db/migrate/20210531100250_add_fields_to_user_extensions.rb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3c62b1145..4cbb2cbdf 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -72,9 +72,13 @@ class UsersController < ApplicationController end def update - @user = User.find params[:id] - @user.update!(user_params) - render_ok + return render_not_found unless @user = User.find_by_id(params[:id]) || User.find_by(login: params[:id]) + @user.attributes = user_params + if @user.save + render_ok + else + render_error(@user.errors.full_messages.join(", ")) + end end def me @@ -274,11 +278,12 @@ class UsersController < ApplicationController end def user_params - params.require(:user).permit(:nickname, :lastname, :show_realname,:login,:mail, + params.require(:user).permit(:nickname, user_extension_attributes: [ :gender, :location, :location_city, :occupation, :technical_title, - :school_id, :department_id,:identity, :student_id, :description] + :school_id, :department_id, :province, :city, + :custom_department, :identity, :student_id, :description] ) end diff --git a/app/docs/slate/source/includes/_users.md b/app/docs/slate/source/includes/_users.md index d7e8ed488..573e171eb 100644 --- a/app/docs/slate/source/includes/_users.md +++ b/app/docs/slate/source/includes/_users.md @@ -1,7 +1,7 @@ # Users @@ -47,6 +47,57 @@ await octokit.request('GET /api/users/me.json') Success Data. +## 更改用户信息 +更改用户信息 + +> 示例: + +```shell +curl -X POST http://localhost:3000/api/users/yystopf.json +``` + +```javascript +await octokit.request('PATCH/PUT /api/users/:login.json') +``` + +### HTTP 请求 +`PATCH/PUT /api/users/:login.json` + +### 请求字段说明: +参数 | 类型 | 字段说明 +--------- | ----------- | ----------- +|user.nickname |string |用户昵称 | +|user.user_extension_attributes.gender |int |性别, 0男 1女 | +|user.user_extension_attributes.province |string |省份 | +|user.user_extension_attributes.city |string |城市 | +|user.user_extension_attributes.description |string |个性签名 | +|user.user_extension_attributes.custom_department|string |单位名称 | + +> 请求的JSON示例: + +```json +{ + "user": { + "nickname": "xxx", + "user_extension_attributes": { + "gender": 0, + "province": "湖南", + "city": "长沙", + "description": "个性签名", + "custom_department": "湖南智擎科技有限公司", + } + } +} +``` + +> 返回的JSON示例: + +```json +{ + "status": 0, + "message": "success" +} +``` ## 获取用户星标项目 获取用户星标项目 diff --git a/app/models/user.rb b/app/models/user.rb index 56043f5f6..e190e82ea 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -181,7 +181,7 @@ class User < Owner attr_accessor :password, :password_confirmation - delegate :gender, :department_id, :school_id, :location, :location_city, :technical_title, to: :user_extension, allow_nil: true + delegate :description, :gender, :department_id, :school_id, :location, :location_city, :technical_title, :province, :city, :custom_department, to: :user_extension, allow_nil: true before_save :update_hashed_password after_create do diff --git a/app/models/user_extension.rb b/app/models/user_extension.rb index 4afd89bd5..56eed12bd 100644 --- a/app/models/user_extension.rb +++ b/app/models/user_extension.rb @@ -22,9 +22,9 @@ # school_id :integer # description :string(255) default("") # department_id :integer -# honor :text(65535) -# edu_background :integer -# edu_entry_year :integer +# province :string(255) +# city :string(255) +# custom_department :string(255) # # Indexes # diff --git a/app/views/users/show.json.jbuilder b/app/views/users/show.json.jbuilder index c529cf79f..560c207d6 100644 --- a/app/views/users/show.json.jbuilder +++ b/app/views/users/show.json.jbuilder @@ -18,4 +18,9 @@ json.user_org_count @user_org_count json.common_projects_count @projects_common_count json.mirror_projects_count @projects_mirrior_count json.sync_mirror_projects_count @projects_sync_mirrior_count -json.created_time format_time(@user.created_on) \ No newline at end of file +json.created_time format_time(@user.created_on) +json.email @user.mail +json.province @user.province +json.city @user.city +json.custom_department @user.custom_department +json.description @user.description \ No newline at end of file diff --git a/db/migrate/20210531100250_add_fields_to_user_extensions.rb b/db/migrate/20210531100250_add_fields_to_user_extensions.rb new file mode 100644 index 000000000..880438405 --- /dev/null +++ b/db/migrate/20210531100250_add_fields_to_user_extensions.rb @@ -0,0 +1,8 @@ +class AddFieldsToUserExtensions < ActiveRecord::Migration[5.2] + def change + add_column :user_extensions, :province, :string # 省份 + add_column :user_extensions, :city, :string # 城市 + add_column :user_extensions, :custom_department, :string #自己填写的单位名称 + remove_column :users, :description + end +end diff --git a/public/docs/api.html b/public/docs/api.html index 616803e4e..e713ea1d0 100644 --- a/public/docs/api.html +++ b/public/docs/api.html @@ -331,6 +331,9 @@
  • 获取当前登陆用户信息
  • +
  • + 更改用户信息 +
  • 获取用户星标项目
  • @@ -611,7 +614,7 @@ Success — a happy kitten is an authenticated kitten!

    Users

    获取当前登陆用户信息

    @@ -673,7 +676,80 @@ Success — a happy kitten is an authenticated kitten! -

    获取用户星标项目

    +

    更改用户信息

    +

    更改用户信息

    + +
    +

    示例:

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

    HTTP 请求

    +

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

    +

    请求字段说明:

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    参数类型字段说明
    user.nicknamestring用户昵称
    user.user_extension_attributes.genderint性别, 0男 1女
    user.user_extension_attributes.provincestring省份
    user.user_extension_attributes.citystring城市
    user.user_extension_attributes.descriptionstring个性签名
    user.user_extension_attributes.custom_departmentstring单位名称
    + +
    +

    请求的JSON示例:

    +
    +
    {
    +    "user": {
    +        "nickname": "xxx",
    +        "user_extension_attributes": {
    +            "gender": 0,
    +            "province": "湖南",
    +            "city": "长沙",
    +            "description": "个性签名",
    +            "custom_department": "湖南智擎科技有限公司",
    +        }
    +    }
    +}
    +
    +
    +

    返回的JSON示例:

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

    获取用户星标项目

    获取用户星标项目

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

    HTTP 请求

    +

    HTTP 请求

    GET api/users/:login/is_pinned_projects.json

    返回字段说明:

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

    HTTP 请求

    +

    HTTP 请求

    POST /api/users/:login/is_pinned_projects/pin.json

    -

    请求字段说明:

    同时设定多个星标项目

    +

    请求字段说明:

    同时设定多个星标项目

    @@ -909,7 +985,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

    返回字段说明:

    参数
    @@ -998,9 +1074,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

    -

    请求字段说明:

    +

    请求字段说明:

    @@ -1143,9 +1219,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1460,9 +1536,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1603,9 +1679,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1685,9 +1761,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1746,9 +1822,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -1983,9 +2059,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -2175,9 +2251,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    @@ -2366,9 +2442,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

    -

    请求字段说明:

    +

    请求字段说明:

    参数
    参数