diff --git a/app/controllers/api/using_nothing/artworks_controller.rb b/app/controllers/api/using_nothing/artworks_controller.rb index 5148435..d13ce8e 100644 --- a/app/controllers/api/using_nothing/artworks_controller.rb +++ b/app/controllers/api/using_nothing/artworks_controller.rb @@ -21,6 +21,16 @@ def create end end + def update + artwork = Artwork.find(params[:id]) + if artwork.update(artwork_params) + render json: artwork + else + errors = {errors: artwork.errors.full_messages.to_sentence} + render json: errors, status: :bad_request + end + end + private def artwork_params diff --git a/spec/requests/using_nothing/create_artwork_spec.rb b/spec/requests/using_nothing/create_artwork_spec.rb index 8455a0a..f2b1b79 100644 --- a/spec/requests/using_nothing/create_artwork_spec.rb +++ b/spec/requests/using_nothing/create_artwork_spec.rb @@ -6,6 +6,7 @@ params = {} post "/api/using_nothing/artworks", params: params expect(response.status).to eq 400 + expect(response.parsed_body.key?("errors")).to eq true end end diff --git a/spec/requests/using_nothing/update_artwork_spec.rb b/spec/requests/using_nothing/update_artwork_spec.rb new file mode 100644 index 0000000..e3eba5b --- /dev/null +++ b/spec/requests/using_nothing/update_artwork_spec.rb @@ -0,0 +1,46 @@ +require "rails_helper" + +describe "PUT /api/using_nothing/artworks/:id" do + let(:artwork) { FactoryBot.create(:artwork, title: "Fooncy cup") } + + context "with invalid update" do + it "returns 400 and the errors" do + params = {title: ""} + + put "/api/using_nothing/artworks/#{artwork.id}", params: params + + expect(response.status).to eq 400 + expect(response.parsed_body.key?("errors")).to eq true + end + end + + context "with partial update" do + it "returns 200 and that updated artwork" do + params = {title: "Fancy cup"} + + put "/api/using_nothing/artworks/#{artwork.id}", params: params + + expect(response.status).to eq 200 + expect(response.parsed_body["title"]).to eq "Fancy cup" + end + end + + context "with full update" do + it "returns a 200 and that updated artwork" do + params = { + amount_cents: 100_000, + artist_name: "Sally Sculptor", + medium: "Clay", + title: "Fancy cup" + } + + put "/api/using_nothing/artworks/#{artwork.id}", params: params + + expect(response.status).to eq 200 + expect(response.parsed_body["amount_cents"]).to eq 100_000 + expect(response.parsed_body["artist_name"]).to eq "Sally Sculptor" + expect(response.parsed_body["medium"]).to eq "Clay" + expect(response.parsed_body["title"]).to eq "Fancy cup" + end + end +end