add skill filtering and batch_destroy for collection artifacts
This commit is contained in:
parent
9ce86b22b4
commit
ab19403904
2 changed files with 35 additions and 1 deletions
|
|
@ -9,7 +9,7 @@ module Api
|
|||
before_action :set_collection_artifact_for_read, only: %i[show]
|
||||
|
||||
# Write actions: require auth, use current_user
|
||||
before_action :restrict_access, only: %i[create update destroy batch import]
|
||||
before_action :restrict_access, only: %i[create update destroy batch batch_destroy import]
|
||||
before_action :set_collection_artifact_for_write, only: %i[update destroy]
|
||||
|
||||
def index
|
||||
|
|
@ -19,6 +19,12 @@ module Api
|
|||
@collection_artifacts = @collection_artifacts.where(element: params[:element]) if params[:element]
|
||||
@collection_artifacts = @collection_artifacts.joins(:artifact).where(artifacts: { rarity: params[:rarity] }) if params[:rarity]
|
||||
|
||||
# Skill filters - each slot uses OR logic, slots combined with AND logic
|
||||
@collection_artifacts = @collection_artifacts.with_skill_in_slot(1, params[:skill1]) if params[:skill1].present?
|
||||
@collection_artifacts = @collection_artifacts.with_skill_in_slot(2, params[:skill2]) if params[:skill2].present?
|
||||
@collection_artifacts = @collection_artifacts.with_skill_in_slot(3, params[:skill3]) if params[:skill3].present?
|
||||
@collection_artifacts = @collection_artifacts.with_skill_in_slot(4, params[:skill4]) if params[:skill4].present?
|
||||
|
||||
@collection_artifacts = @collection_artifacts.paginate(page: params[:page], per_page: params[:limit] || 50)
|
||||
|
||||
render json: Api::V1::CollectionArtifactBlueprint.render(
|
||||
|
|
@ -127,6 +133,17 @@ module Api
|
|||
}, status: status
|
||||
end
|
||||
|
||||
# DELETE /collection/artifacts/batch_destroy
|
||||
# Deletes multiple collection artifacts in a single request
|
||||
def batch_destroy
|
||||
ids = batch_destroy_params[:ids] || []
|
||||
deleted_count = current_user.collection_artifacts.where(id: ids).destroy_all.count
|
||||
|
||||
render json: {
|
||||
meta: { deleted: deleted_count }
|
||||
}, status: :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def set_target_user
|
||||
|
|
@ -181,6 +198,10 @@ module Api
|
|||
data: params[:data]&.to_unsafe_h
|
||||
}
|
||||
end
|
||||
|
||||
def batch_destroy_params
|
||||
params.permit(ids: [])
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -48,6 +48,19 @@ class CollectionArtifact < ApplicationRecord
|
|||
scope :standard_only, -> { joins(:artifact).where(artifacts: { rarity: :standard }) }
|
||||
scope :quirk_only, -> { joins(:artifact).where(artifacts: { rarity: :quirk }) }
|
||||
|
||||
# Filter by skill modifier in a specific slot (1-4)
|
||||
# Uses OR logic when multiple modifiers are provided
|
||||
scope :with_skill_in_slot, ->(slot, modifiers) {
|
||||
return all if modifiers.blank?
|
||||
|
||||
modifiers = Array(modifiers).map(&:to_s)
|
||||
column = "skill#{slot}"
|
||||
|
||||
# Build OR conditions for multiple modifiers
|
||||
conditions = modifiers.map { |_| "#{column}->>'modifier' = ?" }.join(' OR ')
|
||||
where(conditions, *modifiers)
|
||||
}
|
||||
|
||||
# Returns the effective proficiency - from instance for quirk, from artifact for standard
|
||||
def effective_proficiency
|
||||
quirk_artifact? ? proficiency : artifact&.proficiency
|
||||
|
|
|
|||
Loading…
Reference in a new issue