diff --git a/app/controllers/libraries_controller.rb b/app/controllers/libraries_controller.rb index 55894aaa6..c7c6029da 100644 --- a/app/controllers/libraries_controller.rb +++ b/app/controllers/libraries_controller.rb @@ -22,7 +22,7 @@ class LibrariesController < ApplicationController end @count = libraries.count - @libraries = paginate libraries.includes(:library_tags, :praise_tread_cache, user: :user_extension) + @libraries = paginate libraries.includes(:library_tags, user: :user_extension) ids = @libraries.map(&:id) @download_count_map = Attachment.where(container_type: 'Library', container_id: ids) diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index d950fe1c8..0475eb0da 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -85,7 +85,7 @@ class MemosController < ApplicationController params[:tags].each do |tag| MemoTagRepertoire.create!(memo_id: @memo.id, tag_repertoire_id: tag) end - normal_status("帖子创建成功") + render :json => {memo_id: @memo.id, status: 0, message: "帖子创建成功"} rescue Exception => e tip_exception("帖子创建失败,原因:#{e}") raise ActiveRecord::Rollback diff --git a/app/models/library.rb b/app/models/library.rb index 894dcdac0..13a5c3243 100644 --- a/app/models/library.rb +++ b/app/models/library.rb @@ -10,6 +10,8 @@ class Library < ApplicationRecord has_many :attachments, as: :container has_one :praise_tread_cache, foreign_key: :object_id + has_many :praise_treads, as: :praise_tread_object, dependent: :destroy + validates :uuid, presence: true, uniqueness: true diff --git a/app/views/games/picture_display.json.jbuilder b/app/views/games/picture_display.json.jbuilder index 8b5271ec5..2bc88fdf4 100644 --- a/app/views/games/picture_display.json.jbuilder +++ b/app/views/games/picture_display.json.jbuilder @@ -26,13 +26,13 @@ elsif @type =="qrcode" json.qrcode_str @qrcode_str elsif @type == "mp3" || @type == "mp4" if @type == "mp4" - json.orignal_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378171"}] - json.user_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378172"}] - json.answer_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378173"}] + json.orignal_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378171/123.mp4"}] + json.user_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378172/456.mp4"}] + json.answer_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378173/789.mp4"}] else - json.orignal_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378174"}] - json.user_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378175"}] - json.answer_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378175"}] + json.orignal_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378174/58099.mp3"}] + json.user_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378175/654058514.mp3"}] + json.answer_file [{"file_url": "http://120.27.231.56:48080/attachments/download/378175/654058514.mp3"}] end # json.orignal_file do # json.array! @orignal_picture do |file| diff --git a/app/views/libraries/index.json.jbuilder b/app/views/libraries/index.json.jbuilder index c58d390b0..13ab8d77b 100644 --- a/app/views/libraries/index.json.jbuilder +++ b/app/views/libraries/index.json.jbuilder @@ -5,7 +5,7 @@ json.libraries do json.cover_url library.cover_id.present? ? download_url(library.cover) : nil - json.praise_count library.praise_tread_cache&.praise_num || 0 + json.praise_count library.praises_count json.download_count @download_count_map.fetch(library.id, 0) json.published_at library.display_published_at diff --git a/app/views/libraries/show.json.jbuilder b/app/views/libraries/show.json.jbuilder index 0f4b6ea17..e1fc7d781 100644 --- a/app/views/libraries/show.json.jbuilder +++ b/app/views/libraries/show.json.jbuilder @@ -2,7 +2,7 @@ library = current_library json.extract! library, :id, :uuid, :title, :content, :author_name, :author_school_name, :status, :visited_count -json.praise_count library.praise_tread_cache&.praise_num || 0 +json.praise_count library.praises_count json.published_at library.display_published_at json.created_at library.display_created_at diff --git a/app/views/memos/_memo.json.jbuilder b/app/views/memos/_memo.json.jbuilder index bc3744d1f..a33de2492 100644 --- a/app/views/memos/_memo.json.jbuilder +++ b/app/views/memos/_memo.json.jbuilder @@ -9,6 +9,6 @@ json.memo do json.tag memo.tag_repertoires.map(&:name) json.time memo.created_at json.replies_count memo.all_replies_count - json.user_praise memo.praise_treads.user_liker(@user.try(:id)) ? true : false + json.user_praise memo.praise_treads.user_liker(@user.try(:id)).count > 1 ? true : false json.memo_praise_count memo.praise_treads.liker.count end diff --git a/config/routes.rb b/config/routes.rb index 79542817f..b8db3afa6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,6 +5,7 @@ Rails.application.routes.draw do mount Sidekiq::Web => '/sidekiq', :constraints => AdminConstraint.new get 'attachments/download/:id', to: 'attachments#show' + get 'attachments/download/:id/:filename', to: 'attachments#show' resources :edu_settings scope '/api' do diff --git a/db/migrate/20190801075337_add_praises_count_to_memos.rb b/db/migrate/20190801075337_add_praises_count_to_memos.rb new file mode 100644 index 000000000..0cc2259e2 --- /dev/null +++ b/db/migrate/20190801075337_add_praises_count_to_memos.rb @@ -0,0 +1,12 @@ +class AddPraisesCountToMemos < ActiveRecord::Migration[5.2] + def change + add_column :memos, :praises_count, :integer, :default => 0 + + memos = Memo.includes(:praise_treads).all + memos.find_each do |m| + puts("####{m.id}") + praises_count = m.praise_treads.select{|pt| pt.praise_or_tread == 1}.count + m.update_column(:praises_count, praises_count) + end + end +end diff --git a/db/migrate/20190801084533_add_praises_count_to_libraries.rb b/db/migrate/20190801084533_add_praises_count_to_libraries.rb new file mode 100644 index 000000000..6a619104d --- /dev/null +++ b/db/migrate/20190801084533_add_praises_count_to_libraries.rb @@ -0,0 +1,10 @@ +class AddPraisesCountToLibraries < ActiveRecord::Migration[5.2] + def change + add_column :libraries, :praises_count, :integer, :default => 0 + + Library.find_each do |library| + praises_count = library.praise_treads.count + library.update_column(:praises_count, praises_count) + end + end +end