Class: Stay::Api::V1::BookingQueriesController
- Inherits:
-
BaseApiController
- Object
- BaseApiController
- Stay::Api::V1::BookingQueriesController
- Defined in:
- app/controllers/stay/api/v1/booking_queries_controller.rb
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'app/controllers/stay/api/v1/booking_queries_controller.rb', line 77 def create ActiveRecord::Base.transaction do return render json: { error: "you can not query for your own property", success: false }, status: :unprocessable_entity if current_devise_api_user == @property.user chat = create_chat(current_devise_api_user, @property) if chat.persisted? booking_query = chat.build_booking_query(booking_query_params.merge(property: @property, user: current_devise_api_user)) if booking_query.save Stay::Chat::QueryMessagingService.new(booking_query, current_devise_api_user).perform render json: { success: true, booking_query: BookingQuerySerializer.new(booking_query) }, status: :created else raise ActiveRecord::Rollback end else raise ActiveRecord::Rollback, "Failed to create chat" end end rescue ActiveRecord::Rollback => e render json: { success: false, errors: e. || booking_query.errors. }, status: :unprocessable_entity end |
#host_query ⇒ 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 74 75 |
# File 'app/controllers/stay/api/v1/booking_queries_controller.rb', line 43 def host_query 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 query = Stay::BookingQuery.joins(:property).where(stay_properties: { user_id: current_devise_api_user&.id }).ongoing.order(created_at: :desc) @booking_queries = query.limit(cumulative_per_page).uniq total_count = query.count total_pages = (total_count.to_f / per_page).ceil return render json: { success: false, error: "Query not found" }, status: :not_found if @booking_queries.empty? render json: { success: true, booking_queries: ActiveModelSerializers::SerializableResource.new(@booking_queries, each_serializer: BookingQuerySerializer), 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: "Query 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 |
#index ⇒ Object
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/booking_queries_controller.rb', line 9 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 query = current_devise_api_user.booking_queries.ongoing @booking_queries = query.order(created_at: :desc).limit(cumulative_per_page) total_count = query.count total_pages = (total_count.to_f / per_page).ceil return render json: { success: false, error: "Query not found" }, status: :not_found if @booking_queries.empty? render json: { success: true, booking_queries: ActiveModelSerializers::SerializableResource.new(@booking_queries, each_serializer: BookingQuerySerializer), 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: "Query 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 |
#show ⇒ Object
99 100 101 102 103 104 105 106 107 |
# File 'app/controllers/stay/api/v1/booking_queries_controller.rb', line 99 def show last_five = @booking_query.chat&..order(created_at: :desc).limit(5) render json: { success: true, booking_query: BookingQuerySerializer.new(@booking_query), booking: @booking_query.booking ? BookingSerializer.new(@booking_query.booking) : nil, messages: ActiveModelSerializers::SerializableResource.new(last_five, each_serializer: MessageSerializer) }, status: :ok end |
#update ⇒ Object
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 |
# File 'app/controllers/stay/api/v1/booking_queries_controller.rb', line 110 def update if %w[request_change booking_invitation accepted rejected].include?(booking_query_params[:state]) @booking_query.update(state: booking_query_params[:state].to_sym) Stay::Chat::QueryMessagingService.new(@booking_query, current_devise_api_user).perform end unless @booking_query.update(booking_query_params) return render json: { success: false, errors: @booking_query.errors. }, status: :unprocessable_entity end last_five = @booking_query.chat&..order(created_at: :desc).limit(5) if @booking_query.accepted? if @booking_query.booking.present? render json: { success: false, message: "Booking already created for this query" }, status: :unprocessable_entity else result = Stay::Bookings::CreateBookingService.new(@booking_query).perform if result[:success] render json: { message: "Booking created successfully", success: true, booking_query: BookingQuerySerializer.new(@booking_query), booking: @booking_query.booking ? BookingSerializer.new(@booking_query.booking) : nil, messages: ActiveModelSerializers::SerializableResource.new(last_five, each_serializer: MessageSerializer) }, status: :ok else render json: { success: false, errors: result[:errors] }, status: :unprocessable_entity end end else render json: { success: true, booking_query: BookingQuerySerializer.new(@booking_query), booking: @booking_query.booking ? BookingSerializer.new(@booking_query.booking) : nil, messages: ActiveModelSerializers::SerializableResource.new(last_five, each_serializer: MessageSerializer) }, status: :ok, status: :ok end end |