Skip to content

Commit 4b7e73d

Browse files
committed
feat: Update Airline controller to use offset and limit parameters
1 parent 186094a commit 4b7e73d

File tree

2 files changed

+12
-41
lines changed

2 files changed

+12
-41
lines changed

app/controllers/api/v1/airlines_controller.rb

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,8 @@ def destroy
7777
# GET /api/v1/airlines/list
7878
def index
7979
country = params[:country]
80-
page = params[:page].to_i || 1
81-
per_page = params[:per_page].to_i || 10
82-
offset = (page - 1) * per_page
80+
offset = params[:offset].to_i || 0
81+
limit = params[:limit].to_i || 10
8382

8483
begin
8584
airlines = if country.present?
@@ -112,22 +111,11 @@ def to_airport
112111
raise ArgumentError, 'Destination airport is missing' unless params[:destinationAirportCode].present?
113112

114113
destination_airport = params[:destinationAirportCode]
115-
page = params[:page].to_i || 1
116-
per_page = params[:per_page].to_i || 10
114+
offset = params[:offset].to_i || 0
115+
limit = params[:limit].to_i || 10
117116

118-
offset = (page - 1) * per_page
119-
120-
# airline_ids = Route.where(destinationairport: destination_airport)
121-
# # .distinct(:airlineid)
122-
# .pluck(:airlineid)
123-
124-
# airlines = Airline.where(id: airline_ids)
125-
# .pluck(:callsign, :country, :iata, :icao, :id, :name)
126-
127-
airlines = Airline.to_airport(destination_airport, limit: per_page, offset:)
128-
129-
# .offset(offset)
130-
# .limit(per_page)
117+
airlines = Airline.to_airport(key: [destination_airport])
118+
.pluck(:callsign, :country, :iata, :icao, :name)
131119

132120
formatted_airlines = airlines.map do |airline|
133121
{

app/models/airline.rb

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

1314
validates :name, presence: true
1415
validates :callsign, presence: true
@@ -18,30 +19,12 @@ class Airline < CouchbaseOrm::Base
1819

1920
# Custom N1QL query to list airlines by country with pagination
2021
n1ql :list_by_country, emit_key: [:country], query_fn: proc { |bucket, values, options|
21-
# limit = options[:limit] || 10
22-
# offset = options[:offset] || 0
2322
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` WHERE type = 'airline' AND country = #{quote(values[0])} LIMIT 10 OFFSET 0", options)
2423
}
25-
# n1ql :by_country,
26-
# "SELECT * FROM `#{bucket.name}` WHERE type = 'airline' AND country = $1 ORDER BY name ASC LIMIT $2 OFFSET $3"
2724

28-
# SELECT
29-
# air.callsign,
30-
# air.country,
31-
# air.iata,
32-
# air.icao,
33-
# air.id,
34-
# air.name,
35-
# air.type
36-
# FROM (
37-
# SELECT DISTINCT META(airline).id AS airlineId
38-
# FROM route
39-
# JOIN airline ON route.airlineid = META(airline).id
40-
# WHERE route.destinationairport = ?
41-
# ) AS subquery
42-
# JOIN airline AS air ON META(air).id = subquery.airlineId;
43-
44-
n1ql :to_airport, emit_key: :icao, query_fn: proc { |bucket, values, options|
45-
cluster.query("SELECT raw meta().id FROM `#{bucket.name}` WHERE type = 'airline' AND icao = #{quote(values[0])} LIMIT 10 OFFSET 0", options)
46-
}
25+
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 = 'JFK') AS subquery JOIN `#{bucket.name}` AS air ON META(air).id = subquery.airlineId LIMIT 10 OFFSET 0", options)
29+
}
4730
end

0 commit comments

Comments
 (0)