From 216d1dade61c3b0f325d737d54b25e06188d7c69 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Mon, 10 Feb 2020 12:07:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B4=E6=92=AD=E6=96=B0=E5=A2=9E=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/live_links_controller.rb | 43 +++++++++++++++++------- app/views/live_links/edit.json.jbuilder | 1 + app/views/live_links/index.json.jbuilder | 2 ++ config/routes.rb | 2 +- 4 files changed, 34 insertions(+), 14 deletions(-) create mode 100644 app/views/live_links/edit.json.jbuilder diff --git a/app/controllers/live_links_controller.rb b/app/controllers/live_links_controller.rb index b1ff0c187..ea7155948 100644 --- a/app/controllers/live_links_controller.rb +++ b/app/controllers/live_links_controller.rb @@ -2,31 +2,44 @@ class LiveLinksController < ApplicationController before_action :require_login before_action :find_course, only: [:index, :create] before_action :user_course_identity, :teacher_allowed, only: [:create] + before_action :edit_auth, only: [:edit, :update] def index - lives = @course.live_links.order("id desc") + lives = @course.live_links + order_str = "on_status desc,id desc" @total_count = lives.size + @my_live_id = @course.live_links.find_by(user_id: current_user.id)&.id + order_str = "#{@my_live_id} desc, #{order_str}" if @my_live_id.present? + lives = lives.order("#{order_str}") @lives = paginate lives.includes(user: :user_extension) end def create - @course.live_links.create!( create_params.merge(user_id: current_user.id)) + tip_exception("一个老师只能设置一个直播间") if @course.live_links.where(user_id: current_user.id).exists? + @course.live_links.create!(create_params.merge(user_id: current_user.id)) render_ok end - def update - tip_exception(403, "无权限操作") unless current_user.id == current_live.user_id || current_user.admin? - tip_exception("请勿重复开启") if current_live.on_status && params[:on_status].to_i == 1 - - ActiveRecord::Base.transaction do - current_live.update!(on_status: params[:on_status]) + def edit + @live = current_live + end - # 开启时发送消息,关闭直播时删除对应的消息 - if params[:on_status].to_i == 1 - LivePublishJob.perform_later(current_live.id) - else - current_live.tidings.destroy_all + def update + if params[:on_status] + tip_exception("请勿重复开启") if current_live.on_status && params[:on_status].to_i == 1 + + ActiveRecord::Base.transaction do + current_live.update!(on_status: params[:on_status]) + + # 开启时发送消息,关闭直播时删除对应的消息 + if params[:on_status].to_i == 1 + LivePublishJob.perform_later(current_live.id) + else + current_live.tidings.destroy_all + end end + else + current_live.update!(create_params) end render_ok end @@ -40,4 +53,8 @@ class LiveLinksController < ApplicationController def current_live @_current_live = LiveLink.find params[:id] end + + def edit_auth + tip_exception(403, "无权限操作") unless current_user.id == current_live.user_id || current_user.admin? + end end \ No newline at end of file diff --git a/app/views/live_links/edit.json.jbuilder b/app/views/live_links/edit.json.jbuilder new file mode 100644 index 000000000..047a226e8 --- /dev/null +++ b/app/views/live_links/edit.json.jbuilder @@ -0,0 +1 @@ +json.(@live, :id, :description, :url) diff --git a/app/views/live_links/index.json.jbuilder b/app/views/live_links/index.json.jbuilder index 02eb7be53..e951f24bc 100644 --- a/app/views/live_links/index.json.jbuilder +++ b/app/views/live_links/index.json.jbuilder @@ -5,5 +5,7 @@ json.lives @lives do |live| json.author_login live.user.login json.author_img url_to_avatar(live.user) json.op_auth live.op_auth? + json.created_at live.created_at.strftime('%Y-%m-%d') end +json.my_live_id @my_live_id json.total_count @total_count \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 7dcd3baff..f0c91e8f9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -550,7 +550,7 @@ Rails.application.routes.draw do end end - resources :live_links, only: [:index, :update, :create], shallow: true + resources :live_links, only: [:index, :update, :create, :edit], shallow: true resources :homework_commons, shallow: true do