Skip to content

Commit

Permalink
Merge pull request #6524 from pulibrary/6509-delete-file-sets
Browse files Browse the repository at this point in the history
Add service and task to delete all file sets from a resource
  • Loading branch information
eliotjordan authored Oct 2, 2024
2 parents d49d1ee + 4e8fda2 commit 508a926
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
25 changes: 25 additions & 0 deletions app/services/file_set_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true
class FileSetService
def self.delete_all_from(resource_id)
new.delete_all_from(resource_id: resource_id)
end

def delete_all_from(resource_id:)
resource = query_service.find_by(id: resource_id)
change_set_persister.buffer_into_index do |buffered_change_set_persister|
Wayfinder.for(resource).file_sets.each do |file_set|
buffered_change_set_persister.delete(change_set: ChangeSet.for(file_set))
end
end
end

private

def change_set_persister
@change_set_persister ||= ChangeSetPersister.default
end

def query_service
change_set_persister.query_service
end
end
12 changes: 12 additions & 0 deletions lib/tasks/file_sets.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true
namespace :figgy do
namespace :file_sets do
desc "Delete all FileSets from a resource"
task delete: :environment do
resource_id = ENV["ID"]
abort "usage: ID=id rake figgy:file_sets:delete" unless resource_id

FileSetService.delete_all_from(resource_id)
end
end
end
23 changes: 23 additions & 0 deletions spec/services/file_set_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true
require "rails_helper"

RSpec.describe FileSetService do
describe ".delete_all_from" do
it "deletes all member file sets from a resource but no other member types" do
resource = FactoryBot.create_for_repository(:scanned_map_with_raster_child, state: "pending")
wayfinder = Wayfinder.for(resource)
file_sets = wayfinder.file_sets
raster_children = wayfinder.raster_resources
expect(file_sets.count).to eq 1
expect(raster_children.count).to eq 1

described_class.delete_all_from(resource.id.to_s)
reloaded = ChangeSetPersister.default.query_service.find_by(id: resource.id.to_s)
wayfinder = Wayfinder.for(reloaded)
file_sets = wayfinder.file_sets
raster_children = wayfinder.raster_resources
expect(file_sets.count).to eq 0
expect(raster_children.count).to eq 1
end
end
end

0 comments on commit 508a926

Please sign in to comment.