Only accept users to relationship use cases

Dry Types was having issues with taking either an object or string so it's easier to deal with just passing in an object directly
This commit is contained in:
Karina Kwiatek 2022-06-11 23:10:20 +02:00 committed by Karina Kwiatek
parent a48a187086
commit d67ae1eb19
3 changed files with 6 additions and 48 deletions

View File

@ -12,7 +12,7 @@ class Ajax::RelationshipController < AjaxController
UseCase::Relationship::Create.call( UseCase::Relationship::Create.call(
source_user: current_user, source_user: current_user,
target_user: params[:screen_name], target_user: ::User.find_by!(screen_name: params[:screen_name]),
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true
@ -26,7 +26,7 @@ class Ajax::RelationshipController < AjaxController
def destroy def destroy
UseCase::Relationship::Destroy.call( UseCase::Relationship::Destroy.call(
source_user: current_user, source_user: current_user,
target_user: params[:screen_name], target_user: ::User.find_by!(screen_name: params[:screen_name]),
type: params[:type] type: params[:type]
) )
@response[:success] = true @response[:success] = true

View File

@ -6,14 +6,11 @@ require "errors"
module UseCase module UseCase
module Relationship module Relationship
class Create < UseCase::Base class Create < UseCase::Base
option :source_user, type: Types::Strict::String | Types.Instance(::User) option :source_user, type: Types.Instance(::User)
option :target_user, type: Types::Strict::String | Types.Instance(::User) option :target_user, type: Types.Instance(::User)
option :type, type: Types::RelationshipTypes option :type, type: Types::RelationshipTypes
def call def call
source_user = find_source_user
target_user = find_target_user
source_user.public_send(type, target_user) source_user.public_send(type, target_user)
{ {
@ -24,24 +21,6 @@ module UseCase
} }
} }
end end
private
def find_source_user
return source_user if source_user.is_a?(::User)
::User.find_by!(screen_name: source_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
def find_target_user
return target_user if target_user.is_a?(::User)
::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
end end
end end
end end

View File

@ -6,14 +6,11 @@ require "errors"
module UseCase module UseCase
module Relationship module Relationship
class Destroy < UseCase::Base class Destroy < UseCase::Base
option :source_user, type: Types::Strict::String | Types.Instance(::User) option :source_user, type: Types.Instance(::User)
option :target_user, type: Types::Strict::String | Types.Instance(::User) option :target_user, type: Types.Instance(::User)
option :type, type: Types::RelationshipTypes option :type, type: Types::RelationshipTypes
def call def call
source_user = find_source_user
target_user = find_target_user
source_user.public_send("un#{type}", target_user) source_user.public_send("un#{type}", target_user)
{ {
@ -24,24 +21,6 @@ module UseCase
} }
} }
end end
private
def find_source_user
return source_user if source_user.is_a?(::User)
::User.find_by!(screen_name: source_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
def find_target_user
return target_user if target_user.is_a?(::User)
::User.find_by!(screen_name: target_user)
rescue ActiveRecord::RecordNotFound
raise Errors::UserNotFound
end
end end
end end
end end