You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.9 KiB
108 lines
2.9 KiB
class HacksController < ApplicationController
|
|
before_action :require_login, except: [:index]
|
|
before_action :require_teacher_identity, only: [:create, :edit, :update]
|
|
before_action :require_auth_identity, only: [:update, :edit, :publish]
|
|
before_action :find_hack, only: [:edit, :update]
|
|
|
|
|
|
def index
|
|
render_ok
|
|
end
|
|
|
|
def create
|
|
begin
|
|
hack = Hack.new(hack_params)
|
|
ActiveRecord::Base.transaction do
|
|
hack.user_id = current_user.id
|
|
hack.identifier = generate_identifier Hack, 8
|
|
hack.save!
|
|
# 创建测试集与代码
|
|
hack.hack_sets.create!(hack_sets_params)
|
|
hack.hack_codes.create!(hack_code_params)
|
|
end
|
|
render_ok({identifier: hack.identifier})
|
|
rescue Exception => e
|
|
logger.error("########create_hack_error: #{e.message}")
|
|
render_error("创建失败")
|
|
end
|
|
end
|
|
|
|
def update
|
|
begin
|
|
ActiveRecord::Base.transaction do
|
|
@hack.update_attributes!(hack_params)
|
|
set_ids = @hack.hack_sets.pluck(:id)
|
|
# 更新
|
|
param_update_sets params[:update_hack_sets], set_ids
|
|
# 新建
|
|
@hack.hack_sets.create!(hack_sets_params)
|
|
end
|
|
render_ok
|
|
rescue Exception => e
|
|
logger.error("####update_hack_error: #{e.message}")
|
|
render_error("更新失败")
|
|
end
|
|
end
|
|
|
|
# 发布功能
|
|
def publish
|
|
Hack.where(identifier: params[:identifiers]).update_all(publish_params.to_h.merge(status:1))
|
|
render_ok
|
|
end
|
|
|
|
# 发布列表
|
|
def unpulished_list
|
|
limit = params[:limit] || 16
|
|
page = params[:page] || 1
|
|
hacks = Hack.where(user_id: current_user.id, status: 0)
|
|
@hacks_count = hacks.count
|
|
@hacks = hacks.includes(:hack_sets).page(page).per(limit)
|
|
end
|
|
|
|
def edit;end
|
|
|
|
private
|
|
# 实名认证老师,管理员与运营人员权限
|
|
def require_teacher_identity
|
|
current_user.certification_teacher? || admin_or_business?
|
|
end
|
|
|
|
# 只有自己,或者管理员才能更新
|
|
def require_auth_identity
|
|
@hack.user_id == current_user.id || admin_or_business?
|
|
end
|
|
|
|
def find_hack
|
|
@hack = Hack.find_by_identifier(params[:identifier])
|
|
end
|
|
|
|
def hack_params
|
|
params.require(:hack).permit(:name, :description)
|
|
end
|
|
|
|
def hack_sets_params
|
|
params.permit(hack_sets: [:input, :output, :position])[:hack_sets]
|
|
end
|
|
|
|
def hack_code_params
|
|
params.require(:hack_codes).permit(:code, :language)
|
|
end
|
|
|
|
def publish_params
|
|
params.require(:hack).permit(:difficult, :category, :open_or_not, :time_limit, :score)
|
|
end
|
|
|
|
|
|
def param_update_sets sets, all_sets_id
|
|
delete_set_ids = all_sets_id - sets.map{|set|set[:id]}
|
|
@hack.hack_sets.where(id: delete_set_ids).destroy_all
|
|
sets.each do |set|
|
|
if all_sets_id.include?(set[:id])
|
|
update_attrs = {input: set[:input], output: set[:output], position: set[:position]}
|
|
@hack.hack_sets.find_by!(id: set[:id]).update_attributes(update_attrs)
|
|
end
|
|
end
|
|
end
|
|
|
|
end
|