Class: VsCode::Settings::CreateOrUpdateService

Inherits:
Object
  • Object
show all
Defined in:
app/services/vs_code/settings/create_or_update_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(current_user:, params: {}) ⇒ CreateOrUpdateService

Returns a new instance of CreateOrUpdateService.



6
7
8
9
# File 'app/services/vs_code/settings/create_or_update_service.rb', line 6

def initialize(current_user:, params: {})
  @current_user = current_user
  @params = params
end

Instance Method Details

#executeObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/services/vs_code/settings/create_or_update_service.rb', line 11

def execute
  setting = VsCode::VsCodeSetting.by_user(current_user).by_setting_type(params[:setting_type]).first

  if setting.nil?
    merged_params = params.merge(user: current_user)
    setting = VsCode::VsCodeSetting.new(merged_params)
  else
    setting.content = params[:content]
  end

  if setting.save
    ServiceResponse.success(payload: setting)
  else
    ServiceResponse.error(
      message: setting.errors.full_messages.to_sentence,
      payload: { setting: setting }
    )
  end
end