Skip to content

Commit

Permalink
Add grape endpoint to create an artwork
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 5, 2024
1 parent 1870493 commit ec90d09
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions app/api/using_grape/artworks_endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ class ArtworksEndpoint < Grape::API
get ":id" do
Artwork.find(params[:id])
end

post do
artwork = Artwork.new(params)
if artwork.save
artwork
else
errors = {errors: artwork.errors.full_messages.to_sentence}
error! errors, 400
end
end
end
end
end
27 changes: 27 additions & 0 deletions spec/requests/using_grape/create_artwork_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require "rails_helper"

describe "POST /api/using_grape/artworks" do
context "without required params" do
it "returns a 400 and the errors" do
params = {}
post "/api/using_grape/artworks", params: params
expect(response.status).to eq 400
expect(response.parsed_body.key?("errors")).to eq true
end
end

context "with required params" do
it "returns a 201 and the new artwork" do
params = {
amount_cents: 100_000,
artist_name: "Sally Sculptor",
medium: "Clay",
title: "Fancy cup"
}
post "/api/using_grape/artworks", params: params
expect(response.status).to eq 201
artwork = Artwork.last
expect(response.parsed_body).to eq artwork.as_json
end
end
end

0 comments on commit ec90d09

Please sign in to comment.