REPP: Move nameservers endpoint to Rails API format

This commit is contained in:
Karl Erik Õunapuu 2020-10-13 16:37:47 +03:00
parent a782b19d28
commit c31dc15207
No known key found for this signature in database
GPG key ID: C9DD647298A34764
3 changed files with 47 additions and 45 deletions

View file

@ -0,0 +1,40 @@
module Repp
module V1
module Registrar
class NameserversController < BaseController
before_action :verify_nameserver_existance, only: %i[update]
def update
domains = current_user.registrar
.replace_nameservers(hostname, hostname_params[:data][:attributes])
render_success(data: data_format_for_success(domains))
rescue ActiveRecord::RecordInvalid => e
handle_errors(e.record)
end
private
def data_format_for_success(affected_domains)
{ type: 'nameserver', id: params[:data][:attributes][:hostname],
attributes: params[:data][:attributes], affected_domains: affected_domains }
end
def hostname_params
params.require(:data).require(%i[type id])
params.require(:data).require(:attributes).require(%i[hostname ipv4 ipv6])
params.permit(data: [:type, :id, attributes: [:hostname, ipv4: [], ipv6: []]])
end
def hostname
hostname_params[:data][:id]
end
def verify_nameserver_existance
current_user.registrar.nameservers.find_by!(hostname: hostname)
end
end
end
end
end