Refactor how we output favorite bool on parties

This commit is contained in:
Justin Edmund 2022-02-27 22:28:17 -08:00
parent 8d9b434d1e
commit 07dc8dd457
5 changed files with 27 additions and 25 deletions

View file

@ -2,19 +2,6 @@ class Api::V1::PartiesController < Api::V1::ApiController
before_action :set_from_slug, except: ['create', 'update', 'index', 'favorites'] before_action :set_from_slug, except: ['create', 'update', 'index', 'favorites']
before_action :set, only: ['update', 'destroy'] before_action :set, only: ['update', 'destroy']
def index
now = DateTime.current
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
conditions = {}
conditions[:element] = request.params['element'] unless request.params['element'].blank?
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank?
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
@parties = Party.where(conditions)
render :all, status: :ok
end
def create def create
@party = Party.new(shortcode: random_string) @party = Party.new(shortcode: random_string)
@party.extra = party_params['extra'] @party.extra = party_params['extra']
@ -27,14 +14,32 @@ class Api::V1::PartiesController < Api::V1::ApiController
end end
def show def show
@favorited = (current_user) ? @party.is_favorited(current_user) : false
render_not_found_response if @party.nil? render_not_found_response if @party.nil?
end end
def index
now = DateTime.current
start_time = (now - params['recency'].to_i.seconds).to_datetime.beginning_of_day unless request.params['recency'].blank?
conditions = {}
conditions[:element] = request.params['element'] unless request.params['element'].blank?
conditions[:raid] = request.params['raid'] unless request.params['raid'].blank?
conditions[:created_at] = start_time..now unless request.params['recency'].blank?
@parties = Party.where(conditions).each { |party|
party.favorited = (current_user) ? party.is_favorited(current_user) : false
}
render :all, status: :ok
end
def favorites def favorites
raise Api::V1::UnauthorizedError unless current_user raise Api::V1::UnauthorizedError unless current_user
@parties = current_user.favorite_parties @parties = current_user.favorite_parties.each { |party|
party.favorited = party.is_favorited(current_user)
}
render :all, status: :ok render :all, status: :ok
end end
@ -80,6 +85,7 @@ class Api::V1::PartiesController < Api::V1::ApiController
def set_from_slug def set_from_slug
@party = Party.where("shortcode = ?", params[:id]).first @party = Party.where("shortcode = ?", params[:id]).first
@party.favorited = (current_user) ? @party.is_favorited(current_user) : false
end end
def set def set

View file

@ -1,15 +1,11 @@
object :party object :party
attributes :id, :name, :description, :element, :shortcode, :created_at, :updated_at attributes :id, :name, :description, :element, :favorited, :shortcode, :created_at, :updated_at
node :is_extra do |p| node :extra do |p|
p.extra p.extra
end end
node :is_favorite do |p|
@favorited
end
node :user do |p| node :user do |p|
partial('users/base', :object => p.user) partial('users/base', :object => p.user)
end end

View file

@ -1,6 +1,6 @@
object @party object @party
attributes :id, :name, :description, :shortcode attributes :id, :name, :description, :shortcode, :favorited, :created_at, :updated_at
node :user do |p| node :user do |p|
partial('users/base', :object => p.user) partial('users/base', :object => p.user)

View file

@ -1,6 +1,6 @@
object @party object @party
attributes :id, :name, :description, :shortcode attributes :id, :name, :description, :shortcode, :favorited, :created_at, :updated_at
node :user do |p| node :user do |p|
partial('users/base', :object => p.user) partial('users/base', :object => p.user)

View file

@ -1,6 +1,6 @@
object @party object @party
attributes :id, :name, :description, :shortcode attributes :id, :name, :description, :shortcode, :favorited, :created_at, :updated_at
node :user do |p| node :user do |p|
partial('users/base', :object => p.user) partial('users/base', :object => p.user)
@ -10,7 +10,7 @@ node :raid do |p|
partial('raids/base', :object => p.raid) partial('raids/base', :object => p.raid)
end end
node :is_extra do |p| node :extra do |p|
p.extra p.extra
end end