Skip to content

Commit 2c5aca4

Browse files
committed
Refactor controllers to use merge method for setting the ID in airline, airport, and route models
1 parent 511757c commit 2c5aca4

File tree

6 files changed

+6
-12
lines changed

6 files changed

+6
-12
lines changed

app/controllers/api/v1/airlines_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
if @airline
2626
render json: { message: "Airline with ID #{params[:id]} already exists" }, status: :conflict
2727
else
28-
@airline = Airline.new(airline_params)
28+
@airline = Airline.new(airline_params.merge(id: params[:id]))
2929
if @airline.save
3030
render json: @airline, status: :created
3131
else
@@ -41,7 +41,7 @@ def create
4141

4242
# PUT /api/v1/airlines/{id}
4343
def update
44-
@airline = Airline.new(airline_params)
44+
@airline = Airline.new(airline_params.merge(id: params[:id]))
4545
if @airline.save
4646
render json: @airline.attributes.except('id'), status: :ok
4747
else

app/controllers/api/v1/airports_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
if @airport
2626
render json: { message: "Airport with ID #{params[:id]} already exists" }, status: :conflict
2727
else
28-
@airport = Airport.new(airport_params)
28+
@airport = Airport.new(airport_params.merge(id: params[:id]))
2929
if @airport.save
3030
render json: @airport, status: :created
3131
else
@@ -41,7 +41,7 @@ def create
4141

4242
# PUT /api/v1/airports/{id}
4343
def update
44-
@airport = Airport.new(airport_params)
44+
@airport = Airport.new(airport_params.merge(id: params[:id]))
4545
if @airport.save
4646
render json: @airport.attributes.except('id'), status: :ok
4747
else

app/controllers/api/v1/routes_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
if @route
2626
render json: { message: "Route with ID #{params[:id]} already exists" }, status: :conflict
2727
else
28-
@route = Route.new(route_params)
28+
@route = Route.new(route_params.merge(id: params[:id]))
2929
if @route.save
3030
render json: @route, status: :created
3131
else
@@ -41,7 +41,7 @@ def create
4141

4242
# PUT /api/v1/routes/{id}
4343
def update
44-
@route = Route.new(route_params)
44+
@route = Route.new(route_params.merge(id: params[:id]))
4545
if @route.save
4646
render json: @route.attributes.except('id'), status: :ok
4747
else

test/integration/airlines_spec.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
let(:airline_id) { 'airline_post' }
2929
let(:airline_params) do
3030
{
31-
'id' => airline_id, # 'id' is not a required field in the airline_params hash
3231
'name' => '40-Mile Air',
3332
'iata' => 'Q5',
3433
'icao' => 'MLA',
@@ -67,7 +66,6 @@
6766

6867
let(:current_params) do
6968
{
70-
'id' => airline_id, # 'id' is not a required field in the airline_params hash
7169
'name' => '40-Mile Air',
7270
'iata' => 'U5',
7371
'icao' => 'UPD',
@@ -77,7 +75,6 @@
7775
end
7876
let(:updated_params) do
7977
{
80-
'id' => airline_id,
8178
'name' => '41-Mile Air',
8279
'iata' => 'U6',
8380
'icao' => 'UPE',
@@ -128,7 +125,6 @@
128125
let(:airline_id) { 'airline_delete' }
129126
let(:airline_params) do
130127
{
131-
'id' => airline_id, # 'id' is not a required field in the airline_params hash
132128
'name' => '40-Mile Air',
133129
'iata' => 'Q5',
134130
'icao' => 'MLA',

test/integration/airports_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@
157157
let(:airport_id) { 'airport_delete' }
158158
let(:airport_params) do
159159
{
160-
'id' => airport_id, # 'id' should be 'airport_delete
161160
'airportname' => 'Test Airport',
162161
'city' => 'Test City',
163162
'country' => 'Test Country',

test/integration/routes_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@
139139
let(:route_id) { 'route_delete' }
140140
let(:route_params) do
141141
{
142-
'id' => route_id,
143142
'airline' => 'AF',
144143
'airlineid' => 'airline_137',
145144
'sourceairport' => 'TLV',

0 commit comments

Comments
 (0)