Class: ApiExt::V1::PropsController

Inherits:
JSONAPI::ResourceController
  • Object
show all
Defined in:
app/controllers/pwb/api_ext/v1/props_controller.rb

Overview

class ApiExt::V1::PropsController < ActionController::Base

Instance Method Summary collapse

Instance Method Details

#bulk_create_with_tokenObject



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
# File 'app/controllers/pwb/api_ext/v1/props_controller.rb', line 90

def bulk_create_with_token
  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.except "extras", "property_photos"
        if propertyJSON["extras"]
          new_prop.set_features = 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
        errors.push err.message
        # logger.error err.message
      end
    end
  end

  render json: {
    new_props: new_props,
    existing_props: existing_props,
    errors: errors
  }
end

#cors_preflight_checkObject

If this is a preflight OPTIONS request, then short-circuit the request, return only the necessary headers and return an empty text/plain.



31
32
33
34
35
36
37
38
39
# File 'app/controllers/pwb/api_ext/v1/props_controller.rb', line 31

def cors_preflight_check
  if request.method == :options
    headers['Access-Control-Allow-Origin'] = '*'
    headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
    headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
    headers['Access-Control-Max-Age'] = '1728000'
    render text: '', content_type: 'text/plain'
  end
end

#cors_set_access_control_headersObject

For all responses in this controller, return the CORS access control headers.



21
22
23
24
25
# File 'app/controllers/pwb/api_ext/v1/props_controller.rb', line 21

def cors_set_access_control_headers
  headers['Access-Control-Allow-Origin'] = '*'
  headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
  headers['Access-Control-Max-Age'] = "1728000"
end

#create_with_tokenObject



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
# File 'app/controllers/pwb/api_ext/v1/props_controller.rb', line 41

def create_with_token
  unless params["token"] == "20182018"
    return render_json_error "Invalid Token"
  end
  propertyJSON = params["property"]
  pwb_prop = {}
  message = ""

  if Pwb::Prop.where(reference: propertyJSON["reference"]).exists?
    pwb_prop = Pwb::Prop.find_by_reference propertyJSON["reference"]
    pwb_prop.update property_params
    pwb_prop.save!
    # message = "PWB property already exists"
  else
    begin
      pwb_prop = Pwb::Prop.create property_params
      # propertyJSON.except "extras", "property_photos"
      # message = "PWB property added"
    rescue => err
      # logger.error err.message
      return render_json_error err.message
    end
  end

  if params["extras"]
    pwb_prop.set_features = params["extras"]
  end
  if params["property_photos"] && (pwb_prop.prop_photos.count < 1)
    params["property_photos"].each do |property_photo|
      begin
        photo = PropPhoto.create
        # photo.sort_order = property_photo["sort_order"] || nil
        # photo.remote_image_url = property_photo["image"]["url"] || property_photo["url"]
        photo.remote_image_url = property_photo
        photo.save!
        pwb_prop.prop_photos.push photo
      rescue Exception => err
        logger.error err.message
      end
    end
  end

  message = "Property published"
  render json: {
    pwb_prop: pwb_prop,
    message: message
  }
end