diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index fab384e..9ebaee8 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -36,7 +36,13 @@ class Api::V1::PartiesController < Api::V1::ApiController def favorites raise Api::V1::UnauthorizedError unless current_user - @parties = current_user.favorite_parties.each { |party| + 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? + conditions[:favorites] = { user_id: current_user.id } + + @parties = Party.joins(:favorites).where(conditions).each { |party| party.favorited = party.is_favorited(current_user) } diff --git a/app/models/favorite.rb b/app/models/favorite.rb index 921a7c3..a074c9a 100644 --- a/app/models/favorite.rb +++ b/app/models/favorite.rb @@ -1,6 +1,6 @@ class Favorite < ApplicationRecord belongs_to :user - has_one :party + belongs_to :party def party Party.find(self.party_id) diff --git a/app/models/party.rb b/app/models/party.rb index 122a5d0..0f0c080 100644 --- a/app/models/party.rb +++ b/app/models/party.rb @@ -2,12 +2,12 @@ class Party < ApplicationRecord ##### ActiveRecord Associations belongs_to :user, optional: true belongs_to :raid, optional: true - belongs_to :favorite, optional: true has_many :characters, foreign_key: "party_id", class_name: "GridCharacter", dependent: :destroy has_many :weapons, foreign_key: "party_id", class_name: "GridWeapon", dependent: :destroy has_many :summons, foreign_key: "party_id", class_name: "GridSummon", dependent: :destroy - + has_many :favorites + attr_accessor :favorited def is_favorited(user)