From d9155fe4fee2dc5aaa38ec93189f1cbf93447f10 Mon Sep 17 00:00:00 2001 From: Kemi Thomas Date: Tue, 17 Oct 2023 17:36:28 -0400 Subject: [PATCH 1/5] initial commt --- .tool-versions | 1 + app/controllers/puzzles_controller.rb | 5 +++++ config/routes.rb | 5 ++++- spec/requests/puzzles_spec.rb | 11 +++++++++++ 4 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 .tool-versions create mode 100644 app/controllers/puzzles_controller.rb create mode 100644 spec/requests/puzzles_spec.rb diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..e7f4c11 --- /dev/null +++ b/.tool-versions @@ -0,0 +1 @@ +ruby 3.1.1 diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb new file mode 100644 index 0000000..8c2628b --- /dev/null +++ b/app/controllers/puzzles_controller.rb @@ -0,0 +1,5 @@ +class PuzzlesController < ApplicationController + def index + head :ok + end +end diff --git a/config/routes.rb b/config/routes.rb index 262ffd5..e539bfb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,9 @@ Rails.application.routes.draw do + # get 'puzzles/index' + get '/api/v1/puzzles' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html # Defines the root path route ("/") # root "articles#index" -end + resources :puzzles, only: :index +end \ No newline at end of file diff --git a/spec/requests/puzzles_spec.rb b/spec/requests/puzzles_spec.rb new file mode 100644 index 0000000..e70a4ab --- /dev/null +++ b/spec/requests/puzzles_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Puzzles", type: :request do + describe "GET /index" do + it "returns http success" do + get "/puzzles/index" + expect(response).to have_http_status(:success) + end + end + +end From 178c20315e5565ac084345a8b8bacff413589873 Mon Sep 17 00:00:00 2001 From: Kemi Thomas Date: Wed, 18 Oct 2023 10:36:44 -0400 Subject: [PATCH 2/5] puzzles index --- app/controllers/puzzles_controller.rb | 6 ++++-- config/routes.rb | 6 +----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb index 8c2628b..a3ea51c 100644 --- a/app/controllers/puzzles_controller.rb +++ b/app/controllers/puzzles_controller.rb @@ -1,5 +1,7 @@ class PuzzlesController < ApplicationController + def index - head :ok + @puzzles = Puzzle.all end -end + +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e539bfb..aa5e9a4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,5 @@ Rails.application.routes.draw do - # get 'puzzles/index' - get '/api/v1/puzzles' # Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html - # Defines the root path route ("/") - # root "articles#index" - resources :puzzles, only: :index + resources :puzzles end \ No newline at end of file From 3d71dbbec59108b1ced8f4bd26f71c5707b1ddfe Mon Sep 17 00:00:00 2001 From: Kemi Thomas Date: Thu, 19 Oct 2023 19:03:14 -0400 Subject: [PATCH 3/5] index file complete with correct routing --- app/controllers/api/v1/puzzles_controller.rb | 18 ++++++++++++++++++ app/controllers/puzzles_controller.rb | 7 ------- config/routes.rb | 2 +- 3 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 app/controllers/api/v1/puzzles_controller.rb delete mode 100644 app/controllers/puzzles_controller.rb diff --git a/app/controllers/api/v1/puzzles_controller.rb b/app/controllers/api/v1/puzzles_controller.rb new file mode 100644 index 0000000..dea4b1d --- /dev/null +++ b/app/controllers/api/v1/puzzles_controller.rb @@ -0,0 +1,18 @@ +class Api::V1::PuzzlesController < ApplicationController + + def index + + zipcode = params[:zip_code] + + user = User.find_by(zip_code: zipcode) + + if user + puzzles = user.puzzles.to_a + render json: puzzles + else + puzzles = [] + render json: { error: "Puzzles not found in this area" }, status: :not_found + end + end + +end \ No newline at end of file diff --git a/app/controllers/puzzles_controller.rb b/app/controllers/puzzles_controller.rb deleted file mode 100644 index a3ea51c..0000000 --- a/app/controllers/puzzles_controller.rb +++ /dev/null @@ -1,7 +0,0 @@ -class PuzzlesController < ApplicationController - - def index - @puzzles = Puzzle.all - end - -end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 204a60f..3d0ff5c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,7 +7,7 @@ namespace :v1 do # resources :users, only: [:show] #for ease of understanding, we will skip resoruces for now get '/users/:id', to: 'users#show' - + get 'puzzles', to: 'puzzles#index' end end end From ca5dd418281f5302c10ef6ea21dc2431a9571b13 Mon Sep 17 00:00:00 2001 From: Kemi Thomas Date: Fri, 20 Oct 2023 13:13:45 -0400 Subject: [PATCH 4/5] fixing merge conflict --- config/routes.rb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 8ca6376..092e637 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,11 +6,8 @@ namespace :api do namespace :v1 do # resources :users, only: [:show] #for ease of understanding, we will skip resoruces for now -<<<<<<< HEAD get 'puzzles', to: 'puzzles#index' - get '/users/:user_id/puzzles/:puzzle_id', to: 'users/puzzles#show' -======= ->>>>>>> 49ab3114a5b0246e3d8308ca61eb0cca1b7ccb3b + get '/users/:user_id', to: 'users#show' get '/users/:user_id/dashboard', to: 'users#dashboard' From 4e2f14ed0f8f719d7f7a2a8d323ba5a2154289a7 Mon Sep 17 00:00:00 2001 From: Kemi Thomas Date: Fri, 20 Oct 2023 15:19:27 -0400 Subject: [PATCH 5/5] refactored index method and have passing tests --- app/controllers/api/v1/puzzles_controller.rb | 18 ++--- config/routes.rb | 2 +- spec/requests/api/v1/puzzles_request_spec.rb | 84 ++++++++++++++++++++ spec/requests/puzzles_spec.rb | 79 ------------------ 4 files changed, 93 insertions(+), 90 deletions(-) create mode 100644 spec/requests/api/v1/puzzles_request_spec.rb delete mode 100644 spec/requests/puzzles_spec.rb diff --git a/app/controllers/api/v1/puzzles_controller.rb b/app/controllers/api/v1/puzzles_controller.rb index 626538b..4bc37d5 100644 --- a/app/controllers/api/v1/puzzles_controller.rb +++ b/app/controllers/api/v1/puzzles_controller.rb @@ -1,18 +1,16 @@ class Api::V1::PuzzlesController < ApplicationController def index - - zip_code = params[:zip_code] - - @users = User.where(zip_code: zip_code) if zip_code.present? - if @users.present? + zip_code = params[:zip_code] - @puzzles = Puzzle.where(user_id: @users.pluck(:id)) - render json: @puzzles - else - puzzles = [] - render json: { error: "Puzzles not found in this area" }, status: :not_found + users = User.where(zip_code: zip_code) if zip_code.present? + + if users != [] + puzzles = Puzzle.where(user_id: users.pluck(:id)) + render json: PuzzleSerializer.new(puzzles) + elsif puzzles == [] || users = [] + render json: { error: "Puzzles not found in this area" }, status: "404" end end diff --git a/config/routes.rb b/config/routes.rb index 092e637..4301db4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,7 +6,7 @@ namespace :api do namespace :v1 do # resources :users, only: [:show] #for ease of understanding, we will skip resoruces for now - get 'puzzles', to: 'puzzles#index' + put 'puzzles', to: 'puzzles#index' get '/users/:user_id', to: 'users#show' get '/users/:user_id/dashboard', to: 'users#dashboard' diff --git a/spec/requests/api/v1/puzzles_request_spec.rb b/spec/requests/api/v1/puzzles_request_spec.rb new file mode 100644 index 0000000..39a06f8 --- /dev/null +++ b/spec/requests/api/v1/puzzles_request_spec.rb @@ -0,0 +1,84 @@ +require 'rails_helper' + +RSpec.describe "PuzzlesController", type: :request do + describe "#ndex" do + context 'when successful' do + it "returns all puzzles from a zipcode" do + zip_code = 12345 + + user_1 = create(:user, id: 1, zip_code: zip_code) + user_2 = create(:user, id: 2, zip_code: zip_code) + user_3 = create(:user, id: 3, zip_code: 54321) + puzzle_1 = create(:puzzle, user: user_1) + puzzle_2 = create(:puzzle, user: user_1) + puzzle_3 = create(:puzzle, user: user_2) + puzzle_4 = create(:puzzle, user: user_3) + + zipcode_params = { zip_code: 12345 } + + headers = { 'CONTENT_TYPE' => 'application/json' } + put "/api/v1/puzzles", headers: headers, params: JSON.generate(zipcode_params) + + expect(response).to have_http_status(200) + + parsed_data = JSON.parse(response.body, symbolize_names: true) + + expect(parsed_data).to be_a(Hash) + expect(parsed_data.keys).to eq([:data]) + expect(parsed_data[:data]).to be_an(Array) + expect(parsed_data[:data][0]).to be_a(Hash) + expect(parsed_data[:data][0].keys).to eq([:id, :type, :attributes]) + + expect(parsed_data[:data][0][:attributes]).to be_a(Hash) + expect(parsed_data[:data][0][:attributes].keys).to eq([:user_id, :status, :title, :description, :total_pieces, :notes, :puzzle_image_url]) + expect(parsed_data[:data][0][:attributes][:user_id]).to eq(puzzle_1.user_id) + expect(parsed_data[:data][0][:attributes][:status]).to eq(puzzle_1.status) + expect(parsed_data[:data][0][:attributes][:title]).to eq(puzzle_1.title) + expect(parsed_data[:data][0][:attributes][:description]).to eq(puzzle_1.description) + expect(parsed_data[:data][0][:attributes][:total_pieces]).to eq(puzzle_1.total_pieces) + expect(parsed_data[:data][0][:attributes][:notes]).to eq(puzzle_1.notes) + expect(parsed_data[:data][0][:attributes][:puzzle_image_url]).to eq(puzzle_1.puzzle_image_url) + + # NOTE Could refactor tests to not see a puzzle_4 in the test because it's in a diff zipcode + + expect(parsed_data[:data][2][:attributes]).to be_a(Hash) + expect(parsed_data[:data][2][:attributes].keys).to eq([:user_id, :status, :title, :description, :total_pieces, :notes, :puzzle_image_url]) + expect(parsed_data[:data][2][:attributes][:user_id]).to eq(puzzle_3.user_id) + expect(parsed_data[:data][2][:attributes][:status]).to eq(puzzle_3.status) + expect(parsed_data[:data][2][:attributes][:title]).to eq(puzzle_3.title) + expect(parsed_data[:data][2][:attributes][:description]).to eq(puzzle_3.description) + expect(parsed_data[:data][2][:attributes][:total_pieces]).to eq(puzzle_3.total_pieces) + expect(parsed_data[:data][2][:attributes][:notes]).to eq(puzzle_3.notes) + expect(parsed_data[:data][2][:attributes][:puzzle_image_url]).to eq(puzzle_3.puzzle_image_url) + + + end + end + + context 'when NOT successful' do + it 'returns an error message when zipcode is not found' do + zip_code = 12345 + user_1 = create(:user, id: 1, zip_code: 54321) + user_2 = create(:user, id: 2, zip_code: 12346) + puzzle_1 = create(:puzzle, user: user_1) + puzzle_2 = create(:puzzle, user: user_1) + puzzle_3 = create(:puzzle, user: user_2) + puzzle_4 = create(:puzzle, user: user_2) + + zipcode_params = { zip_code: 00000 } + + headers = { 'CONTENT_TYPE' => 'application/json' } + put "/api/v1/puzzles", headers: headers, params: JSON.generate(zipcode_params) + + expect(response).to have_http_status(404) + + parsed_error_data = JSON.parse(response.body, symbolize_names: true) + + expect(parsed_error_data).to be_a(Hash) + expect(parsed_error_data.keys).to eq([:error]) + expect(parsed_error_data[:error]).to eq("Puzzles not found in this area") + end + + end + end +end diff --git a/spec/requests/puzzles_spec.rb b/spec/requests/puzzles_spec.rb deleted file mode 100644 index 5269052..0000000 --- a/spec/requests/puzzles_spec.rb +++ /dev/null @@ -1,79 +0,0 @@ -require 'rails_helper' - -RSpec.describe "PuzzlesController", type: :request do - describe "#ndex" do - context 'when successful' do - it "returns all puzzles from a zipcode" do - zip_code = 12345 - - user_1 = create(:user, id: 1, zip_code: zip_code) - user_2 = create(:user, id: 2, zip_code: zip_code) - puzzle_1 = create(:puzzle, user: user_1) - puzzle_2 = create(:puzzle, user: user_1) - puzzle_3 = create(:puzzle, user: user_2) - puzzle_4 = create(:puzzle, user: user_2) - - request.headers['CONTENT_TYPE'] = ‘application/json’ - get "/api/v1/puzzles", request.headers - - expect(response).to have_http_status(200) - - parsed_data = JSON.parse(response.body, symbolize_names: true) - - expect(parsed_data).to be_a(Hash) - expect(parsed_data.keys).to eq([:data]) - expect(parsed_data[:data]).to be_a(Hash) - expect(parsed_data[:data].keys).to eq([:id, :type, :attributes]) - - - expect(parsed_data[:data][:attributes]).to be_a(Hash) - expect(parsed_data[:data][:attributes].keys).to eq([:user_id, :status, :title, :description, :total_pieces, :notes, :puzzle_image_url]) - expect(parsed_data[:data][:attributes][:user_id]).to eq(puzzle_1.user_id) - expect(parsed_data[:data][:attributes][:status]).to eq(puzzle_1.status) - expect(parsed_data[:data][:attributes][:title]).to eq(puzzle_1.title) - expect(parsed_data[:data][:attributes][:description]).to eq(puzzle_1.description) - expect(parsed_data[:data][:attributes][:total_pieces]).to eq(puzzle_1.total_pieces) - expect(parsed_data[:data][:attributes][:notes]).to eq(puzzle_1.notes) - expect(parsed_data[:data][:attributes][:puzzle_image_url]).to eq(puzzle_1.puzzle_image_url) - - expect(parsed_data[:data][:attributes]).to be_a(Hash) - expect(parsed_data[:data][:attributes].keys).to eq([:user_id, :status, :title, :description, :total_pieces, :notes, :puzzle_image_url]) - expect(parsed_data[:data][:attributes][:user_id]).to eq(puzzle_3.user_id) - expect(parsed_data[:data][:attributes][:status]).to eq(puzzle_3.status) - expect(parsed_data[:data][:attributes][:title]).to eq(puzzle_3.title) - expect(parsed_data[:data][:attributes][:description]).to eq(puzzle_3.description) - expect(parsed_data[:data][:attributes][:total_pieces]).to eq(puzzle_3.total_pieces) - expect(parsed_data[:data][:attributes][:notes]).to eq(puzzle_3.notes) - expect(parsed_data[:data][:attributes][:puzzle_image_url]).to eq(puzzle_3.puzzle_image_url) - - - end - end - - context 'when NOT successful' do - it 'returns an error message when zipcode is not found' do - zip_code = 12345 - user_1 = create(:user, id: 1, zip_code: 54321) - user_2 = create(:user, id: 2, zip_code: 12346) - puzzle_1 = create(:puzzle, user: user_1) - puzzle_2 = create(:puzzle, user: user_1) - puzzle_3 = create(:puzzle, user: user_2) - puzzle_4 = create(:puzzle, user: user_2) - - get '/api/v1/puzzles' - - expect(response).to have_http_status(404) - - parsed_error_data = JSON.parse(response.body, symbolize_names: true) - - - expect(parsed_error_data).to be_a(Hash) - expect(parsed_error_data.keys).to eq([:error]) - expect(parsed_error_data[:error][0].keys).to eq([:status, :title, :detail]) - expect(parsed_error_data[:error][0][:status]).to eq("404") - expect(parsed_error_data[:error][0][:detail]).to eq("Puzzles not found in this area") - end - - end - end -end