diff --git a/app/controllers/courses_controller.rb b/app/controllers/courses_controller.rb index cc7cacb44..53a85572d 100644 --- a/app/controllers/courses_controller.rb +++ b/app/controllers/courses_controller.rb @@ -18,7 +18,7 @@ class CoursesController < ApplicationController :left_banner, :top_banner, :apply_to_join_course, :exit_course, :course_groups] before_action :set_course, only: [:show, :update, :destroy, :settings, :set_invite_code_halt, :set_public_or_private, :search_teacher_candidate, :teachers, :apply_teachers, - :top_banner, :left_banner, :add_teacher_popup, :add_teacher, + :top_banner, :left_banner, :add_teacher_popup, :add_teacher, :inform_up, :inform_down, :graduation_group_list, :create_graduation_group, :join_graduation_group, :course_group_list, :set_course_group, :change_course_admin, :change_course_teacher, :delete_course_teacher, :teacher_application_review, :students, :all_course_groups, @@ -41,7 +41,7 @@ class CoursesController < ApplicationController :set_course_group, :create_group_by_importing_file, :update_task_position, :tasks_list] before_action :teacher_or_admin_allowed, only: [:graduation_group_list, :create_graduation_group, :join_graduation_group, - :change_course_teacher, :course_group_list, :change_member_role, + :change_course_teacher, :course_group_list, :change_member_role,:inform_up, :inform_down, :teacher_application_review, :apply_teachers, :delete_course_teacher] before_action :validate_course_name, only: [:create, :update] before_action :find_board, only: :board_list @@ -281,13 +281,43 @@ class CoursesController < ApplicationController end def informs - @informs = @course.informs + @informs = @course.informs.order("position desc") + end + + def inform_up + inform = @course.informs.find_by(id: params[:inform_id]) + next_inform = inform.next_inform + ActiveRecord::Base.transaction do + if next_inform + render_error('已经到达最底部') + else + inform.update_attribute(:position, (position + 1)) + next_inform.update_attribute(:position, last_inform.position - 1) + render_ok + end + end + end + + def inform_down + inform = @course.informs.find_by(id: params[:inform_id]) + last_inform = inform.last_inform + ActiveRecord::Base.transaction do + if last_inform + render_error('已经到达最顶部') + else + inform.update_attribute(:position, (position - 1)) + last_inform.update_attribute(:position, last_inform.position + 1) + render_ok + end + end + end def new_informs inform = Inform.new(container: @course) inform.name = params[:name] inform.description = params[:description] + inform.position = @course.informs.max(:position).to_i + 1 inform.save! normal_status("创建成功") end @@ -300,6 +330,7 @@ class CoursesController < ApplicationController def delete_informs inform = @course.informs.find_by(id: params[:inform_id]) + @course.informs.where("position > ?", inform.position).update_all("position = position - 1") inform.destroy! normal_status("删除成功") end diff --git a/app/models/inform.rb b/app/models/inform.rb index d486b6f11..a6f40fe6d 100644 --- a/app/models/inform.rb +++ b/app/models/inform.rb @@ -5,4 +5,13 @@ class Inform < ApplicationRecord validates :description, length: { maximum: 5000 } has_many :attachments, as: :container, dependent: :destroy + + def next_inform + Inform.where(position: self.position+1, container_id: self.course_id, container_type: 'Course') + end + + def last_inform + Inform.where(position: self.position-1, container_id: self.course_id, container_type: 'Course') + end + end diff --git a/config/routes.rb b/config/routes.rb index b2a7b1de6..e13092c08 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -381,6 +381,8 @@ Rails.application.routes.draw do get 'work_score' get 'act_score' get 'statistics' + post :inform_up + post :inform_down end collection do diff --git a/db/migrate/20191106094429_add_position_for_informs.rb b/db/migrate/20191106094429_add_position_for_informs.rb new file mode 100644 index 000000000..3065081f2 --- /dev/null +++ b/db/migrate/20191106094429_add_position_for_informs.rb @@ -0,0 +1,17 @@ +class AddPositionForInforms < ActiveRecord::Migration[5.2] + def change + add_column :informs, :position, :integer, :default => 1 + + course_ids = Inform.where(container_type: 'Course').pluck(:container_id).uniq + courses = Course.where(id: course_ids) + + courses.find_each do |course| + next if course.informs.count == 1 + informs = course.informs.order("created_at asc") + informs.each_with_index do |inform, index| + inform.update_attribute(:position, index+1) + end + end + + end +end