parent
0ad1b56a27
commit
310a628363
@ -0,0 +1,2 @@
|
||||
// Place all the behaviors and hooks related to the matching controller here.
|
||||
// All this logic will automatically be available in application.js.
|
@ -0,0 +1,3 @@
|
||||
// Place all the styles related to the hacks controller here.
|
||||
// They will automatically be included in application.css.
|
||||
// You can use Sass (SCSS) here: http://sass-lang.com/
|
@ -0,0 +1,107 @@
|
||||
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
|
@ -0,0 +1,2 @@
|
||||
module HacksHelper
|
||||
end
|
@ -0,0 +1,26 @@
|
||||
class Hack < ApplicationRecord
|
||||
# 编程题
|
||||
validates_length_of :name, maximum: 60
|
||||
# 测试集
|
||||
has_many :hack_sets, ->{order("position asc")}, :dependent => :destroy
|
||||
|
||||
# 代码
|
||||
has_many :hack_codes, :dependent => :destroy
|
||||
|
||||
def language
|
||||
if hack_codes.count == 1
|
||||
hack_codes.first.language
|
||||
else
|
||||
hack_codes.pluck(:language)
|
||||
end
|
||||
end
|
||||
|
||||
def code
|
||||
if hack_codes.count == 1
|
||||
hack_codes.first.code
|
||||
else
|
||||
hack_codes.pluck(:code)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
class HackCode < ApplicationRecord
|
||||
# 编程题代码相关
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
class HackSet < ApplicationRecord
|
||||
# 编程题测试集
|
||||
end
|
@ -0,0 +1,3 @@
|
||||
class HackUserCode < ApplicationRecord
|
||||
# 用户编程题的信息
|
||||
end
|
@ -0,0 +1,2 @@
|
||||
class HackUserDebug < ApplicationRecord
|
||||
end
|
@ -0,0 +1,4 @@
|
||||
class HackUserLastestCode < ApplicationRecord
|
||||
# 编程题最新代码
|
||||
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
# 编程内容
|
||||
json.(@hack, :name, :description, :language, :code)
|
||||
|
||||
# 代码
|
||||
json.language @hack.language
|
||||
json.code @hack.code
|
||||
|
||||
# 测试集
|
||||
json.hack_sets do
|
||||
json.array! @hack.hack_sets do |set|
|
||||
json.(set, :id, :input, :output, :position)
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
json.hacks_count @hacks_count
|
||||
json.hacks @hacks do |hack|
|
||||
json.(hack, :name, :difficult, :updated_at)
|
||||
json.sets_count hack.hack_sets.count
|
||||
end
|
@ -0,0 +1,20 @@
|
||||
class CreateHacks < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hacks do |t|
|
||||
t.string :name
|
||||
t.integer :difficult
|
||||
t.integer :category
|
||||
t.integer :status, default: 0
|
||||
t.boolean :open_or_not, default: true
|
||||
t.integer :time_limit
|
||||
t.integer :memory_limit
|
||||
t.string :identifier
|
||||
t.references :user
|
||||
t.text :description
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :hacks, :identifier, unique: true
|
||||
end
|
||||
end
|
@ -0,0 +1,11 @@
|
||||
class CreateHackCodes < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hack_codes do |t|
|
||||
t.references :hack
|
||||
t.text :code
|
||||
t.string :language
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
class CreateHackSets < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hack_sets do |t|
|
||||
t.references :hack
|
||||
t.text :input
|
||||
t.text :output
|
||||
t.integer :position
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
add_index :hack_sets, [:hack_id, :position], unique: true
|
||||
end
|
||||
end
|
@ -0,0 +1,18 @@
|
||||
class CreateHackUserCodes < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hack_user_codes do |t|
|
||||
t.references :user
|
||||
t.references :hack
|
||||
t.text :code
|
||||
t.text :output
|
||||
t.text :error_msg
|
||||
t.integer :error_line
|
||||
t.integer :position
|
||||
t.integer :status, default: 0
|
||||
t.integer :query_index
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :hack_user_codes, [:user_id, :hack_id, :query_index], unique: true
|
||||
end
|
||||
end
|
@ -0,0 +1,13 @@
|
||||
class CreateHackUserLastestCodes < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hack_user_lastest_codes do |t|
|
||||
t.references :user
|
||||
t.references :hack
|
||||
t.text :code
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
|
||||
add_index :hack_user_lastest_codes, [:user_id, :hack_id], unique: true
|
||||
end
|
||||
end
|
@ -0,0 +1,14 @@
|
||||
class CreateHackUserDebugs < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
create_table :hack_user_debugs do |t|
|
||||
t.references :user
|
||||
t.references :hack
|
||||
t.text :output
|
||||
t.text :input
|
||||
t.text :error_msg
|
||||
t.integer :error_line
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
@ -0,0 +1,7 @@
|
||||
class AddDefualtForHacks < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
change_column :hacks, :status, :integer, :default => 0
|
||||
change_column :hacks, :open_or_not, :boolean, :default => true
|
||||
change_column :hack_user_codes, :status, :integer, :default => 0
|
||||
end
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
class AddScoreForHacks < ActiveRecord::Migration[5.2]
|
||||
def change
|
||||
add_column :hacks, :score, :integer
|
||||
end
|
||||
end
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HacksController, type: :controller do
|
||||
|
||||
end
|
@ -0,0 +1,15 @@
|
||||
require 'rails_helper'
|
||||
|
||||
# Specs in this file have access to a helper object that includes
|
||||
# the HacksHelper. For example:
|
||||
#
|
||||
# describe HacksHelper do
|
||||
# describe "string concat" do
|
||||
# it "concats two strings with spaces" do
|
||||
# expect(helper.concat_strings("this","that")).to eq("this that")
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
RSpec.describe HacksHelper, type: :helper do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HackCode, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HackSet, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe Hack, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HackUserCode, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HackUserDebug, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
@ -0,0 +1,5 @@
|
||||
require 'rails_helper'
|
||||
|
||||
RSpec.describe HackUserLastestCode, type: :model do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
Loading…
Reference in new issue