|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +module Api |
| 4 | + module V1 |
| 5 | + class AirportsController < ApplicationController |
| 6 | + skip_before_action :verify_authenticity_token, only: %i[create update destroy] |
| 7 | + before_action :set_airport, only: %i[show update destroy] |
| 8 | + |
| 9 | + # GET /api/v1/airports/{id} |
| 10 | + def show |
| 11 | + if @airport |
| 12 | + render json: @airport, status: :ok |
| 13 | + else |
| 14 | + render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found |
| 15 | + end |
| 16 | + rescue Couchbase::Error::DocumentNotFound => e |
| 17 | + render json: { error: "Airport with ID #{params[:id]} not found", message: e.message }, status: :not_found |
| 18 | + rescue StandardError => e |
| 19 | + render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error |
| 20 | + end |
| 21 | + |
| 22 | + # POST /api/v1/airports |
| 23 | + def create |
| 24 | + @airport = Airport.create(airport_params) |
| 25 | + if @airport |
| 26 | + render json: @airport, status: :created |
| 27 | + else |
| 28 | + render json: { message: 'Airport already exists' }, status: :conflict |
| 29 | + end |
| 30 | + rescue ArgumentError => e |
| 31 | + render json: { error: 'Invalid request', message: e.message }, status: :bad_request |
| 32 | + rescue Couchbase::Error::DocumentExists => e |
| 33 | + render json: { error: 'Airport already exists', message: e.message }, status: :conflict |
| 34 | + rescue StandardError => e |
| 35 | + render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error |
| 36 | + end |
| 37 | + |
| 38 | + # PUT /api/v1/airports/{id} |
| 39 | + def update |
| 40 | + if @airport |
| 41 | + @airport.update(airport_params) |
| 42 | + render json: @airport, status: :ok |
| 43 | + else |
| 44 | + render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found |
| 45 | + end |
| 46 | + rescue ArgumentError => e |
| 47 | + render json: { error: 'Invalid request', message: e.message }, status: :bad_request |
| 48 | + rescue StandardError => e |
| 49 | + render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error |
| 50 | + end |
| 51 | + |
| 52 | + # DELETE /api/v1/airports/{id} |
| 53 | + def destroy |
| 54 | + if @airport |
| 55 | + @airport.destroy |
| 56 | + render json: { message: 'Airport deleted successfully' }, status: :accepted |
| 57 | + else |
| 58 | + render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found |
| 59 | + end |
| 60 | + rescue Couchbase::Error::DocumentNotFound => e |
| 61 | + render json: { error: "Airport with ID #{params[:id]} not found", message: e.message }, status: :not_found |
| 62 | + rescue StandardError => e |
| 63 | + render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error |
| 64 | + end |
| 65 | + |
| 66 | + # GET /api/v1/airports/list |
| 67 | + # def index |
| 68 | + # airports = Airport.all |
| 69 | + # formatted_airports = airports.map do |airport| |
| 70 | + # { |
| 71 | + # id: airport.id, |
| 72 | + # airportname: airport.airportname, |
| 73 | + # city: airport.city, |
| 74 | + # country: airport.country, |
| 75 | + # faa: airport.faa, |
| 76 | + # icao: airport.icao, |
| 77 | + # tz: airport.tz, |
| 78 | + # geo: { |
| 79 | + # lat: airport.geo&.lat, |
| 80 | + # lon: airport.geo&.lon, |
| 81 | + # alt: airport.geo&.alt |
| 82 | + # } |
| 83 | + # } |
| 84 | + # end |
| 85 | + # render json: formatted_airports |
| 86 | + # end |
| 87 | + |
| 88 | + private |
| 89 | + |
| 90 | + def set_airport |
| 91 | + @airport = Airport.find(params[:id]) |
| 92 | + rescue Couchbase::Error::DocumentNotFound |
| 93 | + @airport = nil |
| 94 | + end |
| 95 | + |
| 96 | + def airport_params |
| 97 | + params.require(:airport).permit(:id, :airportname, :city, :country, :faa, :icao, :tz, geo: %i[lat lon alt]) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
0 commit comments