-
Notifications
You must be signed in to change notification settings - Fork 0
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
4ef06a2
commit bab7145
Showing
13 changed files
with
137 additions
and
2 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
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,62 @@ | ||
module Api | ||
class DishesController < ApplicationController | ||
before_action :authorize_business_owner, only: [:create, :update, :destroy] | ||
skip_before_action :authorize_request, only: [:show, :index] | ||
|
||
# GET /restaurants/:restaurant_id/dishes | ||
def index | ||
@restaurant = Restaurant.find(params[:restaurant_id]) | ||
@dishes = @restaurant.dishes | ||
render json: @dishes | ||
end | ||
|
||
# GET /dishes/:id | ||
def show | ||
@dish = Dish.find(params[:id]) | ||
render json: @dish | ||
end | ||
|
||
# POST /restaurants/:restaurant_id/dishes | ||
def create | ||
@restaurant = Restaurant.find(params[:restaurant_id]) | ||
@dish = @restaurant.dishes.build(dish_params) | ||
|
||
if @dish.save | ||
render json: @dish, status: :created | ||
else | ||
render json: { errors: @dish.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# PATCH/PUT /dishes/:id | ||
def update | ||
@dish = Dish.find(params[:id]) | ||
|
||
if @dish.update(dish_params) | ||
render json: @dish | ||
else | ||
render json: { errors: @dish.errors.full_messages }, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
# DELETE /dishes/:id | ||
def destroy | ||
@dish = Dish.find(params[:id]) | ||
@dish.destroy | ||
head :no_content | ||
end | ||
|
||
private | ||
|
||
def dish_params | ||
params.require(:dish).permit(:name, :description, :price) | ||
end | ||
|
||
def authorize_business_owner | ||
unless @user.role == 'business_owner' | ||
render json: { error: 'You are not authorized to perform this action' }, status: :forbidden | ||
end | ||
end | ||
|
||
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
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
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
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
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,3 @@ | ||
class Dish < ApplicationRecord | ||
belongs_to :restaurant | ||
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
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
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,12 @@ | ||
class CreateDishes < ActiveRecord::Migration[7.1] | ||
def change | ||
create_table :dishes do |t| | ||
t.string :name | ||
t.text :description | ||
t.decimal :price | ||
t.references :restaurant, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,13 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
name: MyString | ||
description: MyText | ||
price: 9.99 | ||
restaurant: one | ||
|
||
two: | ||
name: MyString | ||
description: MyText | ||
price: 9.99 | ||
restaurant: two |
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,7 @@ | ||
require "test_helper" | ||
|
||
class DishTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |