diff --git a/.github/missing_piece_schema.jpg b/.github/missing_piece_schema.jpg deleted file mode 100644 index 7a11562..0000000 Binary files a/.github/missing_piece_schema.jpg and /dev/null differ diff --git a/.github/schema_missing_piece_final.png b/.github/schema_missing_piece_final.png new file mode 100644 index 0000000..2fd3236 Binary files /dev/null and b/.github/schema_missing_piece_final.png differ diff --git a/README.md b/README.md index 42d6200..d545c6d 100644 --- a/README.md +++ b/README.md @@ -127,7 +127,7 @@ If you'd like to demo this API on your local machine: ### Schema
- Missing Piece Schema + Missing Piece Schema
diff --git a/app/controllers/api/v1/puzzles_controller.rb b/app/controllers/api/v1/puzzles_controller.rb index ad673f0..b909142 100644 --- a/app/controllers/api/v1/puzzles_controller.rb +++ b/app/controllers/api/v1/puzzles_controller.rb @@ -7,7 +7,8 @@ def index if users != [] puzzles = Puzzle.where(user_id: users.pluck(:id)) - render json: PuzzleSerializer.new(puzzles) + current_puzzles = puzzles.where.not(status: 3) + render json: PuzzleSerializer.new(current_puzzles) elsif puzzles == [] || users == [] render json: { error: "Puzzles not found in this area" }, status: "404" end diff --git a/app/models/puzzle.rb b/app/models/puzzle.rb index e16d16a..1898acb 100644 --- a/app/models/puzzle.rb +++ b/app/models/puzzle.rb @@ -5,5 +5,5 @@ class Puzzle < ApplicationRecord validates_presence_of :user_id, :status, :title, :description, :total_pieces, :puzzle_image_url validates_numericality_of :total_pieces - enum status: { 'Available' => 0, 'Pending' => 1, 'Not Available' => 2 } + enum status: { 'Available' => 0, 'Pending' => 1, 'Not Available' => 2, "Permanently Removed" => 3 } end diff --git a/spec/models/puzzle_spec.rb b/spec/models/puzzle_spec.rb index 4e10fe4..1e534e0 100644 --- a/spec/models/puzzle_spec.rb +++ b/spec/models/puzzle_spec.rb @@ -15,6 +15,6 @@ it { should validate_numericality_of :total_pieces } it { should validate_presence_of :puzzle_image_url } - it { should define_enum_for(:status).with_values(["Available", "Pending", "Not Available"]) } + it { should define_enum_for(:status).with_values(["Available", "Pending", "Not Available", "Permanently Removed"]) } end end diff --git a/spec/requests/api/v1/puzzles_request_spec.rb b/spec/requests/api/v1/puzzles_request_spec.rb index 66e30bc..aea082a 100644 --- a/spec/requests/api/v1/puzzles_request_spec.rb +++ b/spec/requests/api/v1/puzzles_request_spec.rb @@ -53,6 +53,37 @@ 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 + + it "does not return any puzzles with a status 3='Permanently Removed' from a zipcode" do + zip_code = 12345 + + user_1 = create(:user, id: 1, zip_code:) + user_2 = create(:user, id: 2, 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, status: 3) # This will NOT be returned since it is 'Permanently Removed' + puzzle_3 = create(:puzzle, user: user_2) + + zipcode_params = { zip_code: 12345 } + + headers = { 'CONTENT_TYPE' => 'application/json' } + put "/api/v1/puzzles", 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].keys).to eq([:user_id, :status, :title, :description, :total_pieces, :notes, :puzzle_image_url]) + + expect(parsed_data[:data].size).to eq(2) + expect(parsed_data[:data][0][:attributes][:user_id]).to eq(puzzle_1.user_id) + expect(parsed_data[:data][1][:attributes][:user_id]).to eq(puzzle_3.user_id) + end end context 'when NOT successful' do diff --git a/spec/requests/api/v1/users/loans_request_spec.rb b/spec/requests/api/v1/users/loans_request_spec.rb index ba09622..5044ecf 100644 --- a/spec/requests/api/v1/users/loans_request_spec.rb +++ b/spec/requests/api/v1/users/loans_request_spec.rb @@ -97,6 +97,25 @@ expect(parsed_error_data.keys).to eq([:error]) expect(parsed_error_data[:error]).to eq("Puzzle is not available for loan.") end + + it 'returns an error message if trying to make a loan when a Puzzle status is Permanently Removed' do + user_1 = create(:user, id: 1) + user_2 = create(:user, id: 2) + puzzle_1 = create(:puzzle, user: user_1, status: 3) # Puzzle status 3 = "Permanently Removed" + + post "/api/v1/users/#{user_1.id}/loans", params: { + puzzle_id: puzzle_1.id, + borrower_id: user_2.id + } + + expect(response).to have_http_status(422) + + 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("Puzzle is not available for loan.") + end end end diff --git a/spec/requests/api/v1/users/puzzles_request_spec.rb b/spec/requests/api/v1/users/puzzles_request_spec.rb index c95474b..3fc329f 100644 --- a/spec/requests/api/v1/users/puzzles_request_spec.rb +++ b/spec/requests/api/v1/users/puzzles_request_spec.rb @@ -181,6 +181,35 @@ expect(parsed_data[:data][:attributes][:total_pieces]).to_not eq(@puzzle_2.total_pieces) expect(parsed_data[:data][:attributes][:notes]).to_not eq(@puzzle_2.notes) end + + it 'changes a single puzzles status to 3="Permanently Removed"' do + puzzle_update = { + status: 3 # FE will send us an enum digit + } + + headers = { 'CONTENT_TYPE' => 'application/json' } + patch "/api/v1/users/#{@user_1.id}/puzzles/#{@puzzle_2.id}", headers:, params: JSON.generate(puzzle_update) + + 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_2.user_id) + expect(parsed_data[:data][:attributes][:status]).to eq("Permanently Removed") # this was the only attribute updated + expect(parsed_data[:data][:attributes][:title]).to eq(@puzzle_2.title) + expect(parsed_data[:data][:attributes][:description]).to eq(@puzzle_2.description) + expect(parsed_data[:data][:attributes][:total_pieces]).to eq(@puzzle_2.total_pieces) + expect(parsed_data[:data][:attributes][:notes]).to eq(@puzzle_2.notes) + + expect(parsed_data[:data][:attributes][:status]).to_not eq(@puzzle_2.status) + end end context "when NOT successful" do