2022-01-02 15:31:55 -08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require "use_case/base"
|
|
|
|
require "errors"
|
|
|
|
|
|
|
|
module UseCase
|
|
|
|
module Relationship
|
|
|
|
class Destroy < UseCase::Base
|
2022-06-09 14:08:38 -07:00
|
|
|
option :source_user, type: Types::Strict::String | Types.Instance(::User)
|
|
|
|
option :target_user, type: Types::Strict::String | Types.Instance(::User)
|
2022-01-16 09:38:06 -08:00
|
|
|
option :type, type: Types::RelationshipTypes
|
2022-01-02 15:31:55 -08:00
|
|
|
|
|
|
|
def call
|
|
|
|
source_user = find_source_user
|
|
|
|
target_user = find_target_user
|
|
|
|
|
|
|
|
source_user.public_send("un#{type}", target_user)
|
|
|
|
|
|
|
|
{
|
2022-01-16 09:51:27 -08:00
|
|
|
status: 204,
|
2022-01-02 15:31:55 -08:00
|
|
|
resource: nil,
|
2022-01-16 09:51:27 -08:00
|
|
|
extra: {
|
2022-01-02 15:31:55 -08:00
|
|
|
target_user: target_user
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def find_source_user
|
2022-01-22 13:48:55 -08:00
|
|
|
return source_user if source_user.is_a?(::User)
|
|
|
|
|
|
|
|
::User.find_by!(screen_name: source_user)
|
2022-01-02 15:31:55 -08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
raise Errors::UserNotFound
|
|
|
|
end
|
|
|
|
|
|
|
|
def find_target_user
|
2022-01-22 13:48:55 -08:00
|
|
|
return target_user if target_user.is_a?(::User)
|
|
|
|
|
2022-01-15 02:08:58 -08:00
|
|
|
::User.find_by!(screen_name: target_user)
|
2022-01-02 15:31:55 -08:00
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
raise Errors::UserNotFound
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|