Skip to content

Commit 8c07d88

Browse files
committed
Add routes for airports in routes.rb
1 parent f461e6b commit 8c07d88

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class AirportsController < ApplicationController
2+
def index
3+
airports = Airport.all
4+
formatted_airports = airports.map do |airport|
5+
{
6+
id: airport.id,
7+
airportname: airport.airportname,
8+
city: airport.city,
9+
country: airport.country,
10+
faa: airport.faa,
11+
icao: airport.icao,
12+
tz: airport.tz,
13+
geo: {
14+
lat: airport.geo&.lat,
15+
lon: airport.geo&.lon,
16+
alt: airport.geo&.alt
17+
}
18+
}
19+
end
20+
render json: formatted_airports
21+
end
22+
23+
def show
24+
airport = Airport.find(params[:id])
25+
render json: airport
26+
rescue Couchbase::Error::DocumentNotFound
27+
render json: { error: 'Airport not found' }, status: :not_found
28+
end
29+
30+
def create
31+
airport = Airport.new(airport_params)
32+
if airport.save
33+
render json: airport, status: :created
34+
else
35+
render json: { errors: airport.errors.full_messages }, status: :unprocessable_entity
36+
end
37+
end
38+
39+
def update
40+
airport = Airport.find(params[:id])
41+
if airport.update(airport_params)
42+
render json: airport
43+
else
44+
render json: { errors: airport.errors.full_messages }, status: :unprocessable_entity
45+
end
46+
rescue Couchbase::Error::DocumentNotFound
47+
render json: { error: 'Airport not found' }, status: :not_found
48+
end
49+
50+
def destroy
51+
airport = Airport.find(params[:id])
52+
airport.destroy
53+
head :no_content
54+
rescue Couchbase::Error::DocumentNotFound
55+
render json: { error: 'Airport not found' }, status: :not_found
56+
end
57+
58+
private
59+
60+
def airport_params
61+
params.require(:airport).permit(:airportname, :city, :country, :faa, :icao, :tz, geo: [:lat, :lon, :alt])
62+
end
63+
end

app/models/airport.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class GeoCoordinates < CouchbaseOrm::NestedDocument
2+
attribute :lat, :float
3+
attribute :lon, :float
4+
attribute :alt, :integer
5+
6+
validates :lat, presence: true, numericality: { greater_than_or_equal_to: -90, less_than_or_equal_to: 90 }
7+
validates :lon, presence: true, numericality: { greater_than_or_equal_to: -180, less_than_or_equal_to: 180 }
8+
validates :alt, presence: true, numericality: { greater_than_or_equal_to: -1000, less_than_or_equal_to: 10_000 }
9+
end
10+
11+
class Airport < CouchbaseOrm::Base
12+
attribute :airportname, :string
13+
attribute :city, :string
14+
attribute :country, :string
15+
attribute :faa, :string
16+
attribute :icao, :string
17+
attribute :tz, :string
18+
attribute :geo, :nested, type: GeoCoordinates
19+
20+
validates :airportname, presence: true
21+
validates :city, presence: true
22+
validates :country, presence: true
23+
validates :faa, presence: true, length: { is: 3 }
24+
validates :icao, presence: true, length: { is: 4 }
25+
validates :tz, presence: true
26+
validates :geo, presence: true, nested: true
27+
end

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
Rails.application.routes.draw do
22
resources :airlines
3+
resources :airports
34
end

0 commit comments

Comments
 (0)