学员编程首页问题

dev_daiao
daiao 5 years ago
parent 3e68b5a652
commit 685e4b09f8

@ -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 HackUserLastestCodes controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

@ -0,0 +1,13 @@
class HackUserLastestCodesController < ApplicationController
before_action :require_login
before_action :find_user_hack, only: [:show]
def show ;end
private
def find_user_hack
@my_hack = HackUserLastestCode.find_by(identifier: params[:identifier])
@hack = @my_hack.hack
end
end

@ -2,7 +2,22 @@ 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]
before_action :find_hack, only: [:edit, :update, :publish, :start]
# 开启编程,如果第一次开启,创建一条记录,如果已经开启过的话,直接返回标识即可
def start
user_hack = @hack.hack_user_lastest_codes.mine(current_user.id)
identifier =
if user_hack
user_hack.identifier
else
user_identifier = generate_identifier HackUserLastestCode, 12
user_code = {user_id: current_user.id, code: @hack.code, identifier: user_identifier}
@hack.hack_user_lastest_codes.create!(user_code)
user_identifier
end
render_ok({identifier: identifier})
end
# 首页
def index
@ -56,7 +71,7 @@ class HacksController < ApplicationController
# 发布功能
def publish
Hack.where(identifier: params[:identifiers]).update_all(publish_params.to_h.merge(status:1))
@hack.update_attribute(:status, 1)
render_ok
end
@ -87,7 +102,7 @@ class HacksController < ApplicationController
end
def hack_params
params.require(:hack).permit(:name, :description)
params.require(:hack).permit(:name, :description, :difficult, :category, :open_or_not, :time_limit, :score)
end
def hack_sets_params

@ -0,0 +1,2 @@
module HackUserLastestCodesHelper
end

@ -8,7 +8,8 @@ class Hack < ApplicationRecord
# 代码
has_many :hack_codes, :dependent => :destroy
has_many :hack_user_codes, :dependent => :destroy
has_one :hack_user_lastest_code, :dependent => :destroy
has_many :hack_user_lastest_codes, :dependent => :destroy
belongs_to :user
scope :published, -> { where(status: 1) }
scope :opening, -> {where(open_or_not: 1)}
@ -30,4 +31,9 @@ class Hack < ApplicationRecord
end
end
# 用于用户调试的第一个测试用例
def input_test_case
hack_sets.first&.input
end
end

@ -1,3 +1,4 @@
class HackSet < ApplicationRecord
# 编程题测试集
belongs_to :hack
end

@ -2,7 +2,7 @@ class HackUserLastestCode < ApplicationRecord
# 编程题最新代码
belongs_to :hack, counter_cache: true
scope :mine, ->(author_id){ where(user_id: author_id)}
scope :mine, ->(author_id){ find_by(user_id: author_id)}
scope :passed, -> {where(status: 1)}
end

@ -147,6 +147,8 @@ class User < ApplicationRecord
# 客户管理
belongs_to :partner, optional: true
# OJ编程题
has_many :hacks, dependent: :destroy
# Groups and active users
scope :active, lambda { where(status: STATUS_ACTIVE) }

@ -0,0 +1,10 @@
json.hack do
json.(@hack, :difficult, :time_limit, :description, :score)
json.language @hack.language
json.username @hack.user.real_name
json.code @my_hack.code
end
json.test_case do
json.input @hack.input_test_case
end

@ -33,15 +33,20 @@ Rails.application.routes.draw do
end
end
resources :hacks, param: :identifier do
resources :hacks, path: :problems, param: :identifier do
collection do
post :publish
get :unpulished_list
end
member do
post :publish
end
end
resources :hack_user_lastest_codes, path: :myproblems, param: :identifier do
end
resources :tem_tests
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
#

@ -3,9 +3,11 @@ class AddStatusEndTimeForHackUserLastestCode < ActiveRecord::Migration[5.2]
add_column :hack_user_lastest_codes, :passed, :boolean, :default => false
add_column :hack_user_lastest_codes, :pass_time, :timestamp
add_column :hack_user_lastest_codes, :status, :integer, :default => 0
add_column :hack_user_lastest_codes, :identifier, :string
add_column :hacks, :hack_user_lastest_codes_count, :integer, :default => 0
add_column :hacks, :pass_num, :integer, :default => 0
add_index :hack_user_lastest_codes, :user_id, name: "user_index"
add_index :hack_user_lastest_codes, :hack_id, name: "hack_index"
add_index :hack_user_lastest_codes, :identifier, unique: true
end
end

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe HackUserLastestCodesController, type: :controller do
end

@ -0,0 +1,15 @@
require 'rails_helper'
# Specs in this file have access to a helper object that includes
# the HackUserLastestCodesHelper. For example:
#
# describe HackUserLastestCodesHelper 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 HackUserLastestCodesHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading…
Cancel
Save