From e91e1664a61568b351814b802cf1fb08c60de78e Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Thu, 22 Dec 2022 18:12:34 -0800 Subject: [PATCH] Update blueprint for availability endpoints --- app/blueprints/api/v1/empty_blueprint.rb | 12 ++++++------ app/controllers/api/v1/users_controller.rb | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/blueprints/api/v1/empty_blueprint.rb b/app/blueprints/api/v1/empty_blueprint.rb index 6709741..ad8b861 100644 --- a/app/blueprints/api/v1/empty_blueprint.rb +++ b/app/blueprints/api/v1/empty_blueprint.rb @@ -3,12 +3,12 @@ module Api module V1 class EmptyBlueprint < Blueprinter::Base - field :email_available, if: ->(_field_name, _empty, options) { options.key?(:email) } do |_, options| - User.where('email = ?', options[:email]).count.zero? - end - - field :username_available, if: ->(_field_name, _empty, options) { options.key?(:username) } do |_, options| - User.where('username = ?', options[:username]).count.zero? + field :available, if: ->(_field_name, _empty, options) { options.key?(:availability) } do |_, options| + if options.key?(:email) + User.where('email = ?', options[:email]).count.zero? + elsif options.key?(:username) + User.where('username = ?', options[:username]).count.zero? + end end end end diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 121c721..fdab8b0 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -69,11 +69,11 @@ module Api end def check_email - render json: EmptyBlueprint.render_as_json(nil, email: params[:email]) + render json: EmptyBlueprint.render_as_json(nil, email: params[:email], availability: true) end def check_username - render json: EmptyBlueprint.render_as_json(nil, username: params[:username]) + render json: EmptyBlueprint.render_as_json(nil, username: params[:username], availability: true) end def destroy; end