Update UsersController to use blueprinter
This commit is contained in:
parent
41c031092e
commit
5c97488a20
3 changed files with 76 additions and 53 deletions
15
app/blueprints/api/v1/empty_blueprint.rb
Normal file
15
app/blueprints/api/v1/empty_blueprint.rb
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
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?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -14,11 +14,15 @@ module Api
|
||||||
end
|
end
|
||||||
|
|
||||||
view :profile do
|
view :profile do
|
||||||
association :parties,
|
include_view :minimal
|
||||||
name: :parties,
|
|
||||||
blueprint: PartyBlueprint, view: :preview,
|
field :parties, if: ->(_fn, _obj, options) { options[:parties].length.positive? } do |_, options|
|
||||||
if: ->(_field_name, user, _options) { user.parties.length.positive? },
|
PartyBlueprint.render_as_hash(options[:parties], view: :preview)
|
||||||
&:parties
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
view :token do
|
||||||
|
fields :username, :token
|
||||||
end
|
end
|
||||||
|
|
||||||
view :settings do
|
view :settings do
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,8 @@
|
||||||
|
|
||||||
module Api
|
module Api
|
||||||
module V1
|
module V1
|
||||||
|
PER_PAGE = 15
|
||||||
|
|
||||||
class UsersController < Api::V1::ApiController
|
class UsersController < Api::V1::ApiController
|
||||||
class ForbiddenError < StandardError; end
|
class ForbiddenError < StandardError; end
|
||||||
|
|
||||||
|
|
@ -9,86 +11,88 @@ module Api
|
||||||
before_action :set_by_id, only: %w[info update]
|
before_action :set_by_id, only: %w[info update]
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.new(user_params)
|
user = User.new(user_params)
|
||||||
|
|
||||||
token = Doorkeeper::AccessToken.create!(
|
token = Doorkeeper::AccessToken.create!(
|
||||||
application_id: nil,
|
application_id: nil,
|
||||||
resource_owner_id: @user.id,
|
resource_owner_id: user.id,
|
||||||
expires_in: 30.days,
|
expires_in: 30.days,
|
||||||
scopes: 'public'
|
scopes: 'public'
|
||||||
).token
|
).token
|
||||||
|
|
||||||
return unless @user.save!
|
if user.save!
|
||||||
|
return render json: UserBlueprint.render({
|
||||||
|
id: user.id,
|
||||||
|
username: user.username,
|
||||||
|
token: token
|
||||||
|
},
|
||||||
|
view: :token),
|
||||||
|
status: :created
|
||||||
|
end
|
||||||
|
|
||||||
@presenter = {
|
render_validation_error_response(@user)
|
||||||
user_id: @user.id,
|
|
||||||
username: @user.username,
|
|
||||||
token: token
|
|
||||||
}
|
|
||||||
|
|
||||||
render :create, status: :created
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
def update
|
||||||
render :info, status: :ok if @user.update(user_params)
|
render json: UserBlueprint.render(@user, view: :minimal) if @user.update(user_params)
|
||||||
end
|
end
|
||||||
|
|
||||||
def info
|
def info
|
||||||
render :info, status: :ok
|
render json: UserBlueprint.render(@user, view: :minimal)
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
if @user
|
render_not_found_response('user') unless @user
|
||||||
@per_page = 15
|
|
||||||
|
|
||||||
now = DateTime.current
|
conditions = build_conditions(request.params)
|
||||||
unless request.params['recency'].blank?
|
conditions[:user_id] = @user.id
|
||||||
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day
|
|
||||||
end
|
|
||||||
|
|
||||||
conditions = {}
|
parties = Party
|
||||||
conditions[:element] = request.params['element'] unless request.params['element'].blank?
|
.where(conditions)
|
||||||
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank?
|
.order(created_at: :desc)
|
||||||
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
|
.paginate(page: request.params[:page], per_page: PER_PAGE)
|
||||||
conditions[:user_id] = @user.id
|
.each do |party|
|
||||||
|
party.favorited = current_user ? party.is_favorited(current_user) : false
|
||||||
@parties = Party
|
|
||||||
.where(conditions)
|
|
||||||
.order(created_at: :desc)
|
|
||||||
.paginate(page: request.params[:page], per_page: @per_page)
|
|
||||||
.each do |party|
|
|
||||||
party.favorited = current_user ? party.is_favorited(current_user) : false
|
|
||||||
end
|
|
||||||
@count = Party.where(conditions).count
|
|
||||||
else
|
|
||||||
render_not_found_response
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
count = Party.where(conditions).count
|
||||||
|
|
||||||
|
render json: UserBlueprint.render(@user,
|
||||||
|
view: :profile,
|
||||||
|
root: 'profile',
|
||||||
|
parties: parties,
|
||||||
|
meta: {
|
||||||
|
count: count,
|
||||||
|
total_pages: count.to_f / PER_PAGE > 1 ? (count.to_f / PER_PAGE).ceil : 1,
|
||||||
|
per_page: PER_PAGE
|
||||||
|
})
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_email
|
def check_email
|
||||||
@available = if params[:email].present?
|
render json: EmptyBlueprint.render_as_json(nil, email: params[:email])
|
||||||
User.where('email = ?', params[:email]).count.zero?
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
render :available
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_username
|
def check_username
|
||||||
@available = if params[:username].present?
|
render json: EmptyBlueprint.render_as_json(nil, username: params[:username])
|
||||||
User.where('username = ?', params[:username]).count.zero?
|
|
||||||
else
|
|
||||||
false
|
|
||||||
end
|
|
||||||
|
|
||||||
render :available
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy; end
|
def destroy; end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def build_conditions(params)
|
||||||
|
unless params['recency'].blank?
|
||||||
|
start_time = (DateTime.current - params['recency'].to_i.seconds)
|
||||||
|
.to_datetime.beginning_of_day
|
||||||
|
end
|
||||||
|
|
||||||
|
{}.tap do |hash|
|
||||||
|
hash[:element] = params['element'] unless params['element'].blank?
|
||||||
|
hash[:raid] = params['raid'] unless params['raid'].blank?
|
||||||
|
hash[:created_at] = start_time..now unless params['recency'].blank?
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
# Specify whitelisted properties that can be modified.
|
# Specify whitelisted properties that can be modified.
|
||||||
def set
|
def set
|
||||||
@user = User.where('username = ?', params[:id]).first
|
@user = User.where('username = ?', params[:id]).first
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue