Skip to content

Commit

Permalink
Add 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 638f524 commit cd7a6bc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
16 changes: 16 additions & 0 deletions app/controllers/api/using_nothing/artworks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ def show
artwork = Artwork.find(params[:id])
render json: artwork
end

def create
artwork = Artwork.new(artwork_params)
if artwork.save
render json: artwork, status: :created
else
errors = {errors: artwork.errors.full_messages.to_sentence}
render json: errors, status: :bad_request
end
end

private

def artwork_params
params.permit(:amount_cents, :artist_name, :medium, :title)
end
end
end
end
4 changes: 4 additions & 0 deletions app/models/artwork.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
class Artwork < ApplicationRecord
validates :amount_cents, presence: true
validates :artist_name, presence: true
validates :medium, presence: true
validates :title, presence: true
end
26 changes: 26 additions & 0 deletions spec/requests/using_nothing/create_artwork_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require "rails_helper"

describe "POST /api/using_nothing/artworks" do
context "without required params" do
it "returns a 400 and the errors" do
params = {}
post "/api/using_nothing/artworks", params: params
expect(response.status).to eq 400
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_nothing/artworks", params: params
expect(response.status).to eq 201
artwork = Artwork.last
expect(response.parsed_body).to eq artwork.as_json
end
end
end
2 changes: 1 addition & 1 deletion spec/requests/using_nothing/list_artworks_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "/api/using_nothing/artworks" do
describe "GET /api/using_nothing/artworks" do
context "without any artworks" do
it "returns an empty array" do
get "/api/using_nothing/artworks"
Expand Down
2 changes: 1 addition & 1 deletion spec/requests/using_nothing/show_artwork_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "rails_helper"

describe "/api/using_nothing/artworks/:id" do
describe "GET /api/using_nothing/artworks/:id" do
let!(:artwork) { FactoryBot.create(:artwork) }

context "with invalid id" do
Expand Down

0 comments on commit cd7a6bc

Please sign in to comment.