Class: Stay::Api::V1::PropertiesController
- Inherits:
-
BaseApiController
- Object
- BaseApiController
- Stay::Api::V1::PropertiesController
- Includes:
- ImageResizerConcern
- Defined in:
- app/controllers/stay/api/v1/properties_controller.rb
Instance Method Summary collapse
- #create ⇒ Object
- #destroy ⇒ Object
- #index ⇒ Object
- #my_properties ⇒ Object
- #property_tax ⇒ Object
- #resubmit ⇒ Object
- #search ⇒ Object
- #show ⇒ Object
- #similar_property ⇒ Object
- #update ⇒ Object
Instance Method Details
#create ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 79 def create ActiveRecord::Base.transaction do begin @property = current_devise_api_user.properties.new(property_params) if @property.save render json: { message: "Property created", property: PropertySerializer.new(@property), success: true }, status: :created else raise ActiveRecord::RecordInvalid.new(@property) end rescue ActiveRecord::RecordInvalid => e render json: { error: @property.errors..to_sentence, success: false }, status: :unprocessable_entity raise ActiveRecord::Rollback rescue StandardError => e render json: { error: "Something went wrong: #{e.}", success: false }, status: :unprocessable_entity raise ActiveRecord::Rollback end end end |
#destroy ⇒ Object
183 184 185 186 187 188 189 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 183 def destroy if @property.destroy render json: { message: "Property destroyed successfully", success: true }, status: :ok else render json: { error: "Property not destroyed", success: :false }, status: :unprocessable_entity end end |
#index ⇒ Object
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 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 8 def index begin page = params[:page].to_i > 0 ? params[:page].to_i : 1 per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : 10 cumulative_per_page = page * per_page @data = Stay::Property.approved.active.joins(:rooms).distinct @properties = @data.order(created_at: :desc).limit(cumulative_per_page) total_count = @data.count total_pages = (total_count.to_f / per_page).ceil return render json: { data: "No properties found", properties: [], success: false }, status: :ok if @properties.empty? render json: { data: "Data Found", properties: ActiveModelSerializers::SerializableResource.new(@properties, each_serializer: PropertyListingSerializer), success: true, meta: { total_pages: total_pages, current_page: page, next_page: page < total_pages ? page + 1 : nil, prev_page: page > 1 ? page - 1 : nil, total_count: total_count } }, status: :ok rescue ActiveRecord::RecordNotFound => e render json: { success: false, error: "Properties not found", message: e. }, status: :not_found rescue ArgumentError => e render json: { success: false, error: "Invalid pagination parameters", message: e. }, status: :bad_request rescue StandardError => e render json: { success: false, error: "Internal server error", message: e. }, status: :internal_server_error end end |
#my_properties ⇒ Object
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 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 43 def my_properties begin page = params[:page].to_i > 0 ? params[:page].to_i : 1 per_page = params[:per_page].to_i > 0 ? params[:per_page].to_i : 10 cumulative_per_page = page * per_page @data = current_devise_api_user.properties @properties = @data.order(created_at: :asc).limit(cumulative_per_page) total_count = @data.count total_pages = (total_count.to_f / per_page).ceil return render json: { data: "No properties found", properties: [], success: false }, status: :ok if @properties.empty? render json: { data: "Property Found", properties: ActiveModelSerializers::SerializableResource.new(@properties, each_serializer: PropertyListingSerializer), success: true, meta: { total_pages: total_pages, current_page: page, next_page: page < total_pages ? page + 1 : nil, prev_page: page > 1 ? page - 1 : nil, total_count: total_count } }, status: :ok rescue ActiveRecord::RecordNotFound => e render json: { success: false, error: "Properties not found", message: e. }, status: :not_found rescue ArgumentError => e render json: { success: false, error: "Invalid pagination parameters", message: e. }, status: :bad_request rescue StandardError => e render json: { success: false, error: "Internal server error", message: e. }, status: :internal_server_error end end |
#property_tax ⇒ Object
169 170 171 172 173 174 175 176 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 169 def property_tax @taxes = Stay::Tax.all if @taxes.any? render json: { message: "tax found", tax: @taxes, success: true }, status: :ok else render json: { message: "No properties found" }, status: :not_found end end |
#resubmit ⇒ Object
178 179 180 181 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 178 def resubmit render json: { message: "Property resubmitted for approval", data: PropertyListingSerializer.new(@property), success: :true }, status: :ok if @property.resubmit! render json: { error: "Property not approved", success: :false }, status: :unprocessable_entity end |
#search ⇒ Object
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 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 136 def search amenity_ids = parse_to_array(params[:q][:amenity_ids]) feature_ids = parse_to_array(params[:q][:feature_ids]) latitude, longitude = params[:q].values_at(:latitude, :longitude) @q = Stay::Property.approved.active.ransack(params[:q]) @properties = @q.result.distinct @properties = @properties.with_amenities(amenity_ids) if amenity_ids.any? @properties = @properties.with_features(feature_ids) if feature_ids.any? @properties = @properties.nearby(latitude, longitude, params[:distance] || 10) if latitude && longitude @properties = @properties.by_property_type(params[:q][:property_type_id]) if params[:q][:property_type_id].present? @properties = @properties.price_filter(params[:q][:min_price], params[:q][:max_price]) if params[:q][:min_price] && params[:q][:max_price].present? @properties = @properties.by_property_category(params[:q][:property_category_id]) if params[:q][:property_category_id].present? @properties = @properties.page(params[:page]).per(params[:per_page] || 10) if @properties.any? render json: { message: "data found", properties: ActiveModelSerializers::SerializableResource.new(@properties, each_serializer: PropertyListingSerializer), total_pages: @properties.total_pages, current_page: @properties.current_page, next_page: @properties.next_page, prev_page: @properties.prev_page, total_count: @properties.total_count, success: true }, status: :ok else render json: { message: "No properties found" }, status: :not_found end end |
#show ⇒ Object
75 76 77 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 75 def show render json: { data: "Data Found", property: PropertySerializer.new(@property), success: true }, status: :ok end |
#similar_property ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 117 def similar_property begin properties = Stay::Property.similar_properties(params[:property_type_id], params[:property_category_id], params[:property_id]) if properties.exists? render json: { data: "Data Found", properties: ActiveModelSerializers::SerializableResource.new(properties, each_serializer: PropertyListingSerializer), success: true }, status: :ok else render json: { error: "No property found", success: false }, status: :unprocessable_entity end rescue StandardError => e render json: { error: "No property found ", message: e., success: false }, status: :internal_server_error end end |
#update ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/controllers/stay/api/v1/properties_controller.rb', line 98 def update ActiveRecord::Base.transaction do if @property.update(property_params) render json: { message: "property updated", property: PropertySerializer.new(@property), success: true }, status: :created else raise ActiveRecord::Rollback end rescue ActiveRecord::RecordInvalid render json: { message: @property.errors., success: false }, status: :unprocessable_entity end end |