From 42357d1f74976901921125d7f2bd2d94bfec9a65 Mon Sep 17 00:00:00 2001 From: daiao <358551898@qq.com> Date: Mon, 23 Dec 2019 20:28:11 +0800 Subject: [PATCH] =?UTF-8?q?=E7=82=B9=E8=B5=9E=E5=92=8C=E9=9A=90=E8=97=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/comments_controller.rb | 10 ++++++++++ app/controllers/discusses_controller.rb | 6 +++--- app/models/hack.rb | 3 +++ app/views/comments/plus.json.jbuilder | 1 + config/routes.rb | 1 + .../20191223120424_add_praises_count_for_hacks.rb | 5 +++++ 6 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 app/views/comments/plus.json.jbuilder create mode 100644 db/migrate/20191223120424_add_praises_count_for_hacks.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 8374e4a91..72afb3342 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -43,6 +43,16 @@ class CommentsController < ApplicationController render_ok end + # 隐藏、取消隐藏 + def hidden + if @hack.manager?(current_user) + @discuss.update_attribute(:hidden, params[:hidden] == "1") + sucess_status + else + Educoder::TipException(403, "..") + end + end + private def find_hack diff --git a/app/controllers/discusses_controller.rb b/app/controllers/discusses_controller.rb index cbb19cb7f..6b21526dd 100644 --- a/app/controllers/discusses_controller.rb +++ b/app/controllers/discusses_controller.rb @@ -117,13 +117,13 @@ class DiscussesController < ApplicationController # 0 取消赞; def plus pt = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], - :user_id => current_user, :praise_or_tread => 1).first + :user_id => current_user, :praise_or_tread => 1) # 如果当前用户已赞过,则不能重复赞 if params[:type] == 1 && pt.blank? PraiseTread.create!(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], - :user_id => current_user.id, :praise_or_tread => 1) if pt.blank? + :user_id => current_user.id, :praise_or_tread => 1) else - pt.destroy if pt.present? # 如果已赞过,则删掉这条赞(取消);如果没赞过,则为非法请求不处理 + pt.destroy_all if pt.present? # 如果已赞过,则删掉这条赞(取消);如果没赞过,则为非法请求不处理 end @praise_count = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], diff --git a/app/models/hack.rb b/app/models/hack.rb index 506cd4942..2e2ffb9e0 100644 --- a/app/models/hack.rb +++ b/app/models/hack.rb @@ -11,6 +11,9 @@ class Hack < ApplicationRecord has_many :hack_codes, :dependent => :destroy has_many :hack_user_lastest_codes, :dependent => :destroy has_many :discusses, as: :dis, dependent: :destroy + # 点赞 + has_many :praise_treads, as: :praise_tread_object, dependent: :destroy + belongs_to :user scope :published, -> { where(status: 1) } diff --git a/app/views/comments/plus.json.jbuilder b/app/views/comments/plus.json.jbuilder new file mode 100644 index 000000000..d24dbb655 --- /dev/null +++ b/app/views/comments/plus.json.jbuilder @@ -0,0 +1 @@ +json.praise_count @praise_count \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 10ae90fde..ace8324ce 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,6 +71,7 @@ Rails.application.routes.draw do resources :comments do collection do post :reply + post :hidden end end end diff --git a/db/migrate/20191223120424_add_praises_count_for_hacks.rb b/db/migrate/20191223120424_add_praises_count_for_hacks.rb new file mode 100644 index 000000000..467c6f602 --- /dev/null +++ b/db/migrate/20191223120424_add_praises_count_for_hacks.rb @@ -0,0 +1,5 @@ +class AddPraisesCountForHacks < ActiveRecord::Migration[5.2] + def change + add_column :hacks, :praises_count, :integer, :default => 0 + end +end