Skip to content

Commit 756d951

Browse files
Merge branch 'issue-28_store-lat/lng-as-strings'
2 parents a1e729c + 1dac3d6 commit 756d951

File tree

5 files changed

+21
-7
lines changed

5 files changed

+21
-7
lines changed

app/controllers/api/v1/hike_events_controller.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
class Api::V1::HikeEventsController < ApplicationController
22
before_action :require_token, only: [:current_user, :create, :destroy, :update, :join, :leave, :confirm]
33

4+
include StrUtil
5+
46
def current_user
57
render json: @user.hike_events
68
end
@@ -110,8 +112,8 @@ def safe_params (p)
110112
return
111113
end
112114

113-
lat = p[:lat].to_f
114-
lng = p[:lng].to_f
115+
lat = StrUtil::is_valid_float?(p[:lat])
116+
lng = StrUtil::is_valid_float?(p[:lng])
115117

116118
if !lat || !lng
117119
render json: { error: 'Invalid \'lat\' and \'lng\' params. They failed to parse as floating point numbers' }, status: 400 and return
@@ -126,7 +128,7 @@ def safe_params (p)
126128
render json: { error: 'Invalid \'difficulty\' param. It failed to parse as an integer or it was out of range (1 <= x <= 10).' }, status: 400 and return
127129
end
128130

129-
return { title: p[:title], description: p[:description], duration: duration, lat: lat, lng: lng, difficulty: difficulty }
131+
return { title: p[:title], description: p[:description], duration: duration, lat: lat.to_s, lng: lng.to_s, difficulty: difficulty }
130132
end
131133

132134
def create_params

config/initializers/util.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
require 'rsa_util.rb'
2-
require 'jwt_util.rb'
2+
require 'str_util.rb'
3+
require 'jwt_util.rb'
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class ChangeLatLngColsToString < ActiveRecord::Migration[7.0]
2+
def change
3+
change_column :hike_events, :lat, :string
4+
change_column :hike_events, :lng, :string
5+
end
6+
end

db/schema.rb

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/str_util.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module StrUtil
2+
def self::is_valid_float? (str)
3+
!!Float(str) rescue false
4+
end
5+
end

0 commit comments

Comments
 (0)