Skip to content

Commit ba80433

Browse files
committed
Refactor controllers to limit the number of records returned in the index action of airlines_controller.rb and airports_controller.rb
1 parent fbb8575 commit ba80433

File tree

4 files changed

+106
-7
lines changed

4 files changed

+106
-7
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# or operating system, you probably want to add a global ignore instead:
55
# git config --global core.excludesfile '~/.gitignore_global'
66

7+
8+
.vscode/
9+
.idea/
10+
711
Gemfile.lock
812

913
# Ignore bundler config.

app/controllers/api/v1/airlines_controller.rb

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,71 @@ def destroy
7676

7777
# GET /api/v1/airlines/list
7878
def index
79-
airlines = Airline.all.offset(0).limit(10)
80-
render json: airlines.map { |airline| airline.attributes.except('id') }, status: :ok
79+
country = params[:country]
80+
page = params[:page].to_i || 1
81+
per_page = params[:per_page].to_i || 10
82+
83+
offset = (page - 1) * per_page
84+
85+
airlines = if country.present?
86+
Airline.where(country:)
87+
else
88+
Airline.all
89+
end
90+
.pluck(:callsign, :country, :iata, :icao, :id, :name)
91+
# .order(:name)
92+
# .offset(offset)
93+
# .limit(per_page)
94+
95+
formatted_airlines = airlines.map do |airline|
96+
{
97+
callsign: airline[0],
98+
country: airline[1],
99+
iata: airline[2],
100+
icao: airline[3],
101+
id: airline[4],
102+
name: airline[5]
103+
}
104+
end
105+
106+
render json: formatted_airlines
107+
end
108+
109+
# GET /api/v1/airlines/to-airport
110+
def to_airport
111+
raise ArgumentError, 'Destination airport is missing' unless params[:destination_airport].present?
112+
113+
destination_airport = params[:destination_airport]
114+
page = params[:page].to_i || 1
115+
per_page = params[:per_page].to_i || 10
116+
117+
offset = (page - 1) * per_page
118+
119+
airline_ids = Route.where(destinationairport: destination_airport)
120+
# .distinct(:airlineid)
121+
.pluck(:airlineid)
122+
123+
airlines = Airline.where(id: airline_ids)
124+
.pluck(:callsign, :country, :iata, :icao, :id, :name)
125+
# .offset(offset)
126+
# .limit(per_page)
127+
128+
formatted_airlines = airlines.map do |airline|
129+
{
130+
callsign: airline[0],
131+
country: airline[1],
132+
iata: airline[2],
133+
icao: airline[3],
134+
id: airline[4],
135+
name: airline[5]
136+
}
137+
end
138+
139+
render json: formatted_airlines
140+
rescue ArgumentError => e
141+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
81142
rescue StandardError => e
82-
render json: { error: e.message }, status: :internal_server_error
143+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
83144
end
84145

85146
private

app/controllers/api/v1/airports_controller.rb

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,37 @@ def destroy
7575

7676
# GET /api/v1/airports/list
7777
def index
78-
airports = Airport.pluck(:id, :airportname, :city, :country, :faa, :icao, :tz, :geo)
79-
render json: airports
80-
rescue StandardError => e
81-
render json: { error: e.message }, status: :internal_server_error
78+
country = params[:country]
79+
page = params[:page].to_i || 1
80+
per_page = params[:per_page].to_i || 10
81+
82+
offset = (page - 1) * per_page
83+
84+
begin
85+
airports = if country.present?
86+
Airport.where(country:)
87+
else
88+
Airport.all
89+
end
90+
.pluck(:airportname, :city, :country, :faa, :geo, :icao, :id, :tz)
91+
92+
formatted_airports = airports.map do |airport|
93+
{
94+
airportname: airport[0],
95+
city: airport[1],
96+
country: airport[2],
97+
faa: airport[3],
98+
geo: airport[4],
99+
icao: airport[5],
100+
id: airport[6],
101+
tz: airport[7]
102+
}
103+
end
104+
105+
render json: formatted_airports
106+
rescue StandardError => e
107+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
108+
end
82109
end
83110

84111
private

app/models/airline.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,11 @@ class Airline < CouchbaseOrm::Base
1212
validates :iata, presence: true, length: { is: 2 }
1313
validates :icao, presence: true, length: { is: 3 }
1414
validates :country, presence: true
15+
16+
# Custom N1QL query to list airlines by country with pagination
17+
n1ql :by_country, emit_key: :country, query_fn: proc { |bucket, values, options|
18+
offset = options.delete(:offset) || 0
19+
limit = options.delete(:limit) || 10
20+
cluster.query("SELECT * FROM `#{bucket.name}` WHERE type = 'airline' AND country = #{quote(values[0])} ORDER BY name ASC LIMIT #{limit} OFFSET #{offset}", options)
21+
}
1522
end

0 commit comments

Comments
 (0)