Skip to content

Commit 0db719e

Browse files
committed
feat: Implement pagination for airline listing by country
1 parent 37698a7 commit 0db719e

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

app/controllers/api/v1/airlines_controller.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,14 @@ def destroy
7777
# GET /api/v1/airlines/list
7878
def index
7979
country = params[:country]
80-
offset = params[:offset].to_i || 0
81-
limit = params[:limit].to_i || 10
80+
limit = params[:limit] || 10
81+
offset = params[:offset] || 0
8282

8383
begin
8484
airlines = if country.present?
85-
Airline.list_by_country(key: [country])
85+
Airline.list_by_country(key: [country, limit, offset])
8686
else
87+
# TODO: Implement pagination
8788
Airline.all
8889
end
8990

@@ -111,10 +112,11 @@ def to_airport
111112
raise ArgumentError, 'Destination airport is missing' unless params[:destinationAirportCode].present?
112113

113114
destination_airport = params[:destinationAirportCode]
114-
offset = params[:offset].to_i || 0
115-
limit = params[:limit].to_i || 10
115+
limit = params[:limit] || 10
116+
offset = params[:offset] || 0
116117

117-
airlines = Airline.to_airport(key: [destination_airport])
118+
puts "Destination airport: #{destination_airport}, limit: #{limit}, offset: #{offset}"
119+
airlines = Airline.to_airport(key: [destination_airport, limit, offset])
118120
.pluck(:callsign, :country, :iata, :icao, :name)
119121

120122
formatted_airlines = airlines.map do |airline|

app/models/airline.rb

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ class Airline < CouchbaseOrm::Base
1818
validates :country, presence: true
1919

2020
# Custom N1QL query to list airlines by country with pagination
21-
n1ql :list_by_country, emit_key: [: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 10 OFFSET 0", options)
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)
2323
}
2424

2525
n1ql :to_airport, query_fn: proc { |bucket, values, options|
26-
# limit = options[:limit] || 10
27-
# offset = options[:offset] || 0
28-
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id WHERE route.destinationairport = #{quote(values[0])}) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId LIMIT 10 OFFSET 0", options)
29-
}
26+
cluster.query("SELECT raw META(air).id FROM (SELECT DISTINCT META(airline).id AS airlineId FROM `#{bucket.name}` AS route JOIN `#{bucket.name}` AS airline ON route.airlineid = META(airline).id WHERE route.destinationairport = #{quote(values[0])}) AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId LIMIT #{values[1]} OFFSET #{values[2]}", options)
27+
}
3028
end

0 commit comments

Comments
 (0)