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

@ -1,45 +0,0 @@
module Repp
class NameserversV1 < Grape::API
version 'v1', using: :path
resource 'registrar/nameservers' do
put '/' do
params do
requires :data, type: Hash, allow_blank: false do
requires :type, type: String, allow_blank: false
requires :id, type: String, allow_blank: false
requires :attributes, type: Hash, allow_blank: false do
requires :hostname, type: String, allow_blank: false
requires :ipv4, type: Array
requires :ipv6, type: Array
end
end
end
hostname = params[:data][:id]
unless current_user.registrar.nameservers.exists?(hostname: hostname)
error!({ errors: [{ title: "Hostname #{hostname} does not exist" }] }, 404)
end
new_attributes = {
hostname: params[:data][:attributes][:hostname],
ipv4: params[:data][:attributes][:ipv4],
ipv6: params[:data][:attributes][:ipv6],
}
begin
affected_domains = current_user.registrar.replace_nameservers(hostname, new_attributes)
rescue ActiveRecord::RecordInvalid => e
error!({ errors: e.record.errors.full_messages.map { |error| { title: error } } }, 400)
end
status 200
@response = { data: { type: 'nameserver',
id: params[:data][:attributes][:hostname],
attributes: params[:data][:attributes] },
affected_domains: affected_domains }
end
end
end
end

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

View file

@ -52,6 +52,13 @@ Rails.application.routes.draw do
end
resources :auctions, only: %i[index]
resources :retained_domains, only: %i[index]
namespace :registrar do
resources :nameservers do
collection do
put '/', to: 'nameservers#update'
end
end
end
end
end