Retrospring/lib/use_case/relationship/create.rb

48 lines
1.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "use_case/base"
require "errors"
module UseCase
module Relationship
class Create < UseCase::Base
option :current_user, type: Types::Coercible::String
option :target_user, type: Types::Coercible::String
option :type, type: Types::RelationshipTypes
def call
source_user = find_source_user
target_user = find_target_user
source_user.public_send(type, target_user)
{
2022-01-16 09:51:27 -08:00
status: 201,
resource: true,
2022-01-16 09:51:27 -08:00
extra: {
target_user: target_user
}
}
end
private
def find_source_user
return current_user if current_user.is_a?(::User)
2022-01-16 09:51:27 -08:00
::User.find_by!(screen_name: current_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
def find_target_user
return target_user if target_user.is_a?(::User)
2022-01-16 09:51:27 -08:00
::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
end
end
end