Browse Source

Maintenance: Small refactoring of services layer.

Martin Gruner 2 years ago
parent
commit
e027e6341d

+ 0 - 1
app/controllers/application_controller.rb

@@ -15,5 +15,4 @@ class ApplicationController < ActionController::Base
   include ApplicationController::LogsHttpAccess
   include ApplicationController::Authorizes
   include ApplicationController::Klass
-  include ApplicationController::HandlesServices
 end

+ 0 - 17
app/controllers/application_controller/handles_services.rb

@@ -1,17 +0,0 @@
-# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
-
-module ApplicationController::HandlesServices
-  extend ActiveSupport::Concern
-
-  included do
-    # Easy build method to directly get a service object for a defined class.
-    def use_service(klass)
-      klass.new(current_user: current_user)
-    end
-
-    # Easy build method to directly call the 'execute' method of a service.
-    def execute_service(klass, ...)
-      use_service(klass).execute(...)
-    end
-  end
-end

+ 1 - 1
app/controllers/concerns/checks_user_attributes_by_current_user_permission.rb

@@ -11,6 +11,6 @@ module ChecksUserAttributesByCurrentUserPermission
     # admins can do whatever they want
     return true if current_user.permissions?('admin.user')
 
-    execute_service(User::CheckAttributesService, user_data: params)
+    Service::User::FilterPermissionAssignments.new(current_user: current_user).execute(user_data: params)
   end
 end

+ 1 - 2
app/controllers/search_controller.rb

@@ -24,8 +24,7 @@ class SearchController < ApplicationController
 
     assets = {}
     result = []
-    execute_service(
-      SearchService,
+    Service::Search.new(current_user: current_user).execute(
       term:    query,
       objects: objects.map(&:constantize),
       options: { limit: limit, ids: params[:ids] },

+ 4 - 3
app/controllers/users_controller.rb

@@ -828,19 +828,20 @@ curl http://localhost/api/v1/users/avatar -v -u #{login}:#{password} -H "Content
 =end
 
   def avatar_new
-    file_full = execute_service(Avatar::ImageValidateService, image_data: params[:avatar_full])
+    service = Service::Avatar::ImageValidate.new
+    file_full = service.execute(image_data: params[:avatar_full])
     if file_full[:error].present?
       render json: { error: file_full[:message] }, status: :unprocessable_entity
       return
     end
 
-    file_resize = execute_service(Avatar::ImageValidateService, image_data: params[:avatar_resize])
+    file_resize = service.execute(image_data: params[:avatar_resize])
     if file_resize[:error].present?
       render json: { error: file_resize[:message] }, status: :unprocessable_entity
       return
     end
 
-    render json: { avatar: execute_service(Avatar::AddService, full_image: file_full, resize_image: file_resize) }, status: :ok
+    render json: { avatar: Service::Avatar::Add.new(current_user: current_user).execute(full_image: file_full, resize_image: file_resize) }, status: :ok
   end
 
   def avatar_set_default

+ 0 - 17
app/graphql/gql/concerns/handles_services.rb

@@ -1,17 +0,0 @@
-# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
-
-module Gql::Concerns::HandlesServices
-  extend ActiveSupport::Concern
-
-  included do
-    # Easy build method to directly get a service object for a defined class.
-    def use_service(klass)
-      klass.new(current_user: context.current_user)
-    end
-
-    # Easy build method to directly call the 'execute' method of a service.
-    def execute_service(klass, ...)
-      use_service(klass).execute(...)
-    end
-  end
-end

+ 1 - 1
app/graphql/gql/mutations/account/avatar/add.rb

@@ -17,7 +17,7 @@ module Gql::Mutations
       end
 
       {
-        avatar: execute_service(Avatar::AddService, full_image: file_full, resize_image: file_resize)
+        avatar: Service::Avatar::Add.new(current_user: context.current_user).execute(full_image: file_full, resize_image: file_resize)
       }
     end
   end

+ 0 - 1
app/graphql/gql/mutations/base_mutation.rb

@@ -5,7 +5,6 @@ module Gql::Mutations
   class BaseMutation < GraphQL::Schema::Mutation
     include Gql::Concerns::HandlesAuthorization
     include Gql::Concerns::HasNestedGraphqlName
-    include Gql::Concerns::HandlesServices
 
     # FIXME: Remove when all mutations are using services which are taking care of this flag.
     include Gql::Mutations::Concerns::HandlesCoreWorkflow

+ 3 - 6
app/graphql/gql/mutations/concerns/handles_core_workflow.rb

@@ -4,13 +4,10 @@ module Gql::Mutations::Concerns::HandlesCoreWorkflow
   extend ActiveSupport::Concern
 
   included do
-    def set_core_workflow_information(params, klass, screen = 'create')
-      return if params[:screen].present?
-      return if klass.included_modules.exclude?(ChecksCoreWorkflow)
+    def set_core_workflow_information(data, klass, screen = 'create')
+      return if data[:screen].present? || klass.included_modules.exclude?(ChecksCoreWorkflow)
 
-      params[:screen] = screen
-
-      true
+      data[:screen] = screen
     end
   end
 end

+ 1 - 9
app/graphql/gql/mutations/user/add.rb

@@ -13,15 +13,7 @@ module Gql::Mutations
     end
 
     def resolve(input:)
-      { user: add(input) }
-    end
-
-    private
-
-    def add(input)
-      user_data = input.to_h
-
-      execute_service(::User::AddInternalService, user_data: user_data)
+      { user: Service::User::AddInternal.new(current_user: context.current_user).execute(user_data: input.to_h) }
     end
   end
 end

Some files were not shown because too many files changed in this diff