Skip to content

Commit

Permalink
Merge pull request #61 from WWC-Hackathon-2023/remove_puzzle
Browse files Browse the repository at this point in the history
BE | Refactor to add `Puzzle Status = Permanently Removed`
  • Loading branch information
MelTravelz authored Oct 26, 2023
2 parents fb41a52 + bfe2d3b commit f2c163d
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 4 deletions.
Binary file removed .github/missing_piece_schema.jpg
Binary file not shown.
Binary file added .github/schema_missing_piece_final.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ If you'd like to demo this API on your local machine:
### Schema

<div align="center">
<img src=".github/missing_piece_schema.jpg" alt="Missing Piece Schema">
<img src=".github/schema_missing_piece_final.png" alt="Missing Piece Schema">
</div>

<!-- Testing -->
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/api/v1/puzzles_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/puzzle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion spec/models/puzzle_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
31 changes: 31 additions & 0 deletions spec/requests/api/v1/puzzles_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions spec/requests/api/v1/users/loans_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
29 changes: 29 additions & 0 deletions spec/requests/api/v1/users/puzzles_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f2c163d

Please sign in to comment.