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