/
properties_controller.rb
207 lines (186 loc) · 7.26 KB
/
properties_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
module Pwb
class Api::V1::PropertiesController < JSONAPI::ResourceController
# Skipping action below allows me to browse to endpoint
# without having set mime type
# skip_before_action :ensure_valid_accept_media_type
# def set_default_currency
# @model
# end
def bulk_create
propertiesJSON = params["propertiesJSON"]
unless propertiesJSON.is_a? Array
propertiesJSON = JSON.parse propertiesJSON
end
new_props = []
existing_props = []
errors = []
properties_params(propertiesJSON).each_with_index do |property_params, index|
propertyJSON = propertiesJSON[index]
if Pwb::Prop.where(reference: propertyJSON["reference"]).exists?
existing_props.push Pwb::Prop.find_by_reference propertyJSON["reference"]
# propertyJSON
else
begin
new_prop = Pwb::Prop.create(property_params)
# new_prop = Pwb::Prop.create(propertyJSON.except("features", "property_photos", "image_urls", "last_retrieved_at"))
# create will use website defaults for currency and area_unit
# need to override that
if propertyJSON["currency"]
new_prop.currency = propertyJSON["currency"]
new_prop.save!
end
if propertyJSON["area_unit"]
new_prop.area_unit = propertyJSON["area_unit"]
new_prop.save!
end
# TODO - go over supported locales and save title and description
# into them
# if propertyJSON["features"]
# TODO - process feature (currently not retrieved by PWS so not important)
# new_prop.set_features=propertyJSON["features"]
# end
if propertyJSON["property_photos"]
# uploading images can slow things down so worth setting a limit
max_photos_to_process = 20
# TODO - retrieve above as a param
propertyJSON["property_photos"].each_with_index do |property_photo, index|
if index > max_photos_to_process
return
end
photo = PropPhoto.create
photo.sort_order = property_photo["sort_order"] || nil
photo.remote_image_url = property_photo["url"]
# photo.remote_image_url = property_photo["image"]["url"] || property_photo["url"]
photo.save!
new_prop.prop_photos.push photo
end
end
new_props.push new_prop
rescue => err
errors.push err.message
# logger.error err.message
end
end
end
return render json: {
new_props: new_props,
existing_props: existing_props,
errors: errors,
}
end
# TODO: rename to update_features:
def update_extras
property = Prop.find(params[:id])
# The set_features method goes through ea
property.set_features = params[:extras].to_unsafe_hash
property.save!
return render json: property.features
end
def order_photos
ordered_photo_ids = params[:ordered_photo_ids]
ordered_array = ordered_photo_ids.split(",")
ordered_array.each.with_index(1) do |photo_id, index|
photo = PropPhoto.find(photo_id)
photo.sort_order = index
photo.save!
end
@property = Prop.find(params[:prop_id])
return render json: @property.prop_photos
# { "success": true }, status: :ok, head: :no_content
end
def add_photo_from_url
# subdomain = request.subdomain || ""
property = Prop.find(params[:id])
remote_urls = params[:remote_urls].split(",")
photos_array = []
remote_urls.each do |remote_url|
photo = PropPhoto.create
# photo.subdomain = subdomain
# photo.folder = current_tenant_model.whitelabel_country_code
# photo.tenant_id = current_tenant_model.id
# need the regex below to remove leading and trailing quotationmarks
# photo.remote_image_url = remote_url.gsub!(/\A"|"\Z/, '')
photo.remote_image_url = remote_url
photo.save!
property.prop_photos.push photo
photos_array.push photo
end
# if json below is not valid, success callback on client will fail
return render json: photos_array.to_json
# { "success": true }, status: :ok, head: :no_content
end
def add_photo
# subdomain = request.subdomain || ""
property = Prop.find(params[:id])
files_array = params[:file]
if files_array.class.to_s == "ActionDispatch::Http::UploadedFile"
# In case a single file has been sent, use as an array item
files_array = [files_array]
end
photos_array = []
files_array.each do |file|
photo = PropPhoto.create
# photo.subdomain = subdomain
# photo.folder = current_tenant_model.whitelabel_country_code
# photo.tenant_id = current_tenant_model.id
photo.image = file
photo.save!
# photo.update_attributes(:url => photo.image.metadata['url'])
# ul = Pwb::PropPhotoUploader.new
# ul.store!(file)
# tried various options like above to ensure photo.image.url
# which is nil at this point gets updated
# - in the end it was just a reload that was needed:
photo.reload
property.prop_photos.push photo
photos_array.push photo
end
# if json below is not valid, success callback on client will fail
return render json: photos_array.to_json
# { "success": true }, status: :ok, head: :no_content
end
def remove_photo
photo = PropPhoto.find(params[:id])
property = Prop.find(params[:prop_id])
property.prop_photos.destroy photo
# if json below is not valid, success callback on client will fail
return render json: { "success": true }, status: :ok, head: :no_content
end
# def set_owner
# property = Prop.find(params[:prop_id])
# client = Client.find(params[:client_id])
# property.owners = [client]
# property.save!
# return render json: "success"
# end
# def unset_owner
# property = Prop.find(params[:prop_id])
# client = Client.find(params[:client_id])
# property.owners = []
# property.save!
# return render json: "success"
# end
private
def properties_params(propertiesJSON)
# propertiesJSON = params["propertiesJSON"]
# unless propertiesJSON.is_a? Array
# propertiesJSON = JSON.parse propertiesJSON
# end
# pp = ActionController::Parameters.new(propertiesJSON)
pp = ActionController::Parameters.new({ propertiesJSON: propertiesJSON })
# https://github.com/rails/strong_parameters/issues/140
# params.require(:propertiesJSON).map do |p|
pp.require(:propertiesJSON).map do |p|
p.permit(
:title, :description,
:reference, :street_address, :city,
:postal_code, :price_rental_monthly_current,
:for_rent_short_term, :visible,
:count_bedrooms, :count_bathrooms,
:longitude, :latitude
)
# ActionController::Parameters.new(p.to_hash).permit(:title, :description)
end
end
end
end