-
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 pull request #20 from fmanimashaun/feature/reservations-controller
Feature/reservations controller
- Loading branch information
Showing
5 changed files
with
74 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class API::V1::ReservationsController < ApplicationController | ||
before_action :set_reservation, only: %i[destroy show update] | ||
|
||
def index | ||
@data = Reservation.where(user_id:current_user.id) | ||
render json: { | ||
status: { code: 200, message: 'Reservations retrieved successfully.' }, | ||
reservations: @data.map do |reservation| | ||
ReservationSerializer.new(reservation).serializable_hash[:data][:attributes] | ||
end | ||
}, status: :ok | ||
end | ||
|
||
def destroy | ||
@reservation.destroy! | ||
render json: { | ||
status: { code: 200, message: 'Reservation deleted successfully.' } | ||
}, status: :ok | ||
end | ||
|
||
def show | ||
render json: { | ||
status: { code: 200, message: 'Reservation fetched successfully' }, | ||
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes] | ||
} | ||
end | ||
|
||
def create | ||
@reservation = Reservation.new(reservation_params) | ||
if @reservation.save! | ||
render json: { | ||
status: { code: 201, message: 'Reservation created successfully' }, | ||
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes] | ||
}, status: :created | ||
else | ||
render json: @reservation.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
def update | ||
if @reservation.update(reservation_params) | ||
render json: { | ||
status: { code: 200, message: 'Reservation created successfully' }, | ||
data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes] | ||
}, status: :ok | ||
else | ||
render json: @reservation.errors, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_reservation | ||
@reservation = Reservation.find params[:id] | ||
end | ||
|
||
def reservation_params | ||
params.require(:reservation).permit(:date, :city_id, :car_id, :user_id) | ||
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
class ReservationSerializer | ||
include JSONAPI::Serializer | ||
attributes :date | ||
attributes :id, :date, :city, :car, :user | ||
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
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,7 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe 'API::V1::Reservations', type: :request do | ||
describe 'GET /index' do | ||
pending "add some examples (or delete) #{__FILE__}" | ||
end | ||
end |