Skip to content

Commit 8f7b557

Browse files
committed
feat: Add new hotel search endpoints and improve error handling
1 parent ecbde54 commit 8f7b557

File tree

3 files changed

+51
-16
lines changed

3 files changed

+51
-16
lines changed

app/controllers/api/v1/hotels_controller.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,11 @@ def destroy
7373
end
7474

7575
def index
76-
begin
77-
@hotels = Hotel.all
78-
formatted_hotels = @hotels.map { |hotel| hotel.attributes.except('id') }
79-
render json: formatted_hotels, status: :ok
80-
rescue StandardError => e
81-
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
82-
end
76+
@hotels = Hotel.all
77+
formatted_hotels = @hotels.map { |hotel| hotel.attributes.except('id') }
78+
render json: formatted_hotels, status: :ok
79+
rescue StandardError => e
80+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
8381
end
8482

8583
def create_with_validations
@@ -107,6 +105,40 @@ def destroy_with_callback
107105
end
108106
end
109107

108+
# GET /hotels/:id
109+
def find_hotel_by_id
110+
hotel = Hotel.find('hotel_id_123')
111+
render json: hotel
112+
end
113+
114+
# GET /hotels/find_by_name
115+
def find_hotel_by_name
116+
hotel = Hotel.find_by(name: 'Windy Harbour Farm Hotel')
117+
render json: hotel
118+
end
119+
120+
# GET /hotels/active_hotels
121+
def active_hotels
122+
active_hotels = Hotel.where(vacancy: true)
123+
formatted_hotels = active_hotels.map { |hotel| hotel.attributes.except('id') }
124+
# render just their names and total count
125+
render json: { hotels: formatted_hotels.map { |hotel| hotel['name'] }, total: formatted_hotels.count }
126+
end
127+
128+
# GET /hotels/find_by_name_and_price
129+
def find_hotels_by_name_and_price
130+
hotels = Hotel.where("LOWER(name) LIKE '%hostel%' AND price IS NOT NULL").where(vacancy: true)
131+
render json: { hotels: hotels.map { |hotel| hotel.attributes.except('id') }, total: hotels.count }
132+
end
133+
134+
# GET /hotels/find_by_email_domain
135+
def find_hotels_by_email_domain
136+
hotels = Hotel.where(email: /co.uk/)
137+
formatted_hotels = hotels.map { |hotel| hotel.attributes.except('id') }
138+
render json: formatted_hotels, status: :ok
139+
# render json: { hotels: hotels.map { |hotel| hotel.attributes.except('id') }, total: hotels.count }
140+
end
141+
110142
private
111143

112144
def set_hotel

app/models/hotel.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,9 @@ def do_something_after_destroy
104104
validates :public_likes, exclusion: { in: [nil] }
105105

106106
validate :custom_validation
107-
def custom_validation
108-
return unless email.include?('rhossilibunkhouse.com')
109107

110-
errors.add(:email, 'cannot be from rhossilibunkhouse.com')
108+
def custom_validation
109+
errors.add(:title, 'cannot be funny')
111110
end
112111

113112
def set_timestamps

config/routes.rb

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,16 @@
2828
delete 'routes/:id', to: 'routes#destroy'
2929

3030
# Hotel
31-
get 'hotels/list', to: 'hotels#index'
32-
get 'hotels/:id', to: 'hotels#show'
33-
post 'hotels/:id', to: 'hotels#create'
34-
put 'hotels/:id', to: 'hotels#update'
35-
delete 'hotels/:id', to: 'hotels#destroy'
36-
31+
# get 'hotels/list', to: 'hotels#index'
32+
# get 'hotels/:id', to: 'hotels#show'
33+
# post 'hotels/:id', to: 'hotels#create'
34+
# put 'hotels/:id', to: 'hotels#update'
35+
# delete 'hotels/:id', to: 'hotels#destroy'
36+
get 'hotels/find_hotel_by_id', to: 'hotels#find_hotel_by_id'
37+
get 'hotels/find_hotel_by_name', to: 'hotels#find_hotel_by_name'
38+
get 'hotels/active_hotels', to: 'hotels#active_hotels'
39+
get 'hotels/find_by_name_and_price', to: 'hotels#find_hotels_by_name_and_price'
40+
get 'hotels/find_by_email_domain', to: 'hotels#find_hotels_by_email_domain'
3741
end
3842
end
3943
end

0 commit comments

Comments
 (0)