Browse Source

新增:openkylin_sign 签署协议

pull/342/head
yystopf 3 years ago
parent
commit
e9da21bccf
6 changed files with 113 additions and 2 deletions
  1. +23
    -0
      app/controllers/api/v1/users/openkylin_sign_controller.rb
  2. +2
    -2
      app/controllers/api/v1/users_controller.rb
  3. +24
    -0
      app/models/openkylin_sign_detail.rb
  4. +45
    -0
      app/services/api/v1/users/openkylin_sign/create_service.rb
  5. +5
    -0
      config/routes/api.rb
  6. +14
    -0
      db/migrate/20230420092835_create_openkylin_sign_details.rb

+ 23
- 0
app/controllers/api/v1/users/openkylin_sign_controller.rb View File

@@ -0,0 +1,23 @@
class Api::V1::Users::OpenkylinSignController < Api::V1::BaseController

before_action :load_observe_user

def competitions
@competition_ids = EduSetting.get("openkylin_sign_competitions").split(",") rescue []
render :json => {data: @competition_ids}
end

def create
@object_result = Api::V1::Users::OpenkylinSign::CreateService.call(@observe_user, create_params)
if @result_object
return render_ok
else
return render_error('签署失败!')
end
end

private
def create_params
params.permit(:login, :email, :nickname, :phone, :address)
end
end

+ 2
- 2
app/controllers/api/v1/users_controller.rb View File

@@ -1,7 +1,7 @@
class Api::V1::UsersController < Api::V1::BaseController

before_action :load_observe_user, except: [:check_user_id]
before_action :check_auth_for_observe_user, except: [:check_user_id]
before_action :load_observe_user, except: [:check_user_id, :check_user_login]
before_action :check_auth_for_observe_user, except: [:check_user_id, :check_user_login]

def check_user_id
return tip_exception(-1, "用户ID不存在") unless params[:user_id].present? && User.exists?(id: params[:user_id])


+ 24
- 0
app/models/openkylin_sign_detail.rb View File

@@ -0,0 +1,24 @@
# == Schema Information
#
# Table name: openkylin_sign_details
#
# id :integer not null, primary key
# user_id :integer
# login :string(255)
# email :string(255)
# nickname :string(255)
# phone :string(255)
# address :string(255)
# created_at :datetime not null
# updated_at :datetime not null
#
# Indexes
#
# index_openkylin_sign_details_on_user_id (user_id)
#

class OpenkylinSignDetail < ApplicationRecord

belongs_to :user
end

+ 45
- 0
app/services/api/v1/users/openkylin_sign/create_service.rb View File

@@ -0,0 +1,45 @@
class Api::V1::Users::OpenkylinSign::CreateService < ApplicationService
include ActiveModel::Model

attr_reader :observe_user, :login, :email, :nickname, :phone, :address

# validates :login, format: {with: CustomRegexp::LOGIN}
validates :email, format: {with: CustomRegexp::EMAIL}
validates :nickname, length: { maximum: 32 }
validates :phone, format: {with: CustomRegexp::PHONE}
validates :address, length: { maximum: 100 }

def initialize(observe_user, params={})
@observe_user = observe_user
@login = observe_user.login
@email = params[:email]
@nickname = params[:nickname]
@phone = params[:phone]
@address = params[:address]
end

def call
raise Error, errors.full_messages.join(",") unless valid?
raise Error, '用户已经签署CLA协议!' if @observe_user.sign_cla
begin
ActiveRecord::Base.transaction do
create_openkylin_sign_detail
update_user_sign_cla
end
return true
rescue
raise Error, "服务器错误,请联系系统管理员!"
end
end

private
def create_openkylin_sign_detail
OpenkylinSignDetail.create!(user_id: @observe_user.id, login: @login, email: @email, nickname: @nickname, phone: @phone, address: @address)
end

def update_user_sign_cla
@observe_user.update_attributes!(sign_cla: true)
end
end

+ 5
- 0
config/routes/api.rb View File

@@ -24,6 +24,11 @@ defaults format: :json do
scope module: :users do
resources :projects, only: [:index]
resources :feedbacks, only: [:create]
resources :openkylin_sign, only: [:create] do
collection do
get :competitions
end
end
end

scope ':repo', constraints: { repo: /[^\/]+/ } do


+ 14
- 0
db/migrate/20230420092835_create_openkylin_sign_details.rb View File

@@ -0,0 +1,14 @@
class CreateOpenkylinSignDetails < ActiveRecord::Migration[5.2]
def change
create_table :openkylin_sign_details do |t|
t.references :user
t.string :login
t.string :email
t.string :nickname
t.string :phone
t.string :address

t.timestamps
end
end
end

Loading…
Cancel
Save