From bab7145991a06298449e8c448e27fc49c3905491 Mon Sep 17 00:00:00 2001 From: praneeth-bala Date: Wed, 20 Nov 2024 10:35:48 -0800 Subject: [PATCH] added dishes --- app/controllers/api/comments_controller.rb | 2 + app/controllers/api/dishes_controller.rb | 62 +++++++++++++++++++ app/controllers/api/photos_controller.rb | 9 +++ app/controllers/api/restaurants_controller.rb | 11 +++- app/controllers/api/reviews_controller.rb | 2 + app/controllers/api/users_controller.rb | 2 + app/models/dish.rb | 3 + app/models/restaurant.rb | 1 + config/routes.rb | 2 + db/migrate/20241120182106_create_dishes.rb | 12 ++++ db/schema.rb | 13 +++- test/fixtures/dishes.yml | 13 ++++ test/models/dish_test.rb | 7 +++ 13 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 app/controllers/api/dishes_controller.rb create mode 100644 app/models/dish.rb create mode 100644 db/migrate/20241120182106_create_dishes.rb create mode 100644 test/fixtures/dishes.yml create mode 100644 test/models/dish_test.rb diff --git a/app/controllers/api/comments_controller.rb b/app/controllers/api/comments_controller.rb index ccaba8e..47325f1 100644 --- a/app/controllers/api/comments_controller.rb +++ b/app/controllers/api/comments_controller.rb @@ -1,5 +1,7 @@ module Api class CommentsController < ApplicationController + skip_before_action :authorize_request, only: [:show, :index] + # GET /restaurants/:restaurant_id/reviews/:review_id/comments def index @review = Review.find(params[:review_id]) # Find the review by its ID diff --git a/app/controllers/api/dishes_controller.rb b/app/controllers/api/dishes_controller.rb new file mode 100644 index 0000000..bb98778 --- /dev/null +++ b/app/controllers/api/dishes_controller.rb @@ -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 \ No newline at end of file diff --git a/app/controllers/api/photos_controller.rb b/app/controllers/api/photos_controller.rb index ca0cda7..e01139e 100644 --- a/app/controllers/api/photos_controller.rb +++ b/app/controllers/api/photos_controller.rb @@ -2,6 +2,8 @@ module Api class PhotosController < ApplicationController before_action :set_restaurant, only: %i[index create] before_action :set_photo, only: %i[show update destroy] + before_action :authorize_business_owner, only: [:create, :update, :destroy] + skip_before_action :authorize_request, only: [:show, :index] # GET /restaurants/:restaurant_id/photos def index @@ -66,5 +68,12 @@ def set_photo def photo_params params.require(:photo).permit(:image) # Only permit the photo image 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 diff --git a/app/controllers/api/restaurants_controller.rb b/app/controllers/api/restaurants_controller.rb index a393c6b..eb4cde7 100644 --- a/app/controllers/api/restaurants_controller.rb +++ b/app/controllers/api/restaurants_controller.rb @@ -1,5 +1,8 @@ module Api class RestaurantsController < ApplicationController + before_action :authorize_business_owner, only: [:create, :update, :destroy] + skip_before_action :authorize_request, only: [:show, :index] + # GET /restaurants def index @restaurants = Restaurant.all @@ -53,6 +56,12 @@ def destroy private + def authorize_business_owner + unless @user.role == 'business_owner' + render json: { error: 'You are not authorized to perform this action' }, status: :forbidden + end + end + def restaurant_params params.require(:restaurant).permit( :name, @@ -66,4 +75,4 @@ def restaurant_params ) end end -end +end \ No newline at end of file diff --git a/app/controllers/api/reviews_controller.rb b/app/controllers/api/reviews_controller.rb index 96c7451..5977196 100644 --- a/app/controllers/api/reviews_controller.rb +++ b/app/controllers/api/reviews_controller.rb @@ -1,5 +1,7 @@ module Api class ReviewsController < ApplicationController + skip_before_action :authorize_request, only: [:show, :index] + # GET /restaurants/:restaurant_id/reviews def index @restaurant = Restaurant.find(params[:restaurant_id]) diff --git a/app/controllers/api/users_controller.rb b/app/controllers/api/users_controller.rb index 57fdc1e..70a5a71 100644 --- a/app/controllers/api/users_controller.rb +++ b/app/controllers/api/users_controller.rb @@ -1,5 +1,7 @@ module Api class UsersController < ApplicationController + skip_before_action :authorize_request, only: [:show, :index] + # GET /users def index @users = User.all diff --git a/app/models/dish.rb b/app/models/dish.rb new file mode 100644 index 0000000..9b66e69 --- /dev/null +++ b/app/models/dish.rb @@ -0,0 +1,3 @@ +class Dish < ApplicationRecord + belongs_to :restaurant +end diff --git a/app/models/restaurant.rb b/app/models/restaurant.rb index efbbb85..f3d9a57 100644 --- a/app/models/restaurant.rb +++ b/app/models/restaurant.rb @@ -2,6 +2,7 @@ class Restaurant < ApplicationRecord belongs_to :user # The business owner has_many :reviews, dependent: :destroy has_many :photos, dependent: :destroy + has_many :dishes, dependent: :destroy # Validations to ensure data consistency validates :address, presence: true diff --git a/config/routes.rb b/config/routes.rb index 087dca2..844aefa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,9 +12,11 @@ resources :comments, only: [:index, :create] end resources :photos, only: [:index, :create] + resources :dishes, only: [:index, :create] end resources :reviews, only: [:show, :update, :destroy] resources :comments, only: [:show, :update, :destroy] resources :photos, only: [:show, :update, :destroy] + resources :dishes, only: [:show, :update, :destroy] end end diff --git a/db/migrate/20241120182106_create_dishes.rb b/db/migrate/20241120182106_create_dishes.rb new file mode 100644 index 0000000..64ac39e --- /dev/null +++ b/db/migrate/20241120182106_create_dishes.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 4e59bcb..2395c14 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_11_15_003447) do +ActiveRecord::Schema[7.1].define(version: 2024_11_20_182106) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -52,6 +52,16 @@ t.index ["user_id"], name: "index_comments_on_user_id" end + create_table "dishes", force: :cascade do |t| + t.string "name" + t.text "description" + t.decimal "price" + t.bigint "restaurant_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["restaurant_id"], name: "index_dishes_on_restaurant_id" + end + create_table "photos", force: :cascade do |t| t.bigint "restaurant_id", null: false t.datetime "created_at", null: false @@ -99,6 +109,7 @@ add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "comments", "reviews" add_foreign_key "comments", "users" + add_foreign_key "dishes", "restaurants" add_foreign_key "photos", "restaurants" add_foreign_key "restaurants", "users" add_foreign_key "reviews", "restaurants" diff --git a/test/fixtures/dishes.yml b/test/fixtures/dishes.yml new file mode 100644 index 0000000..40e7de5 --- /dev/null +++ b/test/fixtures/dishes.yml @@ -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 diff --git a/test/models/dish_test.rb b/test/models/dish_test.rb new file mode 100644 index 0000000..c7ffbc4 --- /dev/null +++ b/test/models/dish_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +class DishTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end