From 7ecbc7d95c731411da5c8cf0f9ad67290e4e8ad7 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Thu, 29 Feb 2024 13:51:56 +0200 Subject: [PATCH 1/4] Define new action for place creation --- app/controllers/api/v1/places_controller.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/controllers/api/v1/places_controller.rb b/app/controllers/api/v1/places_controller.rb index 25cfabd..7f1d53c 100644 --- a/app/controllers/api/v1/places_controller.rb +++ b/app/controllers/api/v1/places_controller.rb @@ -12,6 +12,11 @@ def show render json: @place end + def new + @place = Place.new + render json: @place + end + # POST /places def create @place = Place.new(place_params) From 95da506ed3554ac3d74143009d7a44c160af04a2 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Thu, 29 Feb 2024 15:03:04 +0200 Subject: [PATCH 2/4] Refactor place creation and error handling in PlacesController --- app/controllers/api/v1/places_controller.rb | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/controllers/api/v1/places_controller.rb b/app/controllers/api/v1/places_controller.rb index 7f1d53c..13b8529 100644 --- a/app/controllers/api/v1/places_controller.rb +++ b/app/controllers/api/v1/places_controller.rb @@ -22,9 +22,19 @@ def create @place = Place.new(place_params) if @place.save - render json: @place, status: :created, location: @place + render json: { + status: { + code: 200, + message: 'Place created successfully' + } + }, status: :ok else - render json: @place.errors, status: :unprocessable_entity + render json: { + status: { + message: "Place could not be created", + errors: @place.errors.full_messages + } + }, status: :unprocessable_entity end end @@ -51,6 +61,6 @@ def set_place # Only allow a list of trusted parameters through. def place_params - params.require(:place).permit(:description, :photo, :location, :rate, :user_id) + params.require(:place).permit(:description, :photo, :location, :rate, :user_id, :address) end end From de9f5c597ebb26be66d8d3e040f6fffc170b1d86 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Fri, 1 Mar 2024 11:14:40 +0200 Subject: [PATCH 3/4] Refactor place creation in PlacesController --- app/controllers/api/v1/places_controller.rb | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/app/controllers/api/v1/places_controller.rb b/app/controllers/api/v1/places_controller.rb index 13b8529..b2d867e 100644 --- a/app/controllers/api/v1/places_controller.rb +++ b/app/controllers/api/v1/places_controller.rb @@ -22,19 +22,9 @@ def create @place = Place.new(place_params) if @place.save - render json: { - status: { - code: 200, - message: 'Place created successfully' - } - }, status: :ok + render json: @place, status: :created else - render json: { - status: { - message: "Place could not be created", - errors: @place.errors.full_messages - } - }, status: :unprocessable_entity + render json: @place.errors, status: :unprocessable_entity end end From 97498dc9abe025e56cbfedc0d4020de02dcad434 Mon Sep 17 00:00:00 2001 From: tsheporamantso Date: Fri, 1 Mar 2024 18:06:07 +0200 Subject: [PATCH 4/4] Correct rubocop offences --- app/controllers/api/v1/places_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/api/v1/places_controller.rb b/app/controllers/api/v1/places_controller.rb index b2d867e..6625121 100644 --- a/app/controllers/api/v1/places_controller.rb +++ b/app/controllers/api/v1/places_controller.rb @@ -14,7 +14,7 @@ def show def new @place = Place.new - render json: @place + render json: @place end # POST /places