@@ -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
0 commit comments