From 02b6646bd2bf39e76283de3fd9cf1e0b2ae89879 Mon Sep 17 00:00:00 2001 From: cxt <853663049@qq.com> Date: Fri, 20 Dec 2019 10:33:57 +0800 Subject: [PATCH] =?UTF-8?q?=E9=A2=98=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/item_banks_controller.rb | 17 +++++++--- app/forms/item_banks/save_item_form.rb | 13 ++++++++ app/models/item_bank.rb | 15 ++++++++- app/models/item_bank_tag_repertoire.rb | 4 +++ app/queries/item_bank_query.rb | 33 +++++++++++++++++++ app/services/item_banks/save_item_service.rb | 13 ++++++++ app/views/item_banks/index.json.jbuilder | 14 ++++++++ ...20191219092851_migrate_item_bank_column.rb | 7 ++++ ...095652_create_item_bank_tag_repertoires.rb | 10 ++++++ ...9101909_change_item_bank_public_default.rb | 7 ++++ spec/models/item_bank_tag_repertoire_spec.rb | 5 +++ 11 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 app/forms/item_banks/save_item_form.rb create mode 100644 app/models/item_bank_tag_repertoire.rb create mode 100644 app/queries/item_bank_query.rb create mode 100644 app/services/item_banks/save_item_service.rb create mode 100644 app/views/item_banks/index.json.jbuilder create mode 100644 db/migrate/20191219092851_migrate_item_bank_column.rb create mode 100644 db/migrate/20191219095652_create_item_bank_tag_repertoires.rb create mode 100644 db/migrate/20191219101909_change_item_bank_public_default.rb create mode 100644 spec/models/item_bank_tag_repertoire_spec.rb diff --git a/app/controllers/item_banks_controller.rb b/app/controllers/item_banks_controller.rb index 104851676..3ed8c76b8 100644 --- a/app/controllers/item_banks_controller.rb +++ b/app/controllers/item_banks_controller.rb @@ -1,14 +1,21 @@ -class LibrariesController < ApplicationController +class ItemBanksController < ApplicationController include PaginateHelper + before_action :require_login def index - default_sort('updated_at', 'desc') - - @items = ItemBankQuery.call(params) - @items = paginate courses.includes(:school, :students, :attachments, :homework_commons, teacher: :user_extension) + items = ItemBankQuery.call(params) + @items_count = items.size + @items = paginate items.includes(:item_analysis, :user) end def create + item = ItemBank.new(user: current_user) + ItemBank::SaveItemService.call(item, form_params) + end + private + def form_params + params.permit(:repertoire_id, :sub_repertoire_id, :item_type, :difficulty, :name, tag_repertoire_id: [], choices: []) end + end \ No newline at end of file diff --git a/app/forms/item_banks/save_item_form.rb b/app/forms/item_banks/save_item_form.rb new file mode 100644 index 000000000..6e59aa384 --- /dev/null +++ b/app/forms/item_banks/save_item_form.rb @@ -0,0 +1,13 @@ +class ItemBank::SaveItemForm + include ActiveModel::Model + + attr_accessor :repertoire_id, :sub_repertoire_id, :item_type, :difficulty, :name, :tag_repertoire_id, :choices + + validates :repertoire_id, presence: true + validates :sub_repertoire_id, presence: true + validates :item_type, presence: true + validates :difficulty, presence: true + validates :name, presence: true + + +end \ No newline at end of file diff --git a/app/models/item_bank.rb b/app/models/item_bank.rb index 8078a55e0..a7cf6c579 100644 --- a/app/models/item_bank.rb +++ b/app/models/item_bank.rb @@ -1,11 +1,24 @@ class ItemBank < ApplicationRecord # difficulty: 1 简单 2 适中 3 困难 # item_type: 0 单选 1 多选 2 判断 3 填空 4 简答 5 实训 6 编程 - enum item_type: { SINGLE: 0, MULTIPLE: 1, JUDGMENT: 2, COMPLETION: 3, SUBJECTIVE: 4, PRACTICAL: 5, PROGRAM: 6 } + # 试卷的问题类型 + SINGLE = 0 #单选题 + MULTIPLE = 1 #多选题 + JUDGMENT = 2 #判断题 + COMPLETION = 3 # 填空题 + SUBJECTIVE = 4 # 主观题 + PRACTICAL = 5 #实训题 + PROGRAM = 6 #编程题 belongs_to :user + belongs_to :sub_repertoire has_one :item_analysis, dependent: :destroy has_many :item_choices, dependent: :destroy has_many :item_baskets, dependent: :destroy + has_many :item_bank_tag_repertoires, dependent: :destroy + + def analysisi + item_analysis&.analysis + end end diff --git a/app/models/item_bank_tag_repertoire.rb b/app/models/item_bank_tag_repertoire.rb new file mode 100644 index 000000000..271cad6b0 --- /dev/null +++ b/app/models/item_bank_tag_repertoire.rb @@ -0,0 +1,4 @@ +class ItemBankTagRepertoire < ApplicationRecord + belongs_to :item_bank + belongs_to :tag_repertoire +end diff --git a/app/queries/item_bank_query.rb b/app/queries/item_bank_query.rb new file mode 100644 index 000000000..34db153f0 --- /dev/null +++ b/app/queries/item_bank_query.rb @@ -0,0 +1,33 @@ +class ItemBankQuery < ApplicationQuery + include CustomSortable + attr_reader :params + + sort_columns :updated_at, default_by: :updated_at, default_direction: :desc, default_table: 'item_banks' + + def initialize(params) + @params = params + end + + def call + if params[:public].to_i == 1 + items = ItemBank.where(public: 1) + elsif params[:public].to_i == 0 + items = ItemBank.where(user_id: User.current.id) + end + + if params[:tag_repertoire_id].present? + items = items.joins(:item_bank_tag_repertoires).where(item_bank_tag_repertoires: {tag_repertoire_id: params[:tag_repertoire_id]}) + elsif params[:sub_repertoire_id].present? + items = items.where(sub_repertoire_id: params[:sub_repertoire_id]) + elsif params[:repertoire_id].present? + items = items.joins(:sub_repertoire).where(sub_repertoires: {repertoire_id: params[:repertoire_id]}) + end + + items = items.where(item_type: params[:item_type].to_i) if params[:item_type].present? + items = items.where(difficulty: params[:difficulty].to_i) if params[:difficulty].present? + + items = items.where("name like ?", "%#{params[:keyword].strip}%") if params[:keyword].present? + + custom_sort(items, params[:sort_by], params[:sort_direction]) + end +end \ No newline at end of file diff --git a/app/services/item_banks/save_item_service.rb b/app/services/item_banks/save_item_service.rb new file mode 100644 index 000000000..ec9930aaa --- /dev/null +++ b/app/services/item_banks/save_item_service.rb @@ -0,0 +1,13 @@ +class ItemBank::SaveItemService < ApplicationService + attr_reader :item, :params + + def initialize(item, params) + @item = item + @params = params + end + + def call + Competitions::SaveTeamForm.new(params).validate! + + end +end \ No newline at end of file diff --git a/app/views/item_banks/index.json.jbuilder b/app/views/item_banks/index.json.jbuilder new file mode 100644 index 000000000..991cf9592 --- /dev/null +++ b/app/views/item_banks/index.json.jbuilder @@ -0,0 +1,14 @@ +json.items @items.each do |item| + json.(item, :id, :name, :item_type, :difficulty, :public, :quotes) + json.analysisi item.analysisi + json.update_time item.updated_at&.strftime("%Y-%m-%d %H:%M") + json.author do + json.login item.user&.login + json.name item.user&.full_name + end + json.choices item.item_choices do |choice| + json.choice_text choice.choice_text + json.is_answer choice.is_answer + end +end +json.items_count @items_count \ No newline at end of file diff --git a/db/migrate/20191219092851_migrate_item_bank_column.rb b/db/migrate/20191219092851_migrate_item_bank_column.rb new file mode 100644 index 000000000..ec880ad03 --- /dev/null +++ b/db/migrate/20191219092851_migrate_item_bank_column.rb @@ -0,0 +1,7 @@ +class MigrateItemBankColumn < ActiveRecord::Migration[5.2] + def change + remove_column :item_banks, :curriculum_id + remove_column :item_banks, :curriculum_direction_id + add_column :item_banks, :sub_repertoire_id, :integer, index: true + end +end diff --git a/db/migrate/20191219095652_create_item_bank_tag_repertoires.rb b/db/migrate/20191219095652_create_item_bank_tag_repertoires.rb new file mode 100644 index 000000000..c6ce93bc6 --- /dev/null +++ b/db/migrate/20191219095652_create_item_bank_tag_repertoires.rb @@ -0,0 +1,10 @@ +class CreateItemBankTagRepertoires < ActiveRecord::Migration[5.2] + def change + create_table :item_bank_tag_repertoires do |t| + t.references :item_bank, index: true + t.references :tag_repertoire, index: true + + t.timestamps + end + end +end diff --git a/db/migrate/20191219101909_change_item_bank_public_default.rb b/db/migrate/20191219101909_change_item_bank_public_default.rb new file mode 100644 index 000000000..4ad470258 --- /dev/null +++ b/db/migrate/20191219101909_change_item_bank_public_default.rb @@ -0,0 +1,7 @@ +class ChangeItemBankPublicDefault < ActiveRecord::Migration[5.2] + def change + change_column_default :item_banks, :public, 0 + change_column_default :item_banks, :quotes, 0 + change_column_default :item_banks, :difficulty, 1 + end +end diff --git a/spec/models/item_bank_tag_repertoire_spec.rb b/spec/models/item_bank_tag_repertoire_spec.rb new file mode 100644 index 000000000..dce195849 --- /dev/null +++ b/spec/models/item_bank_tag_repertoire_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe ItemBankTagRepertoire, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end