From 74ba5565ed142c535fa9593cf1bd86d524885b1d Mon Sep 17 00:00:00 2001 From: Lauren Oliveri Date: Tue, 1 Aug 2017 18:17:31 -0600 Subject: [PATCH] add post endpoint for bracks --- app/assets/javascripts/new.js | 1 + app/controllers/api/v1/bracks_controller.rb | 17 ++++++++- app/models/brack.rb | 17 +++++++++ config/routes.rb | 2 +- spec/requests/api/v1/bracks/create_spec.rb | 38 +++++++++++++++++++++ 5 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 spec/requests/api/v1/bracks/create_spec.rb diff --git a/app/assets/javascripts/new.js b/app/assets/javascripts/new.js index 1a2752e..ee7fe62 100644 --- a/app/assets/javascripts/new.js +++ b/app/assets/javascripts/new.js @@ -81,5 +81,6 @@ function moveToStepTwo() { } function moveToStepThree() { + // make ajax post request to new bracks } diff --git a/app/controllers/api/v1/bracks_controller.rb b/app/controllers/api/v1/bracks_controller.rb index 2898d2e..ef547c4 100644 --- a/app/controllers/api/v1/bracks_controller.rb +++ b/app/controllers/api/v1/bracks_controller.rb @@ -1,4 +1,4 @@ -class Api::V1::BracksController < ApplicationController +class Api::V1::BracksController < ActionController::API def index latlng = params[:latlng] @@ -6,4 +6,19 @@ def index 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 diff --git a/app/models/brack.rb b/app/models/brack.rb index 3de16ad..9609d8d 100644 --- a/app/models/brack.rb +++ b/app/models/brack.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 7df8d65..97fae2f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,7 +10,7 @@ namespace :api do namespace :v1 do - resources :bracks, only: [:index] + resources :bracks, only: [:index, :create] end end end diff --git a/spec/requests/api/v1/bracks/create_spec.rb b/spec/requests/api/v1/bracks/create_spec.rb new file mode 100644 index 0000000..88b874c --- /dev/null +++ b/spec/requests/api/v1/bracks/create_spec.rb @@ -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