Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reservation with car and user details #32

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions app/controllers/api/v1/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
render json: {
status: { code: 200, message: 'Reservations fetched successfully.' },
data: @reservations.map do |reservation|
ReservationSerializer.new(reservation).serializable_hash[:data][:attributes]
ReservationWithDetailsSerializer.new(reservation).serializable_hash[:data][:attributes]
end
}, status: :ok
end
Expand All @@ -18,7 +18,7 @@ def index
def show
render json: {
status: { code: 200, message: 'Reservation fetched successfully.' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
data: ReservationWithDetailsSerializer.new(@reservation).serializable_hash[:data][:attributes]
}, status: :ok
end

Expand All @@ -29,7 +29,7 @@ def create
if @reservation.save
render json: {
status: { code: 201, message: 'Reservation created successfully.' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
data: ReservationWithDetailsSerializer.new(@reservation).serializable_hash[:data][:attributes]
}, status: :created
else
render json: @reservation.errors, status: :unprocessable_entity
Expand All @@ -41,7 +41,7 @@ def update
if @reservation.update(reservation_params)
render json: {
status: { code: 200, message: 'Reservation updated successfully.' },
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes]
data: ReservationWithDetailsSerializer.new(@reservation).serializable_hash[:data][:attributes]
}, status: :ok
else
render json: @reservation.errors, status: :unprocessable_entity
Expand Down
13 changes: 13 additions & 0 deletions app/serializers/reservation_with_details_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class ReservationWithDetailsSerializer
include JSONAPI::Serializer

attributes :id, :date

attribute :user do |reservation|
UserSerializer.new(reservation.user).serializable_hash[:data][:attributes]
end

attribute :car do |reservation|
CarWithDetailsSerializer.new(reservation.car).serializable_hash[:data][:attributes]
end
end
Loading