From dc0a567a2726af101bcbcf7c18a7453bacddbeb0 Mon Sep 17 00:00:00 2001 From: Jason Denney Date: Sat, 8 Mar 2014 15:03:52 -0500 Subject: [PATCH] Added bike show api method --- app/controllers/api/v1/bikes_controller.rb | 10 +++- app/views/api/v1/bikes/show.json.jbuilder | 3 ++ config/routes.rb | 9 ++-- db/seeds.rb | 2 +- spec/controllers/api/bikes_controller_spec.rb | 48 +++++++++++++++++-- spec/factories/bike_brands.rb | 5 ++ spec/factories/bike_conditions.rb | 5 ++ spec/factories/bike_purposes.rb | 5 ++ spec/factories/bike_styles.rb | 5 ++ spec/factories/bike_wheel_sizes.rb | 12 +++++ spec/factories/bikes.rb | 17 +++++++ 11 files changed, 111 insertions(+), 10 deletions(-) create mode 100644 app/views/api/v1/bikes/show.json.jbuilder create mode 100644 spec/factories/bike_brands.rb create mode 100644 spec/factories/bike_conditions.rb create mode 100644 spec/factories/bike_purposes.rb create mode 100644 spec/factories/bike_styles.rb create mode 100644 spec/factories/bike_wheel_sizes.rb diff --git a/app/controllers/api/v1/bikes_controller.rb b/app/controllers/api/v1/bikes_controller.rb index 6064bfd..976a04f 100644 --- a/app/controllers/api/v1/bikes_controller.rb +++ b/app/controllers/api/v1/bikes_controller.rb @@ -1,8 +1,9 @@ class Api::V1::BikesController < Api::V1::BaseController CANNOT_MANAGE = "You do not have permission to manage bikes." EXPECTED_BIKE = "Expected bike in submitted data" + NOT_FOUND = "The bike could not be found." - before_filter :check_bike_permission + before_filter :check_bike_permission, except: :show def create if params[:bikes] && bike = params[:bikes].first @@ -15,6 +16,13 @@ class Api::V1::BikesController < Api::V1::BaseController end end + def show + @bike = Bike.find_by_id(params[:id]) + if @bike.nil? + render json: { errors: [NOT_FOUND] }, status: 404 and return + end + end + private def check_bike_permission if cannot? :manage, Bike diff --git a/app/views/api/v1/bikes/show.json.jbuilder b/app/views/api/v1/bikes/show.json.jbuilder new file mode 100644 index 0000000..cdaee86 --- /dev/null +++ b/app/views/api/v1/bikes/show.json.jbuilder @@ -0,0 +1,3 @@ +json.bikes [@bike] do |bike| + json.array! bike +end diff --git a/config/routes.rb b/config/routes.rb index b6485d4..918d81c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -11,14 +11,15 @@ Velocipede::Application.routes.draw do ########################### # API Routes - scope 'api', :module => :api do + scope 'api', :module => :api, defaults: {format: :json} do scope 'v1', :module => :v1 do - post 'checkin' => "logs#checkin", :as => "api_checkin" + post 'checkin' => "logs#checkin", :as => "api_checkin" post 'checkout' => "logs#checkout", :as => "api_checkout" - post 'reset' => "users#password_reset", :as => "api_password_reset" + post 'reset' => "users#password_reset", :as => "api_password_reset" + + get 'bikes/:id' => "bikes#show", as: "api_bike" post 'bikes/create' => "bikes#create", as: "api_create_bike" - post 'bikes/:id' => "bikes#show", as: "api_bike" end end diff --git a/db/seeds.rb b/db/seeds.rb index e06aaf5..60e7917 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -41,7 +41,7 @@ if Rails.env.development? #create fake bikes if Bike.all.empty? 42.times do |n| - FactoryGirl.create(:bike) + FactoryGirl.create(:seed_bike) end end elsif Rails.env.production? diff --git a/spec/controllers/api/bikes_controller_spec.rb b/spec/controllers/api/bikes_controller_spec.rb index 3f99c86..e9e2338 100644 --- a/spec/controllers/api/bikes_controller_spec.rb +++ b/spec/controllers/api/bikes_controller_spec.rb @@ -1,6 +1,7 @@ require 'spec_helper' describe Api::V1::BikesController do + render_views describe "#create" do context "as a user" do @@ -41,7 +42,7 @@ describe Api::V1::BikesController do context "with valid bike in json data" do before(:each) do - @submit_json = { bike: { + @submit_json = { bikes: [{ serial_number: "XKCD", bike_brand_id: 1, shop_id: 1, @@ -51,11 +52,12 @@ describe Api::V1::BikesController do bike_condition_id: 1, bike_purpose_id: 1, bike_wheel_size_id: 1, - }} + }]} end it "returns 200" do post :create, @submit_json + puts @response.inspect expect(@response.code.to_i).to eql 200 end @@ -69,9 +71,9 @@ describe Api::V1::BikesController do context "with invalid bike in json data" do before(:each) do - @submit_json = { bike: { + @submit_json = { bikes: [{ serial_number: "XKCD", - }} + }]} end it "returns 422" do @@ -88,4 +90,42 @@ describe Api::V1::BikesController do end end end + + describe "#show" do + context "as a user" do + before(:each) do + @user = FactoryGirl.create(:user) + sign_in @user + end + + context "no bike exists" do + it "returns 404" do + get :show, id: 999 + expect(@response.code.to_i).to eql 404 + end + + it "returns an error message" do + get :show, id: 999 + json = JSON.parse(@response.body) + expect(json["errors"].first).to eql Api::V1::BikesController::NOT_FOUND + end + end + + context "a bike exists" do + let!(:bike){ FactoryGirl.create(:bike) } + + it "returns 200" do + get :show, id: bike.id, format: :json + expect(@response.code.to_i).to eql 200 + end + + it "returns the bike json" do + get :show, id: bike.id, format: :json + json = JSON.parse(@response.body) + expect(json).to have_key("bikes") + expect(json.to_s).to include(bike.serial_number) + end + end + end + end end diff --git a/spec/factories/bike_brands.rb b/spec/factories/bike_brands.rb new file mode 100644 index 0000000..9b2f2df --- /dev/null +++ b/spec/factories/bike_brands.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :bike_brand do + brand {Faker::Commerce.product_name} + end +end diff --git a/spec/factories/bike_conditions.rb b/spec/factories/bike_conditions.rb new file mode 100644 index 0000000..fcb8b5e --- /dev/null +++ b/spec/factories/bike_conditions.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :bike_condition do + condition "POOR" + end +end diff --git a/spec/factories/bike_purposes.rb b/spec/factories/bike_purposes.rb new file mode 100644 index 0000000..a854e0b --- /dev/null +++ b/spec/factories/bike_purposes.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :bike_purpose do + purpose "SHOP" + end +end diff --git a/spec/factories/bike_styles.rb b/spec/factories/bike_styles.rb new file mode 100644 index 0000000..b1653e8 --- /dev/null +++ b/spec/factories/bike_styles.rb @@ -0,0 +1,5 @@ +FactoryGirl.define do + factory :bike_style do + style {Faker::Commerce.product_name} + end +end diff --git a/spec/factories/bike_wheel_sizes.rb b/spec/factories/bike_wheel_sizes.rb new file mode 100644 index 0000000..08bf418 --- /dev/null +++ b/spec/factories/bike_wheel_sizes.rb @@ -0,0 +1,12 @@ +FactoryGirl.define do + factory :bike_wheel_size do + twmm 40 + rdmm 635 + twin "1 1/2 [1 3/8]" + rdin 28 + twfr "38B [35B]" + rdfr 700 + description "a big wheel" + tire_common_score 1 + end +end diff --git a/spec/factories/bikes.rb b/spec/factories/bikes.rb index e2f04ea..18ea59f 100644 --- a/spec/factories/bikes.rb +++ b/spec/factories/bikes.rb @@ -2,6 +2,23 @@ FactoryGirl.define do factory :bike do + sequence(:shop_id) {|n| n} + sequence :serial_number do |n| + "#{Faker::Code.isbn}-#{n}" + end + bike_brand { FactoryGirl.create(:bike_brand) } + model { Faker::Commerce.product_name } + color "FFFFFF" + bike_style { FactoryGirl.create(:bike_style) } + seat_tube_height 42 + top_tube_length 42 + bike_wheel_size { FactoryGirl.create(:bike_wheel_size) } + value 200 + bike_condition { FactoryGirl.create(:bike_condition) } + bike_purpose { FactoryGirl.create(:bike_purpose) } + end + + factory :seed_bike do sequence(:shop_id) {|n| n} sequence :serial_number do |n| "#{Faker::Code.isbn}-#{n}"