You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/item_banks_controller.rb

58 lines
1.3 KiB

5 years ago
class ItemBanksController < ApplicationController
5 years ago
include PaginateHelper
5 years ago
before_action :require_login
5 years ago
before_action :find_item, except: [:index, :create]
before_action :edit_auth, only: [:update, :destroy, :set_public]
5 years ago
def index
5 years ago
items = ItemBankQuery.call(params)
@items_count = items.size
@items = paginate items.includes(:item_analysis, :user)
@item_basket_ids = current_user.item_baskets.pluck(:item_bank_id)
5 years ago
end
def create
5 years ago
item = ItemBank.new(user: current_user)
5 years ago
ItemBanks::SaveItemService.call(item, form_params)
render_ok
rescue ApplicationService::Error => ex
render_error(ex.message)
end
def edit
end
def update
ItemBanks::SaveItemService.call(@item, form_params)
render_ok
rescue ApplicationService::Error => ex
render_error(ex.message)
end
def destroy
@item.destroy!
render_ok
end
def set_public
tip_exception(-1, "该试题已公开") if @item.public?
@item.update_attributes!(public: 1)
render_ok
5 years ago
end
5 years ago
5 years ago
private
5 years ago
def find_item
@item = ItemBank.find_by!(id: params[:id])
end
def edit_auth
current_user.admin_or_business? || @item.user == current_user
end
5 years ago
def form_params
5 years ago
params.permit(:discipline_id, :sub_discipline_id, :item_type, :difficulty, :name, :analysis, tag_discipline_id: [], choices: %i[choice_text is_answer])
5 years ago
end
5 years ago
end