README.md in command_service_object-0.4.0 vs README.md in command_service_object-0.5.0

- old
+ new

@@ -23,85 +23,115 @@ Add this line to your application's Gemfile: ```ruby gem 'command_service_object' ``` - And then execute: $ bundle Or install it yourself as: $ gem install command_service_object +Next, you need to run the generator: + +```bash +$ rails generate service:install +``` + ## Usage $ rails g service [service_name] [usecases usecases] -### Example +### Generate Service ex: - $ rails g service user create update delete + $ rails g service auth login output ```bash -create app/services/user_service -create app/services/user_service/usecases -create app/services/user_service/commands -create app/services/user_service/errors -create app/services/user_service/usecases/create.rb -create app/services/user_service/commands/create.rb -create app/services/user_service/usecases/update.rb -create app/services/user_service/commands/update.rb -create app/services/user_service/usecases/delete.rb -create app/services/user_service/commands/delete.rb +app/services/ +├── application_service.rb +├── auth_service +│   ├── commands +│   │   └── login.rb +│   └── usecases +│   ├── login.rb +│   ├── login.rb +│ ├── getters +│   └── setters +│   └── user_profile_image.rb +├── case_base.rb +└── service_result.rb ``` + +### Generate setters and getters ex: + + $ rails g service:setter auth user_profile_image + $ rails g service:getter auth user_balance + then you can edit command params > you can read [Virtus gem docs](https://github.com/solnic/virtus) for more info. ```ruby -# app/services/user_service/commands/create.rb -module UserService::Commands - class Create +# app/services/auth_service/commands/login.rb +# frozen_string_literal: true + +module AuthService::Commands + class Login + # You can read Virtus gem doc for more info. + # https://github.com/solnic/virtus include Virtus.model + include ActiveModel::Validations - attribute :name, String - attribute :phone, String - attribute :age, Integer + # Attributes + # attribute :REPLACE_ME, String + + # Validations + # validates :REPLACE_ME, presence: true end end ``` and then add your business logic ```ruby -# app/services/user_service/usecases/create.rb -module UserService::Usecases - class Create < ServiceBase +# app/services/auth_service/usecases/login.rb +# frozen_string_literal: true + +module AuthService::Usecases + class Login < CaseBase + include CommandServiceObject::Hooks + setters :user_profile_image + getters :user_balance + # + # Your business logic goes here, keep [call] method clean by using private + # methods for Business logic. + # def call - # your business logic goes here - # keep call method clean by using private methods for Business logic - do_something - do_another_something + result = user_profile_image(image_url) # set user profile image ex. + balance = user_balance # get user balance ex. end - private - - def do_something - # Business logic - # Don't catch errors ApplicationService will do that for you - raise Errors::CustomeError if ERROR + # This method will run if call method raise error + def rollback + # rollback logic end - def do_another_something - # another business logic - end + private + + def replace_me + # [business logic] + end end end + ``` usage from controller ```ruby -class UsersController < ApplicationController - def create - cmd = UserService::Commands::Create.new(user_params) - result = ApplicationService.call(cmd) +class AuthenticationController < ApplicationController + default_service :auth_service + + def Login + cmd = command.new(params) # AuthService::Commands::Login.new + result = execute(cmd) if result.ok? render json: result.value!.as_json, status: 201 else render json: { message: result.error }, status: 422