@@ -9,39 +9,50 @@ class AirportsController < ApplicationController
99 # GET /api/v1/airports/{id}
1010 def show
1111 if @airport
12- render json : @airport , status : :ok
12+ render json : @airport . attributes . except ( 'id' ) , status : :ok
1313 else
14- render json : { error : "Airport with ID #{ params [ :id ] } not found" } , status : :not_found
14+ render json : { message : "Airport with ID #{ params [ :id ] } not found" } , status : :not_found
1515 end
1616 rescue Couchbase ::Error ::DocumentNotFound => e
17- render json : { error : "Airport with ID #{ params [ :id ] } not found" , message : e . message } , status : :not_found
17+ render json : { error : "Airport not found" , message : e . message } , status : :not_found
1818 rescue StandardError => e
1919 render json : { error : 'Internal server error' , message : e . message } , status : :internal_server_error
2020 end
2121
22- # POST /api/v1/airports
22+ # POST /api/v1/airports/{id}
2323 def create
24- @airport = Airport . create ( airport_params )
24+ @airport = Airport . find_by ( id : params [ :id ] )
2525 if @airport
26- render json : @airport , status : :created
26+ render json : { message : "Airport with ID #{ params [ :id ] } already exists" } , status : :conflict
2727 else
28- render json : { message : 'Airport already exists' } , status : :conflict
28+ @airport = Airport . create ( airport_params )
29+ if @airport
30+ render json : @airport , status : :created
31+ else
32+ render json : { error : 'Failed to create airport' } , status : :bad_request
33+ end
2934 end
3035 rescue ArgumentError => e
3136 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
3437 rescue StandardError => e
3538 render json : { error : 'Internal server error' , message : e . message } , status : :internal_server_error
3639 end
3740
38- # PUT /api/v1/airports/{id}
41+ # PATCH/ PUT /api/v1/airports/{id}
3942 def update
4043 if @airport
41- @airport . update ( airport_params )
42- render json : @airport , status : :ok
44+ if @airport . update ( airport_params )
45+ render json : @airport . attributes . except ( 'id' ) , status : :ok
46+ else
47+ render json : { message : 'Failed to update airport' } , status : :bad_request
48+ end
4349 else
44- render json : { error : "Airport with ID #{ params [ :id ] } not found" } , status : :not_found
50+ @airport = Airport . create ( airport_params )
51+ if @airport
52+ render json : @airport . attributes . except ( 'id' ) , status : :ok
53+ else
54+ render json : { message : 'Airport already exists' } , status : :conflict
55+ end
4556 end
4657 rescue ArgumentError => e
4758 render json : { error : 'Invalid request' , message : e . message } , status : :bad_request
@@ -51,50 +62,32 @@ def update
5162
5263 # DELETE /api/v1/airports/{id}
5364 def destroy
65+ @airport = Airport . find_by ( id : params [ :id ] )
5466 if @airport
55- @airport . destroy
56- render json : { message : 'Airport deleted successfully' } , status : :accepted
67+ if @airport . destroy
68+ render json : { message : 'Airport deleted successfully' } , status : :accepted
69+ else
70+ render json : { message : 'Failed to delete airport' } , status : :bad_request
71+ end
5772 else
58- render json : { error : "Airport with ID #{ params [ :id ] } not found" } , status : :not_found
73+ render json : { message : "Airport with ID #{ params [ :id ] } not found" } , status : :not_found
5974 end
6075 rescue Couchbase ::Error ::DocumentNotFound => e
61- render json : { error : "Airport with ID #{ params [ :id ] } not found" , message : e . message } , status : :not_found
76+ render json : { error : "Airport not found" , message : e . message } , status : :not_found
6277 rescue StandardError => e
6378 render json : { error : 'Internal server error' , message : e . message } , status : :internal_server_error
6479 end
6580
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-
8881 private
8982
9083 def set_airport
91- @airport = Airport . find ( params [ :id ] )
84+ @airport = Airport . find_by ( id : params [ :id ] )
9285 rescue Couchbase ::Error ::DocumentNotFound
9386 @airport = nil
9487 end
9588
9689 def airport_params
97- params . require ( :airport ) . permit ( :id , :airportname , :city , :country , :faa , :icao , :tz , geo : %i[ lat lon alt ] )
90+ params . require ( :airport ) . permit ( :id , :airportname , :city , :country , :faa , :icao , :tz , geo : [ : lat, : lon, : alt] )
9891 end
9992 end
10093 end
0 commit comments