Skip to content

Commit

Permalink
Add endpoint to list artworks
Browse files Browse the repository at this point in the history
  • Loading branch information
jonallured committed Mar 5, 2024
1 parent 7445619 commit f8da606
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 8 deletions.
10 changes: 10 additions & 0 deletions app/controllers/api/using_nothing/artworks_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Api
module UsingNothing
class ArtworksController < ApplicationController
def index
artworks = Artwork.all
render json: artworks
end
end
end
end
2 changes: 2 additions & 0 deletions app/models/artwork.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Artwork < ApplicationRecord
end
13 changes: 5 additions & 8 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
Rails.application.routes.draw do
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
# Can be used by load balancers and uptime monitors to verify that the app is live.
get "up" => "rails/health#show", :as => :rails_health_check

# Defines the root path route ("/")
# root "posts#index"
namespace :api do
namespace :using_nothing do
resources :artworks
end
end
end
11 changes: 11 additions & 0 deletions db/migrate/20240305151845_create_artworks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateArtworks < ActiveRecord::Migration[7.1]
def change
create_table :artworks do |t|
t.string :artist_name
t.string :title
t.string :medium
t.integer :amount_cents
t.timestamps
end
end
end
26 changes: 26 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions spec/factories/artwork.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
FactoryBot.define do
factory :artwork do
amount_cents { 77_000 }
artist_name { "Polly Painter" }
medium { "Very wet paint" }
title { "Just like some flowers or whatever" }
end
end
33 changes: 33 additions & 0 deletions spec/requests/using_nothing/list_artworks_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

describe "/api/using_nothing/artworks" do
context "without any artworks" do
it "returns an empty array" do
get "/api/using_nothing/artworks"
expect(response.status).to eq 200
expect(response.parsed_body).to eq []
end
end

context "with an artwork" do
let!(:artwork) { FactoryBot.create(:artwork) }

it "returns that artwork" do
get "/api/using_nothing/artworks"
expect(response.status).to eq 200
expect(response.parsed_body).to eq [artwork].as_json
end
end

context "with a few artworks" do
before do
FactoryBot.create_list(:artwork, 3)
end

it "returns those artworks" do
get "/api/using_nothing/artworks"
expect(response.status).to eq 200
expect(response.parsed_body.count).to eq 3
end
end
end

0 comments on commit f8da606

Please sign in to comment.