-
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.
- Loading branch information
1 parent
f92baaa
commit 9496433
Showing
3 changed files
with
74 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Api::V1::RecipesController < ApplicationController | ||
def index | ||
recipe = Recipe.all.order(created_at: :desc) | ||
render json: recipe | ||
end | ||
|
||
def create | ||
recipe = Recipe.create!(recipe_params) | ||
if recipe | ||
render json: recipe | ||
else | ||
render json: recipe.errors | ||
end | ||
end | ||
|
||
def show | ||
if recipe | ||
render json: recipe | ||
else | ||
render json: recipe.errors | ||
end | ||
end | ||
|
||
def destroy | ||
recipe&.destroy | ||
render json: { message: 'Recipe deleted!' } | ||
end | ||
|
||
private | ||
|
||
def recipe_params | ||
params.permit(:name, :image, :ingredients, :instruction) | ||
end | ||
|
||
def recipe | ||
@recipe ||= Recipe.find(params[:id]) | ||
end | ||
end |
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,4 +1,13 @@ | ||
Rails.application.routes.draw do | ||
namespace :api do | ||
namespace :v1 do | ||
get 'recipes/index' | ||
post 'recipes/create' | ||
get '/show/:id', to: 'recipes#show' | ||
delete '/destroy/:id', to: 'recipes#destroy' | ||
end | ||
end | ||
root 'homepage#index' | ||
get '/*path' => 'homepage#index' | ||
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.