diff --git a/app/assets/javascripts/comments.js b/app/assets/javascripts/comments.js new file mode 100644 index 000000000..dee720fac --- /dev/null +++ b/app/assets/javascripts/comments.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/comments.scss b/app/assets/stylesheets/comments.scss new file mode 100644 index 000000000..3722c124e --- /dev/null +++ b/app/assets/stylesheets/comments.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the comments 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/comments_controller.rb b/app/controllers/comments_controller.rb new file mode 100644 index 000000000..851567c92 --- /dev/null +++ b/app/controllers/comments_controller.rb @@ -0,0 +1,59 @@ +class CommentsController < ApplicationController + before_action :find_hack + before_action :require_login + + + # 评论 + def create + begin + @discuss = @hack.discusses.new(comment_params) # 管理员回复的能够显示 + @discuss.hidden = false + @discuss.user_id = current_user.id + @discuss.save! + rescue Exception => e + uid_logger_error("create discuss failed : #{e.message}") + render_error("评论异常") + end + end + + # 回复 + def reply + begin + @discuss = @hack.discusses.new(reply_params) + @discuss.hidden = false + @discuss.user_id = current_user.id + @discuss.root_id = params[:parent_id] + @discuss.save! + rescue Exception => e + uid_logger_error("reply discuss failed : #{e.message}") + render_error("回复评论异常") + end + end + + # 列表 + def index + disscusses = @hack.disscusses.where(:root_id => nil) + @disscuss_count = disscusses.count + @disscusses= paginate disscusses + end + + # 删除 + def destroy + @hack.discusses.find_by(id: params[:id]).destroy + render_ok + end + + + private + def find_hack + @hack = Hack.find_by_identifier params[:identifier] + end + + def comment_params + params.require(:comments).permit(:content) + end + + def reply_params + params.require(:comments).permit(:content, :parent_id) + end +end diff --git a/app/controllers/hack_user_lastest_codes_controller.rb b/app/controllers/hack_user_lastest_codes_controller.rb index bbe94233f..56cda57d7 100644 --- a/app/controllers/hack_user_lastest_codes_controller.rb +++ b/app/controllers/hack_user_lastest_codes_controller.rb @@ -1,12 +1,13 @@ class HackUserLastestCodesController < ApplicationController before_action :require_login, except: [:listen_result] - before_action :find_my_hack, only: [:show, :code_debug, :code_submit, :update_code, :listen_result, :result] + before_action :find_my_hack, only: [:show, :code_debug, :code_submit, :update_code, + :listen_result, :result, :submit_records] before_action :update_user_hack_status, only: [:code_debug, :code_submit] before_action :require_auth_identity, only: [:update_code] before_action :require_manager_identity, only: [:update_code] def show - @my_hack.update_attribute(:status, 0) if @my_hack.status == 1 + @my_hack.update_attribute(:submit_status, 0) if @my_hack.submit_status == 1 end def update_code @@ -33,8 +34,26 @@ class HackUserLastestCodesController < ApplicationController # 提交结果显示 def result - return if @my_hack.status == 1 + if @my_hack.submit_status == 1 + render json: {status:0, message: "正在评测中"} + else + @mode = params[:mode] + @result = + if @mode == "submit" + @my_hack.hack_user_codes.last + elsif @mode == "debug" + @my_hack.hack_user_debug + end + end + end + + # 提交记录 + def submit_records;end + + # 提交记录详情 + def record_detail + @hack_user = HackUserCode.find params[:id] end # 接收中间件返回结果接口 @@ -60,7 +79,7 @@ class HackUserLastestCodesController < ApplicationController if ojEvaResult['execMode'] == "debug" save_debug_data ds_params elsif ojEvaResult['execMode'] == "submit" - save_submit_data ds_params + save_submit_data ds_params.merge(expected_output: testCase['expectedOutput']) end # 评测完成后,还原评测中的状态 @my_hack.update_attribute(:submit_status, 0) @@ -132,7 +151,7 @@ class HackUserLastestCodesController < ApplicationController RewardExperienceService.call(@my_hack.user, reward_attrs) # 评测完成更新通过数 @hack.increment!(:pass_num) - @my_hack.update_attribute(:passed, true) + @my_hack.update_attributes(passed: true, passed_time: Time.now) end end # 创建用户评测记录 diff --git a/app/helpers/comments_helper.rb b/app/helpers/comments_helper.rb new file mode 100644 index 000000000..0ec9ca5f2 --- /dev/null +++ b/app/helpers/comments_helper.rb @@ -0,0 +1,2 @@ +module CommentsHelper +end diff --git a/app/models/hack.rb b/app/models/hack.rb index 1256aa53f..adc6bba8c 100644 --- a/app/models/hack.rb +++ b/app/models/hack.rb @@ -8,6 +8,7 @@ class Hack < ApplicationRecord # 代码 has_many :hack_codes, :dependent => :destroy has_many :hack_user_lastest_codes, :dependent => :destroy + has_many :discusses, as: :dis, dependent: :destroy belongs_to :user scope :published, -> { where(status: 1) } diff --git a/app/models/hack_user_lastest_code.rb b/app/models/hack_user_lastest_code.rb index 0a9e1131c..1d3e20f87 100644 --- a/app/models/hack_user_lastest_code.rb +++ b/app/models/hack_user_lastest_code.rb @@ -2,6 +2,7 @@ class HackUserLastestCode < ApplicationRecord # passed: 用户之前评测是否通过 # status: 最新评测状态: -1测试用例结果不匹配; 0: 评测通过; ;2 评测超时;3 创建pod失败; 4 编译失败;5 执行失败 # submit_status: 0: 可以评测, 1:评测中 + # passed_time:第一次通关的时间 # 编程题最新代码 belongs_to :hack, counter_cache: true belongs_to :user diff --git a/app/views/comments/_discuss.json.jbuilder b/app/views/comments/_discuss.json.jbuilder new file mode 100644 index 000000000..63bebc5a7 --- /dev/null +++ b/app/views/comments/_discuss.json.jbuilder @@ -0,0 +1,12 @@ +json.id discuss.id +json.content content_safe(discuss.content) +json.time time_from_now(discuss.created_at) +json.position discuss.position +json.hack_id discuss.dis_id +# 主贴和回复有一些不同点 +if discuss.parent_id + json.can_delete discuss.can_deleted?(current_user) +else + json.praise_count discuss.praises_count + json.user_praise discuss.praise_treads.select{|pt| pt.user_id == current_user.id}.length > 0 +end \ No newline at end of file diff --git a/app/views/comments/create.json.jbuilder b/app/views/comments/create.json.jbuilder new file mode 100644 index 000000000..629c3b0a0 --- /dev/null +++ b/app/views/comments/create.json.jbuilder @@ -0,0 +1,4 @@ +json.discuss @discuss +json.author do + json.partial! 'users/user', user: @discuss.user +end \ No newline at end of file diff --git a/app/views/comments/index.json.jbuilder b/app/views/comments/index.json.jbuilder new file mode 100644 index 000000000..399b144d9 --- /dev/null +++ b/app/views/comments/index.json.jbuilder @@ -0,0 +1,7 @@ +json.disscuss_count @disscuss_count +json.comments @discusses do |discuss| + json.partial! 'comments/discuss', locals: { discuss: discuss} + json.children discuss.child_discuss(current_user) do |c_d| + json.partial! 'comments/discuss', locals: { discuss: c_d } + end +end diff --git a/app/views/comments/reply.json.jbuilder b/app/views/comments/reply.json.jbuilder new file mode 100644 index 000000000..4024ee7ea --- /dev/null +++ b/app/views/comments/reply.json.jbuilder @@ -0,0 +1 @@ +json.discuss @discuss \ No newline at end of file diff --git a/app/views/hack_user_lastest_codes/record_detail.json.jbuilder b/app/views/hack_user_lastest_codes/record_detail.json.jbuilder new file mode 100644 index 000000000..247dced91 --- /dev/null +++ b/app/views/hack_user_lastest_codes/record_detail.json.jbuilder @@ -0,0 +1,2 @@ +json.(@hack_user, :id, :status, :error_line, :error_msg, :expected_output, + :input, :output, :execute_time, :execute_memory) \ No newline at end of file diff --git a/app/views/hack_user_lastest_codes/result.json.jbuilder b/app/views/hack_user_lastest_codes/result.json.jbuilder new file mode 100644 index 000000000..31164d4f7 --- /dev/null +++ b/app/views/hack_user_lastest_codes/result.json.jbuilder @@ -0,0 +1,7 @@ +json.(@result, :id, :status, :error_line, :error_msg, + :input, :output, :execute_time, :execute_memory) +# 提交模式多了一个预计输出 +if @mode == "submit" + json.expected_output @result.expected_output +end + diff --git a/app/views/hack_user_lastest_codes/submit_records.json.jbuilder b/app/views/hack_user_lastest_codes/submit_records.json.jbuilder new file mode 100644 index 000000000..9aa505160 --- /dev/null +++ b/app/views/hack_user_lastest_codes/submit_records.json.jbuilder @@ -0,0 +1,3 @@ +json.array! @my_hack.hack_user_codes do |hack_user| + json.(hack_user, :id, :created_at, :status, :execute_time, :execute_memory) +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 277139f20..abe31340d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -44,6 +44,7 @@ Rails.application.routes.draw do get :start get :result end + resources :comments end resources :hack_user_lastest_codes, path: :myproblems, param: :identifier do diff --git a/db/migrate/20191120012538_remove_pass_time_for_hack_user_lastest_codes.rb b/db/migrate/20191120012538_remove_pass_time_for_hack_user_lastest_codes.rb new file mode 100644 index 000000000..48063b9a2 --- /dev/null +++ b/db/migrate/20191120012538_remove_pass_time_for_hack_user_lastest_codes.rb @@ -0,0 +1,6 @@ +class RemovePassTimeForHackUserLastestCodes < ActiveRecord::Migration[5.2] + def change + remove_column :hack_user_lastest_codes, :pass_time + add_column :hack_user_codes, :expected_output, :text + end +end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb new file mode 100644 index 000000000..ebb867fa2 --- /dev/null +++ b/spec/controllers/comments_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe CommentsController, type: :controller do + +end diff --git a/spec/helpers/comments_helper_spec.rb b/spec/helpers/comments_helper_spec.rb new file mode 100644 index 000000000..729cd87bf --- /dev/null +++ b/spec/helpers/comments_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the CommentsHelper. For example: +# +# describe CommentsHelper 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 CommentsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end