app/controllers/pwb/api/v1/properties_controller.rb in pwb-0.1.1 vs app/controllers/pwb/api/v1/properties_controller.rb in pwb-1.0.0
- old
+ new
@@ -8,30 +8,49 @@
# binding.pry
# @model
# end
def bulk_create
- propertiesRaw = params["propertiesJSON"]
- propertiesJSON = JSON.parse propertiesRaw
+ propertiesJSON = params["propertiesJSON"]
+ unless propertiesJSON.is_a? Array
+ propertiesJSON = JSON.parse propertiesJSON
+ end
new_props = []
existing_props = []
+ errors = []
propertiesJSON.each do |propertyJSON|
if Pwb::Prop.where(reference: propertyJSON["reference"]).exists?
existing_props.push propertyJSON
else
begin
- new_prop = Pwb::Prop.create propertyJSON
+ new_prop = Pwb::Prop.create propertyJSON.except "extras", "property_photos"
+ if propertyJSON["extras"]
+ new_prop.set_extras=propertyJSON["extras"]
+ end
+ if propertyJSON["property_photos"]
+ propertyJSON["property_photos"].each do |property_photo|
+ photo = PropPhoto.create
+ photo.sort_order = property_photo["sort_order"] || nil
+ 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
+ # binding.pry
+ errors.push err.message
# logger.error err.message
end
end
end
return render json: {
new_props: new_props,
- existing_props: existing_props
+ existing_props: existing_props,
+ errors: errors
}
end
def update_extras
property = Prop.find(params[:id])