Skip to content

Commit 58f1356

Browse files
committed
feat: Add N1QL query to find hotels by name
1 parent 6299601 commit 58f1356

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

app/controllers/api/v1/hotels_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,15 @@ def create_and_update
198198
render json: { error: e.message }, status: :internal_server_error
199199
end
200200

201+
# GET /hotels/find_by_name_n1ql
202+
def find_hotels_by_name_n1ql
203+
puts "hello"
204+
hotels = Hotel.find_by_name_n1ql('Windy Harbour Farm Hotel')
205+
render json: hotels.map { |hotel| hotel.attributes.except('id') }, status: :ok
206+
rescue StandardError => e
207+
render json: { error: e.message }, status: :internal_server_error
208+
end
209+
201210
private
202211

203212
def set_hotel

app/models/hotel.rb

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Hotel < CouchbaseOrm::Base
2828
before_destroy :do_something_before_destroy
2929
after_destroy :do_something_after_destroy
3030

31+
before_save :encrypt_address
32+
3133
private
3234

3335
def do_something_before_create
@@ -64,7 +66,7 @@ def do_something_after_destroy
6466

6567
attribute :title, :string
6668
attribute :name, :string
67-
attribute :address, :string
69+
attribute :address, :encrypted
6870
attribute :directions, :string
6971
attribute :phone, :string
7072
attribute :tollfree, :string
@@ -99,19 +101,35 @@ def do_something_after_destroy
99101
validates :url, format: { with: URI::DEFAULT_PARSER.make_regexp, message: 'must be a valid URL' }
100102
validates :title, :name, :address, :city, :state, :country, length: { maximum: 255 }
101103
# validates :title, :name, uniqueness: true
102-
validates :price, numericality: { greater_than_or_equal_to: 0 }
104+
# validates :price, numericality: { greater_than_or_equal_to: 0 } # its a string
103105
validates :vacancy, inclusion: { in: [true, false] }
104106
validates :public_likes, exclusion: { in: [nil] }
105107

106108
validate :custom_validation
107109

110+
n1ql :find_by_name_n1ql, 'SELECT * FROM hotels WHERE name = $1'
111+
108112
def custom_validation
109-
errors.add(:title, 'cannot be funny')
113+
if title.include?('funny')
114+
errors.add(:title, 'cannot be funny')
115+
end
110116
end
111117

112118
def set_timestamps
113119
current_time = Time.now
114120
self.created_at = current_time if new_record?
115121
self.updated_at = current_time
116122
end
123+
124+
def encrypt_address
125+
self.address = encrypt(self.address) if address_changed?
126+
end
127+
128+
def encrypt(data)
129+
# Implement your encryption logic here
130+
# For example, using a simple XOR encryption
131+
key = "secret_key"
132+
encrypted_data = data.chars.map { |c| (c.ord ^ key.ord).chr }.join
133+
encrypted_data
134+
end
117135
end

config/routes.rb

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
# post 'hotels/:id', to: 'hotels#create'
3434
# put 'hotels/:id', to: 'hotels#update'
3535
# 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'
41-
get '/hotels/create_and_update', to: 'hotels#create_and_update'
36+
# get 'hotels/find_by_id', to: 'hotels#find_hotel_by_id'
37+
# get 'hotels/find_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'
41+
# get '/hotels/create_and_update', to: 'hotels#create_and_update'
42+
43+
# n1ql
44+
get '/hotels/find_by_name_n1ql', to: 'hotels#find_hotels_by_name_n1ql'
4245
end
4346
end
4447
end

0 commit comments

Comments
 (0)