Return the execution result of Service class in ServiceResponse class

Introduction

Until now, the execution result of the service class has been fairly appropriate.

For example, it returned the number of updates, and whether it was successful or not, with true and false, and so on.

In GitLab, I created a class called ServiceResponse and used it to return the response of the execution result, and I thought it was very good, so I will summarize it.

contents

In GitLab, it was as follows.

https://gitlab.com/gitlab-org/gitlab/-/blob/master/app/services/service_response.rb

# frozen_string_literal: true

class ServiceResponse
  def self.success(message: nil, payload: {}, http_status: :ok)
    new(status: :success, message: message, payload: payload, http_status: http_status)
  end

  def self.error(message:, payload: {}, http_status: nil)
    new(status: :error, message: message, payload: payload, http_status: http_status)
  end

  attr_reader :status, :message, :http_status, :payload

  def initialize(status:, message: nil, payload: {}, http_status: nil)
    self.status = status
    self.message = message
    self.payload = payload
    self.http_status = http_status
  end

  def success?
    status == :success
  end

  def error?
    status == :error
  end

  def errors
    return [] unless error?

    Array.wrap(message)
  end

  private

  attr_writer :status, :message, :http_status, :payload
end

How to use

If successful

ServiceResponse.success(message: 'success!')

Is set as the return value when the service class is executed,

If it fails

ServiceResponse.error(message: 'failed')

Is set as the return value when the service class is executed.

You can check the status as shown below.

response = ServiceResponse.success(message: 'success!')

response.success? #=> true
response.error? #=> false
response.status #=> :success
response.message #=> 'success!'

On the controller side

result = ArticleUpdateService.new(@article, params).call

if result.success?
  redirect_to article_path(@article), notice: result.message
elsif result.error?
  render :new, alert: result.message
end

You can divide the process depending on whether it succeeds or fails.

Recommended Posts

Return the execution result of Service class in ServiceResponse class
Get the result of POST in Java
Recommendation of Service class in Ruby on Rails
Prepare the execution environment of Tomcat in IntelliJ Community
Get the name of the test case in the JUnit test class
Summarize the additional elements of the Optional class in Java 9
Execution result memo of String.substring
Let's see the execution result when using prepared statements in JDBC
Official logo list of the service
Various methods of the String class
Examine the list of timezone IDs available in the Java ZoneId class
Order of processing in the program
Filter the result of BindingResult [Spring]
Let's express the result of analyzing Java bytecode with a class diagram
Error when the member of Entity class used in SpringWebFlux is final
The identity of params [: id] in rails
Try calling the CORBA service in Java 11+
must not return in the for statement
The story of AppClip support in Anyca
The story of writing Java in Emacs
Sort by multiple fields in the class
Write the movement of Rakefile in the runbook
Part of the class called from the class under test in JMockit Mock method memo
Call a method of the parent class by explicitly specifying the name in Ruby
Get the path defined in Controller class of Spring boot as a list
[Java] Where is the implementation class of annotation that exists in Bean Validation?
Conditional branching of the result of SQL statement to search only one in Java
Be absolutely careful when putting the result of and / or in a variable!
[Elasticsearch × Java] Since the execution result of the query acquired in Java was different from the assumption, I investigated → Summary of the corresponding contents
[Ruby] Summary of class definitions. Master the basics.
The story of low-level string comparison in Java
[Java] Handling of JavaBeans in the method chain
[Rails] Button to return to the top of the page
Store the AWS SDK return value in DynamoDB
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
When you get lost in the class name
Feel the passage of time even in Java
About next () and nextLine () of the Scanner class
A quick review of Java learned in class
How to display the result of form input
Import files of the same hierarchy in Java
First touch of the Files class (or Java 8)
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Pass arguments to the method and receive the result of the operation as a return value