From 7293a0615bd4c9a8e5c825d0c321c4f228f2bb60 Mon Sep 17 00:00:00 2001 From: Abdoul Date: Mon, 30 Oct 2023 08:46:43 +0000 Subject: [PATCH 01/15] genrate reservations controller --- app/controllers/api/v1/reservations_controller.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 4341e61..665ff40 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -1,2 +1,4 @@ class Api::V1::ReservationsController < ApplicationController + skip_before_action :authenticate, only: %i[index create show update destroy] + before_action :set_reservation, only: %i[show update destroy] end From 2bbccb8c245ab87833f5382348c0b9ef653ebd92 Mon Sep 17 00:00:00 2001 From: Abdoul Date: Mon, 30 Oct 2023 12:32:16 +0000 Subject: [PATCH 02/15] fixing issue relates to get users --- app/controllers/api/v1/authentication_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/authentication_controller.rb b/app/controllers/api/v1/authentication_controller.rb index 046f941..08086df 100644 --- a/app/controllers/api/v1/authentication_controller.rb +++ b/app/controllers/api/v1/authentication_controller.rb @@ -6,7 +6,7 @@ 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: { From e7641aafecf54ae244e551e2d6026b7a4f029fce Mon Sep 17 00:00:00 2001 From: Abdoul Date: Mon, 30 Oct 2023 12:34:06 +0000 Subject: [PATCH 03/15] adding config cors --- config/initializers/cors.rb | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/config/initializers/cors.rb b/config/initializers/cors.rb index e5a82f1..38fdfd8 100644 --- a/config/initializers/cors.rb +++ b/config/initializers/cors.rb @@ -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 From 938714fe96319c50553e74623d4c296cdd11ea8f Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:25:34 +0430 Subject: [PATCH 04/15] Add index method in reservations_controller --- app/controllers/api/v1/reservations_controller.rb | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 665ff40..0ea3ea0 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -1,4 +1,12 @@ class Api::V1::ReservationsController < ApplicationController skip_before_action :authenticate, only: %i[index create show update destroy] before_action :set_reservation, only: %i[show update destroy] + def index + @reservations = Reservation.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 end From bfb8187f1638bc7fe83a3eedf0c1417bfe690e66 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:26:21 +0430 Subject: [PATCH 05/15] Add the show method for reservations_controller --- app/controllers/api/v1/reservations_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 0ea3ea0..f255c68 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -9,4 +9,7 @@ def index render json: { error: 'Reservations not found.' }, status: :not_found end end + def show + render json: @reservation + end end From 504d8755ba772ebd84759a01bf777f8f1aec5983 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:27:19 +0430 Subject: [PATCH 06/15] Add the create method in reservations_controller --- app/controllers/api/v1/reservations_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index f255c68..45a8fe9 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -12,4 +12,13 @@ def index 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 end From cfa2c677684a7f803b14148645c9a28260f0e6e3 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:27:58 +0430 Subject: [PATCH 07/15] Add the destroy method in reservations_controller --- app/controllers/api/v1/reservations_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 45a8fe9..f774a6b 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -21,4 +21,8 @@ def create render json: @reservation.errors, status: :unprocessable_entity end end + def destroy + @reservation.destroy + head :no_content + end end From 51557803e58b0fa6a6dcba5a6b18bb99ded8ee81 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:29:18 +0430 Subject: [PATCH 08/15] Add the set_reservation method --- app/controllers/api/v1/reservations_controller.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index f774a6b..0a65ba6 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -25,4 +25,9 @@ def destroy @reservation.destroy head :no_content end + private + + def set_reservation + @reservation = Reservation.find(params[:id]) + end end From c7ff9111f6dc8bd37a07a5228474d05a46e3a6a8 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:29:41 +0430 Subject: [PATCH 09/15] Update reservations_controller.rb --- app/controllers/api/v1/reservations_controller.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 0a65ba6..ef5854a 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -30,4 +30,7 @@ def destroy def set_reservation @reservation = Reservation.find(params[:id]) end + def reservation_params + params.permit(:location, :date, :car_id) + end end From d58d51ec70510b562c8b361f0f8bef52d08ba5cf Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 21:33:47 +0430 Subject: [PATCH 10/15] Fix the rubocop errors --- app/controllers/api/v1/reservations_controller.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index ef5854a..0a47444 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -9,27 +9,32 @@ def index 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 + render json: @reservation, status: :created else - render json: @reservation.errors, status: :unprocessable_entity + 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 From f36ed41f0dbdaa6306d9bbb2fc12816aa33a63ce Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 22:06:58 +0430 Subject: [PATCH 11/15] Update reservations_controller.rb --- app/controllers/api/v1/reservations_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 0a47444..0222378 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -1,5 +1,5 @@ class Api::V1::ReservationsController < ApplicationController - skip_before_action :authenticate, only: %i[index create show update destroy] + before_action :authenticate, only: %i[index create show update destroy] before_action :set_reservation, only: %i[show update destroy] def index @reservations = Reservation.order(created_at: :desc).all From 6afbf103edcadcbdef7e35bff3b4f3e2c286d2b8 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 22:12:47 +0430 Subject: [PATCH 12/15] Update users_controller.rb --- app/controllers/api/v1/users_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 3734ed0..d721541 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -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 From 36e457e2cda9e85e7da990adcfe664aebdd6e16e Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 22:17:11 +0430 Subject: [PATCH 13/15] Update cars_controller.rb --- app/controllers/api/v1/cars_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/cars_controller.rb b/app/controllers/api/v1/cars_controller.rb index 4f2237e..2c21d67 100644 --- a/app/controllers/api/v1/cars_controller.rb +++ b/app/controllers/api/v1/cars_controller.rb @@ -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 From ffe0bb51f2977ab955543b211ed533267874b8d0 Mon Sep 17 00:00:00 2001 From: Faranosh Amini Date: Mon, 30 Oct 2023 22:17:16 +0430 Subject: [PATCH 14/15] Update authentication_controller.rb --- app/controllers/api/v1/authentication_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/authentication_controller.rb b/app/controllers/api/v1/authentication_controller.rb index 08086df..63a9c18 100644 --- a/app/controllers/api/v1/authentication_controller.rb +++ b/app/controllers/api/v1/authentication_controller.rb @@ -14,7 +14,7 @@ def login token: } else - render json: { message: 'Could not find user' } + render json: { message: 'Could not find user' }, status: :forbidden end end From b3c04a495b2c42fed737646f27a2d0a66cce269e Mon Sep 17 00:00:00 2001 From: Abdoul Date: Tue, 31 Oct 2023 06:29:01 +0000 Subject: [PATCH 15/15] making changes required by the reviewer --- app/controllers/api/v1/reservations_controller.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 0222378..661f993 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -1,8 +1,10 @@ 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 - @reservations = Reservation.order(created_at: :desc).all + 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