mirror of
https://github.com/internetee/registry.git
synced 2025-06-10 06:34:46 +02:00
REPP: Move Domains#Contacts to rails API format
This commit is contained in:
parent
c31dc15207
commit
6c655e1179
3 changed files with 46 additions and 47 deletions
|
@ -1,47 +0,0 @@
|
|||
module Repp
|
||||
class DomainContactsV1 < Grape::API
|
||||
version 'v1', using: :path
|
||||
|
||||
resource :domains do
|
||||
resource :contacts do
|
||||
patch '/' do
|
||||
current_contact = current_user.registrar.contacts
|
||||
.find_by(code: params[:current_contact_id])
|
||||
new_contact = current_user.registrar.contacts.find_by(code: params[:new_contact_id])
|
||||
|
||||
unless current_contact
|
||||
error!({ error: { type: 'invalid_request_error',
|
||||
param: 'current_contact_id',
|
||||
message: "No such contact: #{params[:current_contact_id]}"} },
|
||||
:bad_request)
|
||||
end
|
||||
|
||||
unless new_contact
|
||||
error!({ error: { type: 'invalid_request_error',
|
||||
param: 'new_contact_id',
|
||||
message: "No such contact: #{params[:new_contact_id]}" } },
|
||||
:bad_request)
|
||||
end
|
||||
|
||||
if new_contact.invalid?
|
||||
error!({ error: { type: 'invalid_request_error',
|
||||
param: 'new_contact_id',
|
||||
message: 'New contact must be valid' } },
|
||||
:bad_request)
|
||||
end
|
||||
|
||||
if current_contact == new_contact
|
||||
error!({ error: { type: 'invalid_request_error',
|
||||
message: 'New contact ID must be different from current' \
|
||||
' contact ID' } },
|
||||
:bad_request)
|
||||
end
|
||||
|
||||
affected_domains, skipped_domains = TechDomainContact
|
||||
.replace(current_contact, new_contact)
|
||||
@response = { affected_domains: affected_domains, skipped_domains: skipped_domains }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
39
app/controllers/repp/v1/domains/contacts_controller.rb
Normal file
39
app/controllers/repp/v1/domains/contacts_controller.rb
Normal file
|
@ -0,0 +1,39 @@
|
|||
module Repp
|
||||
module V1
|
||||
module Domains
|
||||
class ContactsController < BaseController
|
||||
before_action :set_current_contact, only: [:update]
|
||||
before_action :set_new_contact, only: [:update]
|
||||
|
||||
def set_current_contact
|
||||
@current_contact = current_user.registrar.contacts.find_by!(
|
||||
code: params[:current_contact_id]
|
||||
)
|
||||
end
|
||||
|
||||
def set_new_contact
|
||||
@new_contact = current_user.registrar.contacts.find_by!(code: params[:new_contact_id])
|
||||
end
|
||||
|
||||
def update
|
||||
@epp_errors << { code: '2304', msg: 'New contact must be valid' } if @new_contact.invalid?
|
||||
|
||||
if @new_contact == @current_contact
|
||||
@epp_errors << { code: '2304', msg: 'New contact must be different from current' }
|
||||
end
|
||||
|
||||
affected, skipped = TechDomainContact.replace(@current_contact, @new_contact)
|
||||
data = { affected_domains: affected, skipped_domains: skipped }
|
||||
render_success(data: data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def contact_params
|
||||
params.require(%i[current_contact_id new_contact_id])
|
||||
params.permit(:current_contact_id, :new_contact_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -59,6 +59,13 @@ Rails.application.routes.draw do
|
|||
end
|
||||
end
|
||||
end
|
||||
namespace :domains do
|
||||
resources :contacts do
|
||||
collection do
|
||||
patch '/', to: 'contacts#update'
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue