ファイル
btrc-hub/backend/app/services/post_import_runner.rb
T
2026-07-11 21:11:36 +09:00

25 行
900 B
Ruby

class PostImportRunner
def initialize actor:, rows:
@actor = actor
@rows = rows
end
def run
results = @rows.map { |row| run_row(row.to_h.stringify_keys) }
{ created: results.count { _1[:status] == 'created' }, skipped: results.count { _1[:status] == 'skipped' },
failed: results.count { _1[:status] == 'failed' }, rows: results }
end
private
def run_row row
return row.slice('source_row').merge(status: 'skipped') if row['status'] == 'skipped' || row['existing']
attributes = row.fetch('attributes', { }).to_h.transform_keys { _1.to_s.underscore }
attributes['url'] = row['url']
post = PostCreator.new(actor: @actor, attributes:).create!
{ source_row: row['source_row'], status: 'created', post: PostRepr.base(post) }
rescue StandardError => e
{ source_row: row['source_row'], status: 'failed', errors: { base: [e.message] } }
end
end