Merge branch 'dev_aliyun' of https://bdgit.educoder.net/Hjqreturn/educoder into dev_aliyun

dev_forum
jingquan huang 5 years ago
commit b08d6cf54f

@ -0,0 +1,15 @@
class AddDepartmentAppliesController < ApplicationController
before_action :require_login
def create
CreateAddDepartmentApplyService.call(current_user, create_params)
render_ok
rescue CreateAddDepartmentApplyService::Error => ex
render_error(ex.message)
end
private
def create_params
params.permit(:name, :school_id, :remarks)
end
end

@ -0,0 +1,16 @@
class AddSchoolAppliesController < ApplicationController
before_action :require_login
def create
CreateAddSchoolApplyService.call(current_user, create_params)
render_ok
rescue CreateAddSchoolApplyService::Error => ex
render_error(ex.message)
end
private
def create_params
params.permit(:name, :province, :city, :address, :remarks)
end
end

@ -0,0 +1,10 @@
class AddSchoolApplyForm
include ActiveModel::Model
attr_accessor :name, :province, :city, :address, :remarks
validates :name, presence: true
validates :province, presence: true
validates :city, presence: true
validates :address, presence: true
end

@ -10,9 +10,9 @@ class Users::UpdateAccountForm
validates :gender, presence: true, numericality: { only_integer: true }, inclusion: { in: [0, 1] }
validates :location, presence: true
validates :location_city, presence: true
validates :identity, presence: true, numericality: { only_integer: true }, inclusion: { in: [0, 1, 2] }
validates :technical_title, presence: true, unless: -> { identity == 1 }
validates :student_id, presence: true, if: -> { identity == 1 }
validates :identity, presence: true, inclusion: { in: %w[teacher student professional ] }
validates :technical_title, presence: true, unless: -> { identity.to_s == 'student' }
validates :student_id, presence: true, if: -> { identity.to_s == 'student' }
validates :school_id, presence: true
validate :check_school_exist

@ -0,0 +1,5 @@
class AppliedMessage < ApplicationRecord
belongs_to :user
belongs_to :applied, polymorphic: true
end

@ -0,0 +1,15 @@
class ApplyAddDepartment < ApplicationRecord
belongs_to :user
belongs_to :school
belongs_to :department
has_many :applied_messages, as: :applied
has_many :tidings, as: :container, dependent: :destroy
after_create :send_notify
private
def send_notify
tidings.create!(user_id: 1, trigger_user_id: user_id, belong_container: school, tiding_type: 'Apply', status: 0)
end
end

@ -0,0 +1,14 @@
class ApplyAddSchool < ApplicationRecord
belongs_to :school
has_many :applied_messages, as: :applied
has_many :tidings, as: :container, dependent: :destroy
after_create :send_notify
private
def send_notify
tidings.create!(user_id: 1, status: 0, trigger_user_id: user_id, belong_container: school, tiding_type: 'Apply')
end
end

@ -0,0 +1,43 @@
class CreateAddDepartmentApplyService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
name = params[:name].to_s.strip
raise Error, '名称不能为空' if name.blank?
school = School.find_by(id: params[:school_id])
raise Error, '学校/单位不存在' if school.blank?
department = Department.new
department.name = name
department.school = school
ActiveRecord::Base.transaction do
department.save!
attrs = {
user_id: user.id, department: department, school: school,
name: department.name, remarks: params[:remarks], status: 0,
}
apply = ApplyAddDepartment.create!(attrs)
unless user.professional_certification?
user.user_extension.update!(department_id: department.id)
end
# 向管理员发送通知
message = AppliedMessage.new(user_id: 1, status: 0, applied_user_id: user.id, viewed: 0,
applied_id: apply.id, applied_type: 'ApplyAddDepartment', name: department.name)
message.save(validate: false)
end
school
end
end

@ -0,0 +1,37 @@
class CreateAddSchoolApplyService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :params
def initialize(user, params)
@user = user
@params = params
end
def call
AddSchoolApplyForm.new(params).validate!
name = params[:name].to_s.strip
raise Error, '学校/单位已经存在' if name.present? && School.exists?(name: name)
school = School.new
school.name = name
school.province = params[:province].to_s.strip
school.city = params[:city].to_s.strip
school.address = params[:address].to_s.strip
ActiveRecord::Base.transaction do
school.save!
school_attrs = school.as_json(only: %i[name province city address])
ApplyAddSchool.create!(school_attrs.merge(school: school, user_id: user.id, remarks: params[:remarks]))
# 向管理员发送通知
message = AppliedMessage.new(user_id: 1, status: 0, applied_user_id: user.id, viewed: 0,
applied_id: school.id, applied_type: 'ApplyAddSchools', name: school.name)
message.save(validate: false)
end
school
end
end

@ -0,0 +1,8 @@
'zh-CN':
activemodel:
attributes:
add_school_apply_form:
name: 名称
province: 省份
city: 城市
address: 详细地址

@ -596,6 +596,8 @@ Rails.application.routes.draw do
resources :ec_major_schools, only: [:index, :create, :destroy]
end
end
resources :add_school_applies, only: [:create]
resources :add_department_applies, only: [:create]
# 为避免url过长以及层级过深路由定义和controller继承都做了处理
scope module: :ecs do

Loading…
Cancel
Save