Skip to content

Commit 04ad63e

Browse files
committed
Refactored list method in all controllers
1 parent 2c5aca4 commit 04ad63e

File tree

3 files changed

+4
-31
lines changed

3 files changed

+4
-31
lines changed

app/controllers/api/v1/airlines_controller.rb

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,7 @@ def destroy
7777
# GET /api/v1/airlines/list
7878
def index
7979
airlines = Airline.all
80-
airlines.each do |foo|
81-
puts foo
82-
end
83-
formatted_airlines = airlines.map do |airline|
84-
{
85-
id: airline.id,
86-
name: airline.name,
87-
iata: airline.iata,
88-
icao: airline.icao,
89-
callsign: airline.callsign,
90-
country: airline.country
91-
}
92-
end
93-
render json: formatted_airlines
80+
render json: airlines.map { |airline| airline.attributes.except('id') }, status: :ok
9481
rescue StandardError => e
9582
render json: { error: e.message }, status: :internal_server_error
9683
end

app/controllers/api/v1/airports_controller.rb

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,7 @@ def destroy
7676
# GET /api/v1/airports/list
7777
def index
7878
airports = Airport.all
79-
formatted_airports = airports.map do |airport|
80-
{
81-
id: airport.id,
82-
airportname: airport.airportname,
83-
city: airport.city,
84-
country: airport.country,
85-
faa: airport.faa,
86-
icao: airport.icao,
87-
tz: airport.tz,
88-
geo: {
89-
lat: airport.geo&.lat,
90-
lon: airport.geo&.lon,
91-
alt: airport.geo&.alt
92-
}
93-
}
94-
end
95-
render json: formatted_airports
79+
render json: airports.map { |airport| airport.attributes.except('id') }, status: :ok
9680
rescue StandardError => e
9781
render json: { error: e.message }, status: :internal_server_error
9882
end

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@
1313
delete 'airlines/:id', to: 'airlines#destroy'
1414

1515
# Airports resource routes
16+
get 'airports/list', to: 'airports#index'
1617
get 'airports/direct-connections', to: 'airports#direct_connections'
1718
get 'airports/:id', to: 'airports#show'
1819
post 'airports/:id', to: 'airports#create'
1920
put 'airports/:id', to: 'airports#update'
2021
delete 'airports/:id', to: 'airports#destroy'
2122

2223
# Routes resource routes
24+
get 'routes/list', to: 'routes#index'
2325
get 'routes/:id', to: 'routes#show'
2426
post 'routes/:id', to: 'routes#create'
2527
put 'routes/:id', to: 'routes#update'

0 commit comments

Comments
 (0)