Skip to content

Commit 2ce00c1

Browse files
committed
feat: Refactor Airline controller to use offset and limit parameters
1 parent 0db719e commit 2ce00c1

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

app/controllers/api/v1/airlines_controller.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,7 @@ def index
8181
offset = params[:offset] || 0
8282

8383
begin
84-
airlines = if country.present?
85-
Airline.list_by_country(key: [country, limit, offset])
86-
else
87-
# TODO: Implement pagination
88-
Airline.all
89-
end
84+
airlines = Airline.list_by_country_or_all(key: [country, limit, offset])
9085

9186
airlines = airlines.pluck(:callsign, :country, :iata, :icao, :name)
9287
formatted_airlines = airlines.map do |airline|
@@ -115,7 +110,6 @@ def to_airport
115110
limit = params[:limit] || 10
116111
offset = params[:offset] || 0
117112

118-
puts "Destination airport: #{destination_airport}, limit: #{limit}, offset: #{offset}"
119113
airlines = Airline.to_airport(key: [destination_airport, limit, offset])
120114
.pluck(:callsign, :country, :iata, :icao, :name)
121115

app/models/airline.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class Airline < CouchbaseOrm::Base
99
attribute :iata, :string
1010
attribute :icao, :string
1111
attribute :country, :string
12-
# attribute :destinationAirportCode, :string
1312

1413
validates :name, presence: true
1514
validates :callsign, presence: true
@@ -18,8 +17,15 @@ class Airline < CouchbaseOrm::Base
1817
validates :country, presence: true
1918

2019
# Custom N1QL query to list airlines by country with pagination
21-
n1ql :list_by_country, query_fn: proc { |bucket, values, options|
22-
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` WHERE type = 'airline' AND country = #{quote(values[0])} LIMIT #{values[1]} OFFSET #{values[2]}", options)
20+
n1ql :list_by_country_or_all, query_fn: proc { |bucket, values, options|
21+
if values[0].present?
22+
country = quote(values[0])
23+
query = "SELECT raw meta().id FROM `#{bucket.name}` WHERE type = 'airline' AND country = #{country}"
24+
else
25+
query = "SELECT raw meta().id FROM `#{bucket.name}` WHERE type = 'airline'"
26+
end
27+
query += " LIMIT #{values[1]} OFFSET #{values[2]}"
28+
cluster.query(query, options)
2329
}
2430

2531
n1ql :to_airport, query_fn: proc { |bucket, values, options|

0 commit comments

Comments
 (0)