Skip to content

Commit

Permalink
add post endpoint for bracks
Browse files Browse the repository at this point in the history
  • Loading branch information
lao9 committed Aug 2, 2017
1 parent 08ce663 commit 74ba556
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/assets/javascripts/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,6 @@ function moveToStepTwo() {
}

function moveToStepThree() {
// make ajax post request to new bracks

}
17 changes: 16 additions & 1 deletion app/controllers/api/v1/bracks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
class Api::V1::BracksController < ApplicationController
class Api::V1::BracksController < ActionController::API

def index
latlng = params[:latlng]
@bracks = Brack.sort_by_distance(latlng)
render json: @bracks, latlng: latlng
end

def create
@brack = Brack.add_new(brack_params)
if @brack.save
render json: @brack
else
render json: @bracj.errors.full_messages, status: 500
end
end

private

def brack_params
params.require(:brack).permit(:user_id, :lat, :long)
end

end
17 changes: 17 additions & 0 deletions app/models/brack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,21 @@ def self.sort_by_distance(latlng)
brack.calculate_distance(latlng, brack.lat, brack.long)
end.take(20)
end

def self.add_new(params)
# set lat and long
lat = params[:lat]
long = params[:long]
# set user
user = User.find(params[:user_id])
owner = user.organization
unless owner
owner = "#{user.first_name} #{user.last_name[0]}."
end
# get cross_streets
lat_long = {lat: lat, long: long}
cross_streets = CrossStreetService.find_by_coordinates(lat_long)
# create new instance
new(cross_streets: cross_streets, lat: lat, long: long, owner: owner)
end
end
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace :api do
namespace :v1 do
resources :bracks, only: [:index]
resources :bracks, only: [:index, :create]
end
end
end
38 changes: 38 additions & 0 deletions spec/requests/api/v1/bracks/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require 'rails_helper'

describe "Bracks Create API" do
let(:user1) {create(:user)}
let(:user2) {create(:user, organization: nil)}
let(:brack) {build(:brack, cross_streets: nil)}
it "adds a new brack location owned by user with an organization" do
expect(Brack.count).to eq(0)

post '/api/v1/bracks', params: {brack: {user_id: user1.id, lat: brack.lat, long: brack.long}}

expect(response).to be_success
expect(Brack.count).to eq(1)

new_brack = Brack.all[0]

expect(new_brack.lat).to eq(39.749598)
expect(new_brack.long).to eq(brack.long)
expect(new_brack.cross_streets).to eq("15th St & Blake St")
expect(new_brack.owner).to eq(user1.organization)
end
it "adds a new brack location owned by user without an organization" do
expect(Brack.count).to eq(0)

post '/api/v1/bracks', params: {brack: {user_id: user2.id, lat: brack.lat, long: brack.long}}

expect(response).to be_success
expect(Brack.count).to eq(1)

new_brack = Brack.all[0]
user_name = "#{user2.first_name} #{user2.last_name[0]}."

expect(new_brack.lat).to eq(39.749598)
expect(new_brack.long).to eq(brack.long)
expect(new_brack.cross_streets).to eq("15th St & Blake St")
expect(new_brack.owner).to eq(user_name)
end
end

0 comments on commit 74ba556

Please sign in to comment.