From bdd3ddaf8682a37180ca122d5628ae4e67a39efe Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Fri, 6 Dec 2019 11:44:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=8C=E6=AD=A5=E4=BB=A3=E7=A0=81=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/hack_user_lastest_codes_controller.rb | 10 ++++++++-- app/controllers/hacks_controller.rb | 9 +++++---- app/views/hack_user_lastest_codes/show.json.jbuilder | 1 + .../hack_user_lastest_codes/sync_code.json.jbuilder | 1 + config/routes.rb | 1 + .../20191206020354_add_modify_time_for_hack_codes.rb | 6 ++++++ 6 files changed, 22 insertions(+), 6 deletions(-) create mode 100644 app/views/hack_user_lastest_codes/sync_code.json.jbuilder create mode 100644 db/migrate/20191206020354_add_modify_time_for_hack_codes.rb diff --git a/app/controllers/hack_user_lastest_codes_controller.rb b/app/controllers/hack_user_lastest_codes_controller.rb index 4cd13d6d9..d27ec5291 100644 --- a/app/controllers/hack_user_lastest_codes_controller.rb +++ b/app/controllers/hack_user_lastest_codes_controller.rb @@ -1,13 +1,14 @@ class HackUserLastestCodesController < ApplicationController before_action :require_login, except: [:listen_result] - before_action :find_my_hack, only: [:show, :code_debug, :code_submit, :update_code, + before_action :find_my_hack, only: [:show, :code_debug, :code_submit, :update_code, :sync_code, :listen_result, :result, :submit_records, :restore_initial_code] before_action :update_user_hack_status, only: [:code_debug, :code_submit] - before_action :require_auth_identity, only: [:update_code, :restore_initial_code] + before_action :require_auth_identity, only: [:update_code, :restore_initial_code, :sync_code] before_action :require_manager_identity, only: [:update_code] def show @my_hack.update_attribute(:submit_status, 0) if @my_hack.submit_status == 1 + @modify = @my_hack.modify_time.to_i < @hack.modify_time.to_i end def update_code @@ -20,6 +21,11 @@ class HackUserLastestCodesController < ApplicationController @my_hack.update_attribute(:code, @hack.code) end + # 同步代码 + def sync_code + @my_hack.update_attributes(code: @hack.code, modify_time: @hack.modify_time) + end + # 调试代码 def code_debug exec_mode = "debug" diff --git a/app/controllers/hacks_controller.rb b/app/controllers/hacks_controller.rb index ca0ebcb3b..ce8582917 100644 --- a/app/controllers/hacks_controller.rb +++ b/app/controllers/hacks_controller.rb @@ -15,7 +15,7 @@ class HacksController < ApplicationController user_hack.identifier else user_identifier = generate_identifier HackUserLastestCode, 12 - user_code = {user_id: current_user.id, code: @hack.code, + user_code = {user_id: current_user.id, code: @hack.code, modify_time: Time.now, identifier: user_identifier, language: @hack.language} @hack.hack_user_lastest_codes.create!(user_code) user_identifier @@ -47,10 +47,8 @@ class HacksController < ApplicationController hack.identifier = generate_identifier Hack, 8 hack.save! # 创建测试集与代码 - logger.info("hack_sets_params:#{hack_sets_params}") - logger.info("hack_code_params:#{hack_code_params}") hack.hack_sets.create!(hack_sets_params) - hack.hack_codes.create!(hack_code_params) + hack.hack_codes.create!(hack_code_params.merge(modify_time: Time.now)) end render_ok({identifier: hack.identifier}) rescue Exception => e @@ -69,6 +67,9 @@ class HacksController < ApplicationController # 新建 @hack.hack_sets.create!(hack_sets_params) # 更新代码 + if params[:hack_codes][:code] != @hack.code + hack_code_params = hack_code_params.merge(modify_time: Time.now) + end @hack.hack_codes.create!(hack_code_params) end render_ok diff --git a/app/views/hack_user_lastest_codes/show.json.jbuilder b/app/views/hack_user_lastest_codes/show.json.jbuilder index 70e1f2093..d6a17cf1f 100644 --- a/app/views/hack_user_lastest_codes/show.json.jbuilder +++ b/app/views/hack_user_lastest_codes/show.json.jbuilder @@ -5,6 +5,7 @@ json.hack do json.code @my_hack.code json.pass_count @hack.pass_num json.submit_count @hack.submit_num + json.modify_code @modify end json.test_case do diff --git a/app/views/hack_user_lastest_codes/sync_code.json.jbuilder b/app/views/hack_user_lastest_codes/sync_code.json.jbuilder new file mode 100644 index 000000000..3b272f298 --- /dev/null +++ b/app/views/hack_user_lastest_codes/sync_code.json.jbuilder @@ -0,0 +1 @@ +json.code @hack.code \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 283ee0d24..061eaf473 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -59,6 +59,7 @@ Rails.application.routes.draw do get :result get :submit_records post :restore_initial_code + post :sync_code end collection do diff --git a/db/migrate/20191206020354_add_modify_time_for_hack_codes.rb b/db/migrate/20191206020354_add_modify_time_for_hack_codes.rb new file mode 100644 index 000000000..7f32204e4 --- /dev/null +++ b/db/migrate/20191206020354_add_modify_time_for_hack_codes.rb @@ -0,0 +1,6 @@ +class AddModifyTimeForHackCodes < ActiveRecord::Migration[5.2] + def change + add_column :hack_codes, :modify_time, :timestamp + add_column :hack_user_lastest_codes, :modify_time, :timestamp + end +end