Skip to content

Commit

Permalink
added seeding code
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhi12122000 committed Dec 8, 2024
1 parent a5949f8 commit b1c75aa
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ gem "sprockets-rails"

gem "pg", "~> 1.3.0"

gem "faker", "~> 3.5.1"

# Check the latest supported [https://docs.aws.amazon.com/elasticbeanstalk/latest/platforms/platforms-supported.html#platforms-supported.ruby]
gem "puma", ">= 5.0"

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ GEM
reline (>= 0.3.8)
drb (2.2.1)
erubi (1.13.0)
faker (3.5.1)
i18n (>= 1.8.11, < 2)
globalid (1.2.1)
activesupport (>= 6.1)
i18n (1.14.6)
Expand Down Expand Up @@ -273,6 +275,7 @@ DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap
debug
faker (~> 3.5.1)
importmap-rails
jwt
kaminari
Expand Down
184 changes: 124 additions & 60 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,144 @@
# db/seeds.rb
require 'faker'

# Clear existing data
User.destroy_all
puts "Clearing existing data..."
Restaurant.destroy_all
Dish.destroy_all
Review.destroy_all
Comment.destroy_all
Photo.destroy_all
User.destroy_all

# Create Users
users = 5.times.map do |i|
User.create!(
name: "User #{i + 1}",
email: "user#{i + 1}@example.com",
password: "password",
password_confirmation: "password",
role: "business_owner"
)
# Constants
USER_COUNT = 5
RESTAURANTS_PER_USER = 5
DISHES_PER_RESTAURANT = 10
REVIEWS_PER_RESTAURANT = 15
COMMENTS_PER_REVIEW = 5
PHOTOS_PER_RESTAURANT = 3

# Helper method to get current timestamp
current_time = Time.now

# Insert Users
puts "Inserting users..."
users = USER_COUNT.times.map do
{
name: Faker::Name.name,
email: Faker::Internet.unique.email,
password_digest: BCrypt::Password.create('password'), # Ensure passwords are hashed
role: "business_owner",
created_at: current_time,
updated_at: current_time
}
end
User.insert_all(users)
user_ids = User.pluck(:id)

# Insert Restaurants
puts "Inserting restaurants..."
restaurants = []
user_ids.each do |user_id|
RESTAURANTS_PER_USER.times do
restaurants << {
name: Faker::Restaurant.name,
address: Faker::Address.street_address,
city: Faker::Address.city,
state: Faker::Address.state_abbr,
zip: Faker::Address.zip_code,
description: Faker::Restaurant.description,
phone_number: Faker::PhoneNumber.phone_number,
website: Faker::Internet.url,
user_id: user_id,
created_at: current_time,
updated_at: current_time
}
end
end

# Create Restaurants
users.each do |user|
restaurants = 3.times.map do |i|
Restaurant.create!(
name: "Restaurant #{i + 1}",
address: "123 Main St",
city: "City #{i + 1}",
state: "State #{i + 1}",
zip: "12345",
description: "A great place to eat.",
phone_number: "123-456-7890",
website: "http://restaurant#{i + 1}.com",
user: user
)
puts "Now inserting..."
Restaurant.insert_all(restaurants)
restaurant_ids = Restaurant.pluck(:id)

# Insert Dishes
puts "Inserting dishes..."
dishes = []
restaurant_ids.each do |restaurant_id|
DISHES_PER_RESTAURANT.times do
dishes << {
name: Faker::Food.dish,
description: Faker::Food.description,
price: Faker::Commerce.price(range: 5..50),
restaurant_id: restaurant_id,
created_at: current_time,
updated_at: current_time
}
end
end
Dish.insert_all(dishes)

# Create Dishes, Reviews, Comments, and Photos for each Restaurant
restaurants.each do |restaurant|
# Create Dishes
5.times do |i|
Dish.create!(
name: "Dish #{i + 1}",
description: "Delicious dish #{i + 1}",
price: (10 + i).to_f,
restaurant: restaurant
)
end
# Insert Reviews
puts "Inserting reviews..."
reviews = []
restaurant_ids.each do |restaurant_id|
REVIEWS_PER_RESTAURANT.times do
reviews << {
rating: rand(1..5),
content: Faker::Lorem.sentence(word_count: 20),
user_id: user_ids.sample,
restaurant_id: restaurant_id,
created_at: current_time,
updated_at: current_time
}
end
end
Review.insert_all(reviews)
review_ids = Review.pluck(:id)

# Create Reviews
3.times do |i|
review = Review.create!(
rating: rand(1..5),
content: "Review content #{i + 1}",
user: users.sample,
restaurant: restaurant
)
# Insert Comments
puts "Inserting comments..."
comments = []
review_ids.each do |review_id|
COMMENTS_PER_REVIEW.times do
comments << {
content: Faker::Lorem.sentence(word_count: 10),
user_id: user_ids.sample,
review_id: review_id,
created_at: current_time,
updated_at: current_time
}
end
end
Comment.insert_all(comments)

# Create Comments for each Review
2.times do |j|
Comment.create!(
content: "Comment content #{j + 1}",
user: users.sample,
review: review
)
end
end
# Insert Photos
puts "Inserting photos..."
photos = []
current_time = Time.now

# Create Photos
2.times do |i|
Photo.create!(
image: File.open("./db/woodstock.webp"),
primary: i == 0,
restaurant: restaurant
)
end
restaurant_ids.each do |restaurant_id|
PHOTOS_PER_RESTAURANT.times do |i|
photos << {
primary: i == 0,
restaurant_id: restaurant_id,
created_at: current_time,
updated_at: current_time
}
end
end
# Bulk insert photos without attaching images
Photo.insert_all(photos)
# Now attach images after the photos are inserted
photos = Photo.order(:id).last(restaurant_ids.size * PHOTOS_PER_RESTAURANT)
photos.each_with_index do |photo, index|
# You can use a different image for each photo or repeat the same image
photo.image.attach(
io: File.open("./db/woodstock.webp"),
filename: "woodstock_#{index + 1}.webp",
content_type: 'image/webp'
)
end
puts "Photos inserted and images attached!"

puts "Seeding completed!"
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"localhost",
".localhost"
],
"proxy": "https://localhost:3386/"
"proxy": "https://localhost:3000/"
},
"dependencies": {
"@emotion/react": "^11.13.3",
Expand Down

0 comments on commit b1c75aa

Please sign in to comment.