From e9da21bccfd389d19ea978a4d158a3eabc292f76 Mon Sep 17 00:00:00 2001 From: yystopf Date: Fri, 21 Apr 2023 09:23:30 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9Aopenkylin=5Fsign=20?= =?UTF-8?q?=E7=AD=BE=E7=BD=B2=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/v1/users/openkylin_sign_controller.rb | 23 ++++++++++ app/controllers/api/v1/users_controller.rb | 4 +- app/models/openkylin_sign_detail.rb | 24 ++++++++++ .../v1/users/openkylin_sign/create_service.rb | 45 +++++++++++++++++++ config/routes/api.rb | 5 +++ ...420092835_create_openkylin_sign_details.rb | 14 ++++++ 6 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/v1/users/openkylin_sign_controller.rb create mode 100644 app/models/openkylin_sign_detail.rb create mode 100644 app/services/api/v1/users/openkylin_sign/create_service.rb create mode 100644 db/migrate/20230420092835_create_openkylin_sign_details.rb diff --git a/app/controllers/api/v1/users/openkylin_sign_controller.rb b/app/controllers/api/v1/users/openkylin_sign_controller.rb new file mode 100644 index 000000000..d37e76363 --- /dev/null +++ b/app/controllers/api/v1/users/openkylin_sign_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 2204ff89f..47087c523 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -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]) diff --git a/app/models/openkylin_sign_detail.rb b/app/models/openkylin_sign_detail.rb new file mode 100644 index 000000000..7f9000cf5 --- /dev/null +++ b/app/models/openkylin_sign_detail.rb @@ -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 diff --git a/app/services/api/v1/users/openkylin_sign/create_service.rb b/app/services/api/v1/users/openkylin_sign/create_service.rb new file mode 100644 index 000000000..e9703d9dc --- /dev/null +++ b/app/services/api/v1/users/openkylin_sign/create_service.rb @@ -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 \ No newline at end of file diff --git a/config/routes/api.rb b/config/routes/api.rb index 20c56138c..4d5547683 100644 --- a/config/routes/api.rb +++ b/config/routes/api.rb @@ -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 diff --git a/db/migrate/20230420092835_create_openkylin_sign_details.rb b/db/migrate/20230420092835_create_openkylin_sign_details.rb new file mode 100644 index 000000000..e282a5f43 --- /dev/null +++ b/db/migrate/20230420092835_create_openkylin_sign_details.rb @@ -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