Skip to content

Commit

Permalink
refactor: Puzzles#index action Oh YEA
Browse files Browse the repository at this point in the history
'
  • Loading branch information
MelTravelz committed Oct 29, 2023
1 parent bb0d75d commit 5279102
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions app/controllers/api/v1/puzzles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ class Api::V1::PuzzlesController < ApplicationController
skip_before_action :set_current_user, only: [:index]

def index
puzzles_in_zip_code = Puzzle.find_by_zip_code(params[:zip_code])
find_puzzles(params[:zip_code])
raise NoPuzzlesException if @puzzles_in_zip_code.empty?
render json: PuzzleSerializer.new(@puzzles_in_zip_code)
end

private

if puzzles_in_zip_code.any?
render json: PuzzleSerializer.new(puzzles_in_zip_code)
else
raise NoPuzzlesException
end
def find_puzzles(zip_code)
@puzzles_in_zip_code = Puzzle.find_by_zip_code(zip_code)
end
end

# Note to self: Saving this to remember process that lead me to final version:
#def index
# puzzles_in_zip_code = Puzzle.find_by_zip_code(params[:zip_code])
# if @puzzles_in_zip_code.any?
# render json: PuzzleSerializer.new(@puzzles_in_zip_code)
# else
# raise NoPuzzlesException
# end
# end

0 comments on commit 5279102

Please sign in to comment.