Skip to content

Commit ae04512

Browse files
committed
Refactor controllers, routing, models for airlines, airports, routes.
1 parent 8c07d88 commit ae04512

File tree

7 files changed

+355
-141
lines changed

7 files changed

+355
-141
lines changed

app/controllers/airlines_controller.rb

Lines changed: 0 additions & 76 deletions
This file was deleted.

app/controllers/airports_controller.rb

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
module V1
5+
class AirlinesController < ApplicationController
6+
skip_before_action :verify_authenticity_token, only: %i[create update destroy]
7+
before_action :set_airline, only: %i[show update destroy]
8+
9+
# GET /api/v1/airlines/{id}
10+
def show
11+
if @airline
12+
render json: @airline, status: :ok
13+
else
14+
render json: { error: "Airline with ID #{params[:id]} not found" }, status: :not_found
15+
end
16+
rescue Couchbase::Error::DocumentNotFound => e
17+
render json: { error: "Airline with ID #{params[:id]} not found", message: e.message }, status: :not_found
18+
rescue StandardError => e
19+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
20+
end
21+
22+
# POST /api/v1/airlines/{id}
23+
def create
24+
@airline = Airline.create(airline_params)
25+
if @airline
26+
render json: @airline, status: :created
27+
else
28+
render json: { message: 'Airline already exists' }, status: :conflict
29+
end
30+
rescue ArgumentError => e
31+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
32+
rescue Couchbase::Error::DocumentExists => e
33+
render json: { error: "Airline with ID #{params[:id]} already exists", message: e.message }, status: :conflict
34+
rescue StandardError => e
35+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
36+
end
37+
38+
# PUT /api/v1/airlines/{id}
39+
def update
40+
@airline = Airline.new(airline_params).update(airline_params)
41+
render json: @airline, status: :ok
42+
rescue ArgumentError => e
43+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
44+
rescue StandardError => e
45+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
46+
end
47+
48+
# DELETE /api/v1/airlines/{id}
49+
def destroy
50+
if @airline
51+
if @airline.destroy(params[:id])
52+
render json: { message: 'Airline deleted successfully' }, status: :accepted
53+
else
54+
render json: { message: 'Failed to delete airline' }, status: :bad_request
55+
end
56+
else
57+
render json: { error: "Airline with ID #{params[:id]} not found" }, status: :not_found
58+
end
59+
rescue Couchbase::Error::DocumentNotFound => e
60+
render json: { error: "Airline with ID #{params[:id]} not found", message: e.message }, status: :not_found
61+
rescue StandardError => e
62+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
63+
end
64+
65+
# GET /api/v1/airlines/list
66+
def index
67+
airlines = Airline.all
68+
airlines.each do |foo|
69+
puts foo
70+
end
71+
formatted_airlines = airlines.map do |airline|
72+
{
73+
id: airline.id,
74+
name: airline.name,
75+
iata: airline.iata,
76+
icao: airline.icao,
77+
callsign: airline.callsign,
78+
country: airline.country
79+
}
80+
end
81+
render json: formatted_airlines
82+
rescue StandardError => e
83+
render json: { error: e.message }, status: :internal_server_error
84+
end
85+
86+
private
87+
88+
def set_airline
89+
@airline = Airline.find(params[:id])
90+
rescue Couchbase::Error::DocumentNotFound
91+
@airline = nil
92+
end
93+
94+
def airline_params
95+
params.permit(:id, :name, :iata, :icao, :callsign, :country)
96+
end
97+
end
98+
end
99+
end
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# frozen_string_literal: true
2+
3+
module Api
4+
module V1
5+
class AirportsController < ApplicationController
6+
skip_before_action :verify_authenticity_token, only: %i[create update destroy]
7+
before_action :set_airport, only: %i[show update destroy]
8+
9+
# GET /api/v1/airports/{id}
10+
def show
11+
if @airport
12+
render json: @airport, status: :ok
13+
else
14+
render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found
15+
end
16+
rescue Couchbase::Error::DocumentNotFound => e
17+
render json: { error: "Airport with ID #{params[:id]} not found", message: e.message }, status: :not_found
18+
rescue StandardError => e
19+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
20+
end
21+
22+
# POST /api/v1/airports
23+
def create
24+
@airport = Airport.create(airport_params)
25+
if @airport
26+
render json: @airport, status: :created
27+
else
28+
render json: { message: 'Airport already exists' }, status: :conflict
29+
end
30+
rescue ArgumentError => e
31+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
32+
rescue Couchbase::Error::DocumentExists => e
33+
render json: { error: 'Airport already exists', message: e.message }, status: :conflict
34+
rescue StandardError => e
35+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
36+
end
37+
38+
# PUT /api/v1/airports/{id}
39+
def update
40+
if @airport
41+
@airport.update(airport_params)
42+
render json: @airport, status: :ok
43+
else
44+
render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found
45+
end
46+
rescue ArgumentError => e
47+
render json: { error: 'Invalid request', message: e.message }, status: :bad_request
48+
rescue StandardError => e
49+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
50+
end
51+
52+
# DELETE /api/v1/airports/{id}
53+
def destroy
54+
if @airport
55+
@airport.destroy
56+
render json: { message: 'Airport deleted successfully' }, status: :accepted
57+
else
58+
render json: { error: "Airport with ID #{params[:id]} not found" }, status: :not_found
59+
end
60+
rescue Couchbase::Error::DocumentNotFound => e
61+
render json: { error: "Airport with ID #{params[:id]} not found", message: e.message }, status: :not_found
62+
rescue StandardError => e
63+
render json: { error: 'Internal server error', message: e.message }, status: :internal_server_error
64+
end
65+
66+
# GET /api/v1/airports/list
67+
# def index
68+
# airports = Airport.all
69+
# formatted_airports = airports.map do |airport|
70+
# {
71+
# id: airport.id,
72+
# airportname: airport.airportname,
73+
# city: airport.city,
74+
# country: airport.country,
75+
# faa: airport.faa,
76+
# icao: airport.icao,
77+
# tz: airport.tz,
78+
# geo: {
79+
# lat: airport.geo&.lat,
80+
# lon: airport.geo&.lon,
81+
# alt: airport.geo&.alt
82+
# }
83+
# }
84+
# end
85+
# render json: formatted_airports
86+
# end
87+
88+
private
89+
90+
def set_airport
91+
@airport = Airport.find(params[:id])
92+
rescue Couchbase::Error::DocumentNotFound
93+
@airport = nil
94+
end
95+
96+
def airport_params
97+
params.require(:airport).permit(:id, :airportname, :city, :country, :faa, :icao, :tz, geo: %i[lat lon alt])
98+
end
99+
end
100+
end
101+
end

0 commit comments

Comments
 (0)