-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from tsheporamantso/places-controller
Refactor places_controller.rb
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |