Skip to content

Commit

Permalink
Created controller for your recipes
Browse files Browse the repository at this point in the history
  • Loading branch information
begonaguereca committed Jan 23, 2021
1 parent f92baaa commit 9496433
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
38 changes: 38 additions & 0 deletions app/controllers/api/v1/recipes_controller.rb
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
9 changes: 9 additions & 0 deletions config/routes.rb
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
27 changes: 27 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9496433

Please sign in to comment.