From 7feb5157c887b8cdbcec6cbdd8d16a237f364eeb Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Sun, 27 Feb 2022 20:00:29 -0800 Subject: [PATCH] Add method for displaying a user's favorites --- app/controllers/api/v1/parties_controller.rb | 36 ++++++++++++-------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/app/controllers/api/v1/parties_controller.rb b/app/controllers/api/v1/parties_controller.rb index 419a7b4..150c6fa 100644 --- a/app/controllers/api/v1/parties_controller.rb +++ b/app/controllers/api/v1/parties_controller.rb @@ -1,7 +1,20 @@ class Api::V1::PartiesController < Api::V1::ApiController - before_action :set_from_slug, except: ['create', 'update', 'index'] + before_action :set_from_slug, except: ['create', 'update', 'index', 'favorites'] 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) + + end + def create @party = Party.new(shortcode: random_string) @party.extra = party_params['extra'] @@ -17,6 +30,13 @@ class Api::V1::PartiesController < Api::V1::ApiController render_not_found_response if @party.nil? end + def favorites + raise Api::V1::UnauthorizedError unless current_user + + @parties = current_user.favorite_parties + render :all, status: :ok + end + def update if @party.user != current_user render_unauthorized_response @@ -49,20 +69,6 @@ class Api::V1::PartiesController < Api::V1::ApiController render :characters, status: :ok 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) - - render :all, status: :ok - end - private def random_string