From 685e4b09f8449c0afcdda7fa5a44b3cc645dd364 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Thu, 14 Nov 2019 11:14:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AD=A6=E5=91=98=E7=BC=96=E7=A8=8B=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../javascripts/hack_user_lastest_codes.js | 2 ++ .../stylesheets/hack_user_lastest_codes.scss | 3 +++ .../hack_user_lastest_codes_controller.rb | 13 ++++++++++++ app/controllers/hacks_controller.rb | 21 ++++++++++++++++--- app/helpers/hack_user_lastest_codes_helper.rb | 2 ++ app/models/hack.rb | 8 ++++++- app/models/hack_set.rb | 1 + app/models/hack_user_lastest_code.rb | 2 +- app/models/user.rb | 2 ++ .../show.json.jbuilder | 10 +++++++++ config/routes.rb | 9 ++++++-- ...us_end_time_for_hack_user_lastest_code.rb} | 2 ++ ...hack_user_lastest_codes_controller_spec.rb | 5 +++++ .../hack_user_lastest_codes_helper_spec.rb | 15 +++++++++++++ 14 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 app/assets/javascripts/hack_user_lastest_codes.js create mode 100644 app/assets/stylesheets/hack_user_lastest_codes.scss create mode 100644 app/controllers/hack_user_lastest_codes_controller.rb create mode 100644 app/helpers/hack_user_lastest_codes_helper.rb create mode 100644 app/views/hack_user_lastest_codes/show.json.jbuilder rename db/migrate/{20191113061854_add_status_end_time_for_hack_user_lastest_code.rb => 20191113061857_add_status_end_time_for_hack_user_lastest_code.rb} (82%) create mode 100644 spec/controllers/hack_user_lastest_codes_controller_spec.rb create mode 100644 spec/helpers/hack_user_lastest_codes_helper_spec.rb diff --git a/app/assets/javascripts/hack_user_lastest_codes.js b/app/assets/javascripts/hack_user_lastest_codes.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/hack_user_lastest_codes.js @@ -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. diff --git a/app/assets/stylesheets/hack_user_lastest_codes.scss b/app/assets/stylesheets/hack_user_lastest_codes.scss new file mode 100644 index 000000000..37e16a552 --- /dev/null +++ b/app/assets/stylesheets/hack_user_lastest_codes.scss @@ -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/ diff --git a/app/controllers/hack_user_lastest_codes_controller.rb b/app/controllers/hack_user_lastest_codes_controller.rb new file mode 100644 index 000000000..c7d2af92a --- /dev/null +++ b/app/controllers/hack_user_lastest_codes_controller.rb @@ -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 diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index a9a1db44a..4370b5f3b 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -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 diff --git a/app/helpers/hack_user_lastest_codes_helper.rb b/app/helpers/hack_user_lastest_codes_helper.rb new file mode 100644 index 000000000..13350a8f6 --- /dev/null +++ b/app/helpers/hack_user_lastest_codes_helper.rb @@ -0,0 +1,2 @@ +module HackUserLastestCodesHelper +end diff --git a/app/models/hack.rb b/app/models/hack.rb index 89c55a529..e6486401d 100644 --- a/app/models/hack.rb +++ b/app/models/hack.rb @@ -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 diff --git a/app/models/hack_set.rb b/app/models/hack_set.rb index 9be3501bf..d0518a5b4 100644 --- a/app/models/hack_set.rb +++ b/app/models/hack_set.rb @@ -1,3 +1,4 @@ class HackSet < ApplicationRecord # 编程题测试集 + belongs_to :hack end diff --git a/app/models/hack_user_lastest_code.rb b/app/models/hack_user_lastest_code.rb index 4d0fd47e1..f21fa051d 100644 --- a/app/models/hack_user_lastest_code.rb +++ b/app/models/hack_user_lastest_code.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 85b9ef551..3ee74f357 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) } diff --git a/app/views/hack_user_lastest_codes/show.json.jbuilder b/app/views/hack_user_lastest_codes/show.json.jbuilder new file mode 100644 index 000000000..1b6288900 --- /dev/null +++ b/app/views/hack_user_lastest_codes/show.json.jbuilder @@ -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 \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index d9a569c20..1f5ecc772 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 # diff --git a/db/migrate/20191113061854_add_status_end_time_for_hack_user_lastest_code.rb b/db/migrate/20191113061857_add_status_end_time_for_hack_user_lastest_code.rb similarity index 82% rename from db/migrate/20191113061854_add_status_end_time_for_hack_user_lastest_code.rb rename to db/migrate/20191113061857_add_status_end_time_for_hack_user_lastest_code.rb index 65af428b8..4b9379dfa 100644 --- a/db/migrate/20191113061854_add_status_end_time_for_hack_user_lastest_code.rb +++ b/db/migrate/20191113061857_add_status_end_time_for_hack_user_lastest_code.rb @@ -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 diff --git a/spec/controllers/hack_user_lastest_codes_controller_spec.rb b/spec/controllers/hack_user_lastest_codes_controller_spec.rb new file mode 100644 index 000000000..cfa893e89 --- /dev/null +++ b/spec/controllers/hack_user_lastest_codes_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe HackUserLastestCodesController, type: :controller do + +end diff --git a/spec/helpers/hack_user_lastest_codes_helper_spec.rb b/spec/helpers/hack_user_lastest_codes_helper_spec.rb new file mode 100644 index 000000000..94cd6bb00 --- /dev/null +++ b/spec/helpers/hack_user_lastest_codes_helper_spec.rb @@ -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