Retrospring/lib/use_case/relationship/destroy.rb

44 lines
1022 B
Ruby
Raw Normal View History

# frozen_string_literal: true
require "use_case/base"
require "errors"
module UseCase
module Relationship
class Destroy < UseCase::Base
option :current_user, type: Types::Coercible::String | Types.Instance(User)
option :target_user, type: Types::Coercible::String | Types.Instance(User)
option :type, type: Types::RelationshipTypes
def call
source_user = find_source_user
target_user = find_target_user
source_user.public_send("un#{type}", target_user)
{
status: 204,
resource: nil,
extra: {
target_user: target_user
}
}
end
private
def find_source_user
::User.find_by!(screen_name: current_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
def find_target_user
::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
end
end
end