class Employee::AddressBook::UserContactsController < Employee::Controller
  def index
    render_success :ok, json: contacts.map { |c| contact_props(c) }
  end

  def show
    if contact.present?
      render_success :ok, json: contact_props(contact)
    else
      render_error :not_found
    end
  end

  def create
    contact = contacts.build(contact_params)
    if contact.save
      render_success :created, json: contact_props(contact)
    else
      render_error :unprocessable_entity, errors: contact
    end
  end

  def destroy
    if contact.destroy
      render_success :ok
    else
      render_error :unprocessable_entity, errors: contact
    end
  end

  private
    def contacts
      current_user.user_contacts
    end

    def contact
      @contact ||= contacts.find_by(contact_id: params[:id])
    end

    def contact_params
      params.permit(:contact_id)
    end

    def contact_props(contact)
      {
        id: contact.id,
        contact_id: contact.contact_id
      }
    end
end
