Skip to content

Commit ef17098

Browse files
committed
Refactor routes.rb to add routes for hotels
1 parent ba80433 commit ef17098

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# app/controllers/api/v1/hotels_controller.rb
2+
module Api
3+
module V1
4+
class HotelsController < ApplicationController
5+
skip_before_action :verify_authenticity_token, only: %i[create update destroy]
6+
before_action :set_hotel, only: %i[show update destroy]
7+
8+
# GET /api/v1/hotels/{id}
9+
def show
10+
if @hotel
11+
render json: @hotel.attributes.except('id'), status: :ok
12+
else
13+
render json: { message: "Hotel with ID #{params[:id]} not found" }, status: :not_found
14+
end
15+
rescue Couchbase::Error::DocumentNotFound => e
16+
render json: { error: 'Hotel not found', message: e.message }, status: :not_found
17+
rescue StandardError => e
18+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
19+
end
20+
21+
# POST /api/v1/hotels/{id}
22+
def create
23+
@hotel = Hotel.find_by(id: params[:id])
24+
if @hotel
25+
render json: { message: "Hotel with ID #{params[:id]} already exists" }, status: :conflict
26+
else
27+
@hotel = Hotel.new(hotel_params.merge(id: params[:id]))
28+
if @hotel.save
29+
render json: @hotel, status: :created
30+
else
31+
render json: { error: 'Failed to create hotel', message: @hotel.errors.full_messages },
32+
status: :bad_request
33+
end
34+
end
35+
rescue ArgumentError => e
36+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
37+
rescue StandardError => e
38+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
39+
end
40+
41+
# PUT /api/v1/hotels/{id}
42+
def update
43+
@hotel = Hotel.new(hotel_params.merge(id: params[:id]))
44+
if @hotel.save
45+
render json: @hotel.attributes.except('id'), status: :ok
46+
else
47+
render json: { error: 'Invalid request', message: @hotel.errors.full_messages },
48+
status: :bad_request
49+
end
50+
rescue ArgumentError => e
51+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
52+
rescue StandardError => e
53+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
54+
end
55+
56+
# DELETE /api/v1/hotels/{id}
57+
def destroy
58+
@hotel = Hotel.find_by(id: params[:id])
59+
if @hotel
60+
if @hotel.destroy
61+
render json: { message: 'Hotel deleted successfully' }, status: :accepted
62+
else
63+
render json: { error: 'Failed to delete hotel', message: @hotel.errors.full_messages },
64+
status: :bad_request
65+
end
66+
else
67+
render json: { message: "Hotel with ID #{params[:id]} not found" }, status: :not_found
68+
end
69+
rescue Couchbase::Error::DocumentNotFound => e
70+
render json: { error: 'Hotel not found', message: e.message }, status: :not_found
71+
rescue StandardError => e
72+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
73+
end
74+
75+
private
76+
77+
def set_hotel
78+
@hotel = Hotel.find_by(id: params[:id])
79+
rescue Couchbase::Error::DocumentNotFound
80+
@hotel = nil
81+
end
82+
83+
def hotel_params
84+
params.require(:hotel).permit(:id, :title, :name, :address, :directions, :phone, :tollfree, :email, :fax,
85+
:url, :checkin, :checkout, :price, :type, :country, :city, :state,
86+
:vacancy, :description, :alias, :pets_ok, :free_breakfast, :free_internet,
87+
:free_parking, :created_at, :updated_at,
88+
geo: %i[lat lon accuracy],
89+
reviews: [:content, { ratings: {} }, :author, :date],
90+
public_likes: [])
91+
end
92+
end
93+
end
94+
end

app/models/hotel.rb

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# frozen_string_literal: true
2+
3+
# app/models/hotel.rb
4+
class Hotel < CouchbaseOrm::Base
5+
attribute :title, :string
6+
attribute :name, :string
7+
attribute :address, :string
8+
attribute :directions, :string
9+
attribute :phone, :string
10+
attribute :tollfree, :string
11+
attribute :email, :string
12+
attribute :fax, :string
13+
attribute :url, :string
14+
attribute :checkin, :string
15+
attribute :checkout, :string
16+
attribute :price, :string
17+
attribute :geo, :nested, type: GeoCoordinates
18+
attribute :type, :string
19+
attribute :country, :string
20+
attribute :city, :string
21+
attribute :state, :string
22+
attribute :reviews, :array, type: Review
23+
attribute :public_likes, :array, type: :string
24+
attribute :vacancy, :boolean
25+
attribute :description, :string
26+
attribute :alias, :string
27+
attribute :pets_ok, :boolean
28+
attribute :free_breakfast, :boolean
29+
attribute :free_internet, :boolean
30+
attribute :free_parking, :boolean
31+
attribute :created_at, :datetime, precision: 6
32+
attribute :updated_at, :datetime, precision: 6
33+
34+
before_save :set_timestamps
35+
36+
private
37+
38+
def set_timestamps
39+
current_time = Time.now
40+
self.created_at = current_time if new_record?
41+
self.updated_at = current_time
42+
end
43+
end
44+
45+
# app/models/geo_coordinates.rb
46+
class GeoCoordinates < CouchbaseOrm::NestedDocument
47+
attribute :lat, :float
48+
attribute :lon, :float
49+
attribute :accuracy, :string
50+
end
51+
52+
# app/models/review.rb
53+
class Review < CouchbaseOrm::NestedDocument
54+
attribute :content, :string
55+
attribute :ratings, :hash
56+
attribute :author, :string
57+
attribute :date, :datetime
58+
end

config/routes.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@
2626
post 'routes/:id', to: 'routes#create'
2727
put 'routes/:id', to: 'routes#update'
2828
delete 'routes/:id', to: 'routes#destroy'
29+
30+
# 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+
2937
end
3038
end
3139
end

0 commit comments

Comments
 (0)