1
0
mirror of https://github.com/fspc/BikeShed-1.git synced 2025-02-28 08:43:23 -05:00

Added bike show api method

This commit is contained in:
Jason Denney 2014-03-08 15:03:52 -05:00
parent 523046b042
commit dc0a567a27
11 changed files with 111 additions and 10 deletions

View File

@ -1,8 +1,9 @@
class Api::V1::BikesController < Api::V1::BaseController class Api::V1::BikesController < Api::V1::BaseController
CANNOT_MANAGE = "You do not have permission to manage bikes." CANNOT_MANAGE = "You do not have permission to manage bikes."
EXPECTED_BIKE = "Expected bike in submitted data" 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 def create
if params[:bikes] && bike = params[:bikes].first if params[:bikes] && bike = params[:bikes].first
@ -15,6 +16,13 @@ class Api::V1::BikesController < Api::V1::BaseController
end end
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 private
def check_bike_permission def check_bike_permission
if cannot? :manage, Bike if cannot? :manage, Bike

View File

@ -0,0 +1,3 @@
json.bikes [@bike] do |bike|
json.array! bike
end

View File

@ -11,14 +11,15 @@ Velocipede::Application.routes.draw do
########################### ###########################
# API Routes # API Routes
scope 'api', :module => :api do scope 'api', :module => :api, defaults: {format: :json} do
scope 'v1', :module => :v1 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 '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/create' => "bikes#create", as: "api_create_bike"
post 'bikes/:id' => "bikes#show", as: "api_bike"
end end
end end

View File

@ -41,7 +41,7 @@ if Rails.env.development?
#create fake bikes #create fake bikes
if Bike.all.empty? if Bike.all.empty?
42.times do |n| 42.times do |n|
FactoryGirl.create(:bike) FactoryGirl.create(:seed_bike)
end end
end end
elsif Rails.env.production? elsif Rails.env.production?

View File

@ -1,6 +1,7 @@
require 'spec_helper' require 'spec_helper'
describe Api::V1::BikesController do describe Api::V1::BikesController do
render_views
describe "#create" do describe "#create" do
context "as a user" do context "as a user" do
@ -41,7 +42,7 @@ describe Api::V1::BikesController do
context "with valid bike in json data" do context "with valid bike in json data" do
before(:each) do before(:each) do
@submit_json = { bike: { @submit_json = { bikes: [{
serial_number: "XKCD", serial_number: "XKCD",
bike_brand_id: 1, bike_brand_id: 1,
shop_id: 1, shop_id: 1,
@ -51,11 +52,12 @@ describe Api::V1::BikesController do
bike_condition_id: 1, bike_condition_id: 1,
bike_purpose_id: 1, bike_purpose_id: 1,
bike_wheel_size_id: 1, bike_wheel_size_id: 1,
}} }]}
end end
it "returns 200" do it "returns 200" do
post :create, @submit_json post :create, @submit_json
puts @response.inspect
expect(@response.code.to_i).to eql 200 expect(@response.code.to_i).to eql 200
end end
@ -69,9 +71,9 @@ describe Api::V1::BikesController do
context "with invalid bike in json data" do context "with invalid bike in json data" do
before(:each) do before(:each) do
@submit_json = { bike: { @submit_json = { bikes: [{
serial_number: "XKCD", serial_number: "XKCD",
}} }]}
end end
it "returns 422" do it "returns 422" do
@ -88,4 +90,42 @@ describe Api::V1::BikesController do
end end
end 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 end

View File

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :bike_brand do
brand {Faker::Commerce.product_name}
end
end

View File

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :bike_condition do
condition "POOR"
end
end

View File

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :bike_purpose do
purpose "SHOP"
end
end

View File

@ -0,0 +1,5 @@
FactoryGirl.define do
factory :bike_style do
style {Faker::Commerce.product_name}
end
end

View File

@ -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

View File

@ -2,6 +2,23 @@
FactoryGirl.define do FactoryGirl.define do
factory :bike 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(:shop_id) {|n| n}
sequence :serial_number do |n| sequence :serial_number do |n|
"#{Faker::Code.isbn}-#{n}" "#{Faker::Code.isbn}-#{n}"