Skip to content

Commit

Permalink
Merge branch 'dev' into api-documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hmunish committed Oct 31, 2023
2 parents 58b8b3e + c9a2653 commit 5e381c8
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
4 changes: 2 additions & 2 deletions app/controllers/api/v1/authentication_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ class AuthenticationController < ApplicationController
def login
@user = User.find_by(username: params[:username])
if @user
payload = { user_id: @user.id }
payload = { user_id: @user.id, username: @user.username }
ENV['SECRET_KEY_BASE'] || Rails.application.secrets.secret_key_base
token = create_token(payload)
render json: {
username: @user.username,
token:
}
else
render json: { message: 'Could not find user' }
render json: { message: 'Could not find user' }, status: :forbidden
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/api/v1/cars_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Api::V1::CarsController < ApplicationController
before_action :set_car, only: %i[show update destroy]
skip_before_action :authenticate, only: %i[index show create destroy]
before_action :authenticate, only: %i[index show create destroy]

def index
@cars = Car.all
Expand Down
43 changes: 43 additions & 0 deletions app/controllers/api/v1/reservations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Api::V1::ReservationsController < ApplicationController
before_action :authenticate, only: %i[index create show update destroy]
before_action :set_reservation, only: %i[show update destroy]

def index
current_user = User.find(params[:user_id])
@reservations = current_user.reservations.order(created_at: :desc).all
if @reservations
render json: { status: { code: 200, message: 'Reservations retrieved successfully.', data: @reservations } }, status: :ok
else
render json: { error: 'Reservations not found.' }, status: :not_found
end
end

def show
render json: @reservation
end

def create
current_user = User.find(params[:user_id])
@reservation = current_user.reservations.new(reservation_params)
if @reservation.save
render json: @reservation, status: :created
else
render json: @reservation.errors, status: :unprocessable_entity
end
end

def destroy
@reservation.destroy
head :no_content
end

private

def set_reservation
@reservation = Reservation.find(params[:id])
end

def reservation_params
params.permit(:location, :date, :car_id)
end
end
2 changes: 1 addition & 1 deletion app/controllers/api/v1/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Api::V1::UsersController < ApplicationController
before_action :authenticate
skip_before_action :authenticate, only: [:create]
before_action :set_user, only: %i[show update destroy]

def index
Expand Down
18 changes: 9 additions & 9 deletions config/initializers/cors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@

# Read more: https://github.com/cyu/rack-cors

# Rails.application.config.middleware.insert_before 0, Rack::Cors do
# allow do
# origins "example.com"
#
# resource "*",
# headers: :any,
# methods: [:get, :post, :put, :patch, :delete, :options, :head]
# end
# end
Rails.application.config.middleware.insert_before 0, Rack::Cors do
allow do
origins "*"

resource "*",
headers: :any,
methods: [:get, :post, :put, :patch, :delete, :options, :head]
end
end

0 comments on commit 5e381c8

Please sign in to comment.