module VCAP::CloudController
  class PrivateDomainsController < RestController::ModelController
    define_attributes do
      attribute :name, String
      to_one :owning_organization
      to_many :shared_organizations, link_only: true, exclude_in: %i[create update], route_for: :get
    end

    query_parameters :name

    def delete(guid)
      do_delete(find_guid_and_validate_access(:delete, guid))
    end

    define_messages
    define_routes

    def self.translate_validation_exception(e, attributes)
      name_errors = e.errors.on(:name)
      organization_errors = e.errors.on(:organization)
      if name_errors && name_errors.include?(:unique)
        CloudController::Errors::ApiError.new_from_details('DomainNameTaken', attributes['name'])
      elsif organization_errors && organization_errors.include?(:total_private_domains_exceeded)
        CloudController::Errors::ApiError.new_from_details('TotalPrivateDomainsExceeded', Organization[guid: attributes['owning_organization_guid']].name)
      else
        CloudController::Errors::ApiError.new_from_details('DomainInvalid', e.errors.full_messages)
      end
    end

    def self.not_found_exception_name(_model_class)
      :DomainNotFound
    end
  end
end
