Skip to content

Commit 6299601

Browse files
committed
feat: Add create_and_update endpoint for hotels
1 parent 8f7b557 commit 6299601

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

app/controllers/api/v1/hotels_controller.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ def create_with_validations
8787
else
8888
render json: { errors: @hotel.errors.full_messages }, status: :unprocessable_entity
8989
end
90+
rescue StandardError => e
91+
render json: { error: e.message }, status: :internal_server_error
9092
end
9193

9294
def update_with_validations
@@ -95,6 +97,8 @@ def update_with_validations
9597
else
9698
render json: { errors: @hotel.errors.full_messages }, status: :unprocessable_entity
9799
end
100+
rescue StandardError => e
101+
render json: { error: e.message }, status: :internal_server_error
98102
end
99103

100104
def destroy_with_callback
@@ -103,18 +107,24 @@ def destroy_with_callback
103107
else
104108
render json: { errors: @hotel.errors.full_messages }, status: :unprocessable_entity
105109
end
110+
rescue StandardError => e
111+
render json: { error: e.message }, status: :internal_server_error
106112
end
107113

108114
# GET /hotels/:id
109115
def find_hotel_by_id
110116
hotel = Hotel.find('hotel_id_123')
111117
render json: hotel
118+
rescue StandardError => e
119+
render json: { error: e.message }, status: :internal_server_error
112120
end
113121

114122
# GET /hotels/find_by_name
115123
def find_hotel_by_name
116124
hotel = Hotel.find_by(name: 'Windy Harbour Farm Hotel')
117125
render json: hotel
126+
rescue StandardError => e
127+
render json: { error: e.message }, status: :internal_server_error
118128
end
119129

120130
# GET /hotels/active_hotels
@@ -123,12 +133,16 @@ def active_hotels
123133
formatted_hotels = active_hotels.map { |hotel| hotel.attributes.except('id') }
124134
# render just their names and total count
125135
render json: { hotels: formatted_hotels.map { |hotel| hotel['name'] }, total: formatted_hotels.count }
136+
rescue StandardError => e
137+
render json: { error: e.message }, status: :internal_server_error
126138
end
127139

128140
# GET /hotels/find_by_name_and_price
129141
def find_hotels_by_name_and_price
130142
hotels = Hotel.where("LOWER(name) LIKE '%hostel%' AND price IS NOT NULL").where(vacancy: true)
131143
render json: { hotels: hotels.map { |hotel| hotel.attributes.except('id') }, total: hotels.count }
144+
rescue StandardError => e
145+
render json: { error: e.message }, status: :internal_server_error
132146
end
133147

134148
# GET /hotels/find_by_email_domain
@@ -137,6 +151,51 @@ def find_hotels_by_email_domain
137151
formatted_hotels = hotels.map { |hotel| hotel.attributes.except('id') }
138152
render json: formatted_hotels, status: :ok
139153
# render json: { hotels: hotels.map { |hotel| hotel.attributes.except('id') }, total: hotels.count }
154+
rescue StandardError => e
155+
render json: { error: e.message }, status: :internal_server_error
156+
end
157+
158+
# GET /hotels/create_and_update
159+
def create_and_update
160+
# Create a new hotel document
161+
hotel = Hotel.new(
162+
title: 'Glossop',
163+
name: 'Windy Harbour Farm Hotel',
164+
address: 'Woodhead Road',
165+
phone: '+44 1457 853107',
166+
url: 'http://www.peakdistrict-hotel.co.uk/',
167+
geo: { lat: 53.46327, lon: -1.943125, accuracy: 'ROOFTOP' },
168+
type: 'hotel',
169+
country: 'United Kingdom',
170+
city: 'Padfield',
171+
vacancy: false,
172+
description: 'Woodhead Rd, Glossop',
173+
free_internet: true,
174+
free_breakfast: false,
175+
free_parking: false
176+
)
177+
178+
# Save the hotel document
179+
hotel.save
180+
181+
# Increment the price by 100
182+
hotel.increment(:price, 100)
183+
184+
# Decrement the price by 50
185+
hotel.decrement(:price, 50)
186+
187+
# Append additional information to the description
188+
hotel.append(:description, ', additional information')
189+
190+
# Prepend additional description to the existing description
191+
hotel.prepend(:description, 'Additional description, ')
192+
193+
# Update the expiration time
194+
hotel.touch
195+
196+
render json: hotel
197+
rescue StandardError => e
198+
render json: { error: e.message }, status: :internal_server_error
140199
end
141200

142201
private

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
get 'hotels/active_hotels', to: 'hotels#active_hotels'
3939
get 'hotels/find_by_name_and_price', to: 'hotels#find_hotels_by_name_and_price'
4040
get 'hotels/find_by_email_domain', to: 'hotels#find_hotels_by_email_domain'
41+
get '/hotels/create_and_update', to: 'hotels#create_and_update'
4142
end
4243
end
4344
end

0 commit comments

Comments
 (0)