-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feature/reservations-controller
- Loading branch information
Showing
20 changed files
with
834 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class API::V1::CarsController < ApplicationController | ||
before_action :set_car, only: %i[show update destroy] | ||
|
||
def show | ||
render json: { | ||
status: { | ||
code: 200, | ||
message: 'Car fetched successfully' | ||
}, | ||
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes] | ||
}, | ||
status: :ok | ||
end | ||
|
||
def create | ||
@car = Car.new(car_params) | ||
|
||
if @car.save | ||
render json: { | ||
status: { | ||
code: 200, | ||
message: 'Car successfully created' | ||
}, | ||
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes] | ||
}, | ||
status: :created | ||
else | ||
render json: @car.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @car.update(car_params) | ||
render json: { | ||
status: { | ||
code: 200, | ||
message: 'Car successfully updated' | ||
}, | ||
data: CarWithDetailsSerializer.new(@car).serializable_hash[:data][:attributes] | ||
}, | ||
status: :ok | ||
else | ||
render json: @car.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def destroy | ||
@car.destroy | ||
render json: { | ||
status: { | ||
code: 200, | ||
message: 'Car successfully deleted' | ||
} | ||
}, | ||
status: :ok | ||
end | ||
|
||
private | ||
|
||
def set_car | ||
@car = Car.includes(:car_detail).find(params[:id]) | ||
end | ||
|
||
def car_params | ||
params.require(:car).permit( | ||
:name, | ||
:description, | ||
car_detail_attributes: %i[ | ||
engine_type_id | ||
horsepower | ||
torque | ||
fuel_economy | ||
seating_capacity | ||
cargo_space | ||
infotainment_system | ||
safety_rating | ||
tech_features | ||
special_features | ||
] | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CarWithDetailsSerializer | ||
include JSONAPI::Serializer | ||
|
||
attributes :id, :name, :description | ||
|
||
attribute :car_detail do |car| | ||
CarDetailSerializer.new(car.car_detail).serializable_hash[:data][:attributes] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.