From 4a500b5714386aa64098582effba767c5369b437 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:43:50 +0430 Subject: [PATCH 01/12] Generate reservation controller --- app/controllers/api/v1/reservations_controller.rb | 4 ++++ spec/requests/api/v1/reservations_spec.rb | 7 +++++++ 2 files changed, 11 insertions(+) create mode 100644 app/controllers/api/v1/reservations_controller.rb create mode 100644 spec/requests/api/v1/reservations_spec.rb diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb new file mode 100644 index 0000000..43f979f --- /dev/null +++ b/app/controllers/api/v1/reservations_controller.rb @@ -0,0 +1,4 @@ +class API::V1::ReservationsController < ApplicationController + + +end diff --git a/spec/requests/api/v1/reservations_spec.rb b/spec/requests/api/v1/reservations_spec.rb new file mode 100644 index 0000000..266fc70 --- /dev/null +++ b/spec/requests/api/v1/reservations_spec.rb @@ -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 From 4a8e7135c8d8d359f1e568c0a9b67741abf675f2 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:44:21 +0430 Subject: [PATCH 02/12] Update routes to include the reservation api endpoints --- config/routes.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/config/routes.rb b/config/routes.rb index a5cfc5e..fba48f1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,7 @@ namespace :api do namespace :v1 do resources :cities + resources :reservations end end From afc38950d59df1f3bdb4aa458ebdaf72eb7f8cd9 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:45:46 +0430 Subject: [PATCH 03/12] Update the reservation_serializer.rb to include all the attributes of the Observation --- app/serializers/reservation_serializer.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/serializers/reservation_serializer.rb b/app/serializers/reservation_serializer.rb index 1c5330a..b9a2eab 100644 --- a/app/serializers/reservation_serializer.rb +++ b/app/serializers/reservation_serializer.rb @@ -1,4 +1,4 @@ class ReservationSerializer include JSONAPI::Serializer - attributes :date + attributes :id, :date, :city, :car, :user end From 0a0e693d0d5d83b1e875f81724a7ae5ac158fc7b Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:53:12 +0430 Subject: [PATCH 04/12] Update the seed file to generate initial data for Reservations --- db/seeds.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/db/seeds.rb b/db/seeds.rb index 1ed0ca9..77676d8 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,5 +1,5 @@ # create the admin user -User.find_or_create_by!(username: Rails.application.credentials.admin[:username]) do |user| +users = User.find_or_create_by!(username: Rails.application.credentials.admin[:username]) do |user| user.password = Rails.application.credentials.admin[:password] user.password_confirmation = Rails.application.credentials.admin[:password] user.email = Rails.application.credentials.admin[:email] @@ -37,3 +37,7 @@ # Create car models car_names = ['Toyota Camry', 'Honda Accord', 'Ford Mustang', 'Chevrolet Malibu', 'Hyundai Sonata', 'Nissan Altima', 'Volkswagen Passat', 'Subaru Legacy', 'Kia Optima', 'Audi A4', 'BMW 3 Series', 'Mercedes-Benz C-Class', 'Lexus ES', 'Volvo S60', 'Infiniti Q50', 'Acura TLX', 'Cadillac CT5', 'Lincoln MKZ', 'Alfa Romeo Giulia', 'Jaguar XE'] +car_names.each_with_index { |car_name, index| Car.find_or_create_by!(name: car_name, description: "Description #{index + 1}") } + +Reservation.create!(date: Date.tomorrow, car: Car.first, city: City.first, user: User.first) +Reservation.create!(date: Date.tomorrow, car:Car.last, city: City.last, user: User.last) \ No newline at end of file From 14da7b047788870e40a68c5a98e3502010494f8c Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:55:03 +0430 Subject: [PATCH 05/12] Create the private helper functions and call them befor action --- 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 43f979f..37eb5dc 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 + before_action :set_reservation, only: [:destroy, :show, :update] + 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 From d1271a87dd3152970f010446adb14ecd33c98ad3 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:55:45 +0430 Subject: [PATCH 06/12] Add index action to render all the reservations --- app/controllers/api/v1/reservations_controller.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 37eb5dc..3cf45fd 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -1,7 +1,17 @@ class API::V1::ReservationsController < ApplicationController before_action :set_reservation, only: [:destroy, :show, :update] - private + def index + @data = Reservation.all + 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 + + private def set_reservation @reservation = Reservation.find params[:id] end From 27736ad01b1c68a2328290ecbc719a23d2bd7c1a Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:56:33 +0430 Subject: [PATCH 07/12] Add destroy action to delete a reservation --- app/controllers/api/v1/reservations_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 3cf45fd..442419f 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -11,6 +11,13 @@ def index }, status: :ok end + def destroy + @reservation.destroy! + render json: { + status: { code: 200, message: "Reservation deleted successfully." }, + }, status: :ok + end + private def set_reservation @reservation = Reservation.find params[:id] From 1fe5888dd79a4255f8e60ae76fa27f1f44d23910 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:57:57 +0430 Subject: [PATCH 08/12] Add show action to render the details of one specific reservation --- app/controllers/api/v1/reservations_controller.rb | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 442419f..0e9ca93 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -18,6 +18,13 @@ def destroy }, status: :ok end + def show + render json: { + status: { code: 200, message: 'Reservation fetched successfully' }, + data: ReservationSerializer.new(@reservation).serializable_hash[:data][:attributes] + } + end + private def set_reservation @reservation = Reservation.find params[:id] From be976b80c7cfe83ee5771dcf7dd6922678deba14 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:58:28 +0430 Subject: [PATCH 09/12] Add create action to create a new reservation --- app/controllers/api/v1/reservations_controller.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 0e9ca93..1535cad 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -25,6 +25,18 @@ def show } 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 + private def set_reservation @reservation = Reservation.find params[:id] From 9f30e5308a0f8f89822a8d6291470a3c13920225 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 20:59:17 +0430 Subject: [PATCH 10/12] Add update action to update an existing reservation --- app/controllers/api/v1/reservations_controller.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index 1535cad..c3e82e9 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -37,6 +37,17 @@ def create 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] From 17523c53a8d39ee5bc5f80999c4c6f6e5e1b743c Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Fri, 16 Feb 2024 21:00:21 +0430 Subject: [PATCH 11/12] Fix the linters errors --- app/controllers/api/v1/reservations_controller.rb | 5 +++-- spec/requests/api/v1/reservations_spec.rb | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index c3e82e9..ce450f7 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 - before_action :set_reservation, only: [:destroy, :show, :update] + before_action :set_reservation, only: %i[destroy show update] def index @data = Reservation.all @@ -14,7 +14,7 @@ def index def destroy @reservation.destroy! render json: { - status: { code: 200, message: "Reservation deleted successfully." }, + status: { code: 200, message: 'Reservation deleted successfully.' } }, status: :ok end @@ -49,6 +49,7 @@ def update end private + def set_reservation @reservation = Reservation.find params[:id] end diff --git a/spec/requests/api/v1/reservations_spec.rb b/spec/requests/api/v1/reservations_spec.rb index 266fc70..37cee33 100644 --- a/spec/requests/api/v1/reservations_spec.rb +++ b/spec/requests/api/v1/reservations_spec.rb @@ -1,7 +1,7 @@ require 'rails_helper' -RSpec.describe "API::V1::Reservations", type: :request do - describe "GET /index" do +RSpec.describe 'API::V1::Reservations', type: :request do + describe 'GET /index' do pending "add some examples (or delete) #{__FILE__}" end end From 2b03605791e5d0b51d9581fa9360a6ce953a0cf4 Mon Sep 17 00:00:00 2001 From: Anwar Hussaini Date: Sat, 17 Feb 2024 08:23:28 +0430 Subject: [PATCH 12/12] Fix the type and revise the reservation query to only display the reservation of current user --- app/controllers/api/v1/reservations_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/api/v1/reservations_controller.rb b/app/controllers/api/v1/reservations_controller.rb index ce450f7..eb03a1f 100644 --- a/app/controllers/api/v1/reservations_controller.rb +++ b/app/controllers/api/v1/reservations_controller.rb @@ -2,7 +2,7 @@ class API::V1::ReservationsController < ApplicationController before_action :set_reservation, only: %i[destroy show update] def index - @data = Reservation.all + @data = Reservation.where(user_id:current_user.id) render json: { status: { code: 200, message: 'Reservations retrieved successfully.' }, reservations: @data.map do |reservation| @@ -55,6 +55,6 @@ def set_reservation end def reservation_params - params.require(:reservation).permit(:date, :city_id, :car_id, user_id) + params.require(:reservation).permit(:date, :city_id, :car_id, :user_id) end end