Skip to content

Commit

Permalink
Merge pull request #13 from tsheporamantso/places-controller
Browse files Browse the repository at this point in the history
Refactor places_controller.rb
  • Loading branch information
George7h authored Feb 29, 2024
2 parents f6abfe6 + 2e6e27d commit bca1206
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions app/controllers/api/v1/places_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,51 @@
class Api::V1::PlacesController < ApplicationController
before_action :set_place, only: %i[show update destroy]

# GET /places
def index
@places = Place.all
render json: @places
end

# GET /places/1
def show
render json: @place
end

# POST /places
def create
@place = Place.new(place_params)

if @place.save
render json: @place, status: :created, location: @place
else
render json: @place.errors, status: :unprocessable_entity
end
end

# PATCH/PUT /places/1
def update
if @place.update(place_params)
render json: @place
else
render json: @place.errors, status: :unprocessable_entity
end
end

# DELETE /places/1
def destroy
@place.destroy
end

private

# Use callbacks to share common setup or constraints between actions.
def set_place
@place = Place.find(params[:id])
end

# Only allow a list of trusted parameters through.
def place_params
params.require(:place).permit(:description, :photo, :location, :rate, :user_id)
end
end

0 comments on commit bca1206

Please sign in to comment.