Skip to content

Commit e5af406

Browse files
committed
chore: Refactor Hotel model and controllers, and update documentation
1 parent b1280ff commit e5af406

File tree

2 files changed

+84
-24
lines changed

2 files changed

+84
-24
lines changed

app/controllers/api/v1/hotel_controller.rb renamed to app/controllers/api/v1/hotels_controller.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,16 @@ def destroy
7272
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
7373
end
7474

75+
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
83+
end
84+
7585
def create_with_validations
7686
@hotel = Hotel.new(hotel_params)
7787
if @hotel.save

app/models/hotel.rb

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,67 @@
1+
class GeoCoordinates < CouchbaseOrm::NestedDocument
2+
attribute :lat, :float
3+
attribute :lon, :float
4+
attribute :accuracy, :string
5+
6+
validates :lat, presence: true, numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90 }
7+
validates :lon, presence: true, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180 }
8+
validates :accuracy, presence: true
9+
end
10+
11+
class Review < CouchbaseOrm::NestedDocument
12+
attribute :author, :string
13+
attribute :content, :string
14+
attribute :ratings, :integer
15+
attribute :date, :datetime, precision: 6
16+
17+
validates :author, :content, :rating, presence: true
18+
validates :ratings, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 5 }
19+
end
20+
121
class Hotel < CouchbaseOrm::Base
22+
before_create :do_something_before_create
23+
after_create :do_something_after_create
24+
before_save :do_something_before_save
25+
after_save :do_something_after_save
26+
before_update :do_something_before_update
27+
after_update :do_something_after_update
28+
before_destroy :do_something_before_destroy
29+
after_destroy :do_something_after_destroy
30+
31+
private
32+
33+
def do_something_before_create
34+
puts "Before create: #{self}"
35+
end
36+
37+
def do_something_after_create
38+
puts "After create: #{self}"
39+
end
40+
41+
def do_something_before_save
42+
puts "Before save: #{self}"
43+
end
44+
45+
def do_something_after_save
46+
puts "After save: #{self}"
47+
end
48+
49+
def do_something_before_update
50+
puts "Before update: #{self}"
51+
end
52+
53+
def do_something_after_update
54+
puts "After update: #{self}"
55+
end
56+
57+
def do_something_before_destroy
58+
puts "Before destroy: #{self}"
59+
end
60+
61+
def do_something_after_destroy
62+
puts "After destroy: #{self}"
63+
end
64+
265
attribute :title, :string
366
attribute :name, :string
467
attribute :address, :string
@@ -28,34 +91,21 @@ class Hotel < CouchbaseOrm::Base
2891
attribute :created_at, :datetime, precision: 6
2992
attribute :updated_at, :datetime, precision: 6
3093

31-
validates :name, presence: true, uniqueness: true
32-
validates :address, presence: true
33-
validates :phone, presence: true
34-
validates :type, inclusion: { in: ['hotel', 'motel', 'resort'] }
35-
validates :url, format: { with: URI.regexp, message: 'must be a valid URL' }
36-
validates :description, length: { maximum: 500 }
37-
38-
before_create :set_alias
39-
before_save :set_timestamps
40-
after_create :send_welcome_email
41-
before_destroy :check_reviews
42-
43-
private
94+
# Validations
95+
validates :title, :name, :address, :phone, :email, :fax, :url, presence: true
96+
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP, message: 'must be a valid email address' }
97+
validates :phone, format: { with: /\A\d{10}\z/, message: 'must be a valid 10-digit phone number' }
98+
validates :fax, format: { with: /\A\d{10}\z/, message: 'must be a valid 10-digit fax number' }
99+
validates :url, format: { with: URI::DEFAULT_PARSER.make_regexp, message: 'must be a valid URL' }
100+
validates :title, :name, :address, :city, :state, :country, length: { maximum: 255 }
101+
# validates :title, :name, uniqueness: true
102+
validates :price, numericality: { greater_than_or_equal_to: 0 }
103+
validates :vacancy, inclusion: { in: [true, false] }
104+
validates :public_likes, exclusion: { in: [nil] }
44105

45106
def set_timestamps
46107
current_time = Time.now
47108
self.created_at = current_time if new_record?
48109
self.updated_at = current_time
49110
end
50-
51-
def send_welcome_email
52-
# Code to send welcome email after hotel creation
53-
end
54-
55-
def check_reviews
56-
if reviews.any?
57-
errors.add(:base, 'Cannot delete hotel with existing reviews')
58-
throw :abort
59-
end
60-
end
61111
end

0 commit comments

Comments
 (0)