Skip to content
This repository has been archived by the owner on May 10, 2021. It is now read-only.

Commit

Permalink
add place create api
Browse files Browse the repository at this point in the history
  • Loading branch information
y-yagi committed Jan 7, 2016
1 parent e51dfc6 commit 93344aa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/controllers/api/v1/places_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,14 @@ def require_resources
::Place.mine(current_resource_owner).includes(places_station: :station).order(updated_at: :desc)
end
end

def create_resource
@resource = ::Place.create!(
name: params[:name], address: params[:address],
latitude: params[:latitude], longitude: params[:longitude],
user_id: current_resource_owner.id
)
@resource.set_station_info
@resource
end
end
2 changes: 2 additions & 0 deletions app/models/api/place.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ module Api::Place

def self.build_permissions(perms, other, target)
perms.permits! :read if perms.user == other
perms.permits! :write if perms.user == other
end

def build_permissions(perms, other)
perms.permits! :read if perms.user == other
perms.permits! :write if perms.user == other
end

def station_info
Expand Down
1 change: 1 addition & 0 deletions config/initializers/garage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
register :public, desc: 'acessing publicly available data' do
access :read, Travel
access :read, Place
access :write, Place
end
end
Garage.configuration.strategy = Garage::Strategy::Doorkeeper
Expand Down
2 changes: 1 addition & 1 deletion config/routes/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
namespace :api do
namespace :v1 do
resources :travels, only: %i(index show)
resources :places, only: %i(index)
resources :places, only: %i(index create update destroy show)
end
end
15 changes: 15 additions & 0 deletions test/controllers/api/v1/places_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,19 @@ class Api::V1::PlacesControllerTest < ActionController::TestCase
assert_equal 1, parsed_response_body.size
assert_equal latest_place.id, parsed_response_body.first['id']
end

test 'can create place' do
assert_difference 'Place.count' do
post :create, format: :json, user_id: @user.email, user_provider: @user.provider,
name: '赤羽駅', address: '東京都北区赤羽一丁目1-1', latitude: 35.46409, longitude: 139.4315
end

assert_response :success

place = Place.last
assert_equal '赤羽駅', place.name
assert_equal '東京都北区赤羽一丁目1-1', place.address
assert_equal 35.46409, place.latitude
assert_equal 139.4315, place.longitude
end
end

0 comments on commit 93344aa

Please sign in to comment.