Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions app/controllers/rooms_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,52 @@
class RoomsController < ApplicationController
include RoomInviteCodifier

def create
@room = Room.new(create_room_params.merge(invite_code: new_room_invite_code, status: :open))
if @room.save
render :create, status: :created
ActiveRecord::Base.transaction do
if @room.save
@host = Attendee.new(room_id: @room.id, name: host_params[:name])
render :create, status: :created if @host.save
else
@errors = @room.errors.full_messages
render 'shared/errors', status: :unprocessable_entity
end
end
end

def show
@room = Room.find_by(id: params[:id]) || Room.find_by(invite_code: params[:id])
@attendees = @room.attendees
@venues = @room.venues
@room_venue_by_id = RoomVenue.where(room_id: @room.id).index_by(&:id)
@votes = Vote.where(rooms_venues_id: @room_venue_by_id.keys)

render :show, status: :ok
end

def update
@room = Room.find(params[:id])
if @room.update(update_room_status_params)
render :update, status: :ok
else
@errors = @room.errors.full_messages
render 'shared/errors', status: :unprocessable_entity
end
end

def update_room_status_params
params.require(:room)
.permit(:status)
end

def create_room_params
params
.require(:room)
.permit(:name, venue_ids: [])
end

def host_params
params.require(:host)
.permit(:name)
end
end
17 changes: 17 additions & 0 deletions app/controllers/votes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class VotesController < ApplicationController
def create
rooms_venues_id = RoomVenue.find_by(venue_id: vote_params[:venue_id],
room_id: vote_params[:room_id]).id

@vote = Vote.new(rooms_venues_id:, attendee_id: vote_params[:attendee_id])
if @vote.save
render json: { message: 'Vote registered successfully' }, status: :created
else
render json: { errors: @vote.errors.full_messages }, status: :unprocessable_entity
end
end

def vote_params
params.require(:vote).permit(:room_id, :venue_id, :attendee_id)
end
end
4 changes: 3 additions & 1 deletion app/models/room_venue.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class RoomVenue < ApplicationRecord
self.table_name = 'rooms_venues'

belongs_to :room
belongs_to :venue

has_many :votes
has_many :votes, foreign_key: :rooms_venues_id
end
1 change: 1 addition & 0 deletions app/models/vote.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class Vote < ApplicationRecord
has_one :room_venue
end
4 changes: 4 additions & 0 deletions app/views/rooms/create.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
json.room do
json.id @room.id
json.name @room.name
json.invite_code @room.invite_code
json.venue_ids @room.venues.map(&:id)
json.status @room.status
end
json.host do
json.id @host.id
end
28 changes: 28 additions & 0 deletions app/views/rooms/show.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
json.room do
json.id @room.id
json.name @room.name
json.invite_code @room.invite_code
json.status @room.status
json.attendees do
json.array! @attendees do |attendee|
json.id attendee.id
json.name attendee.name
end
end
json.venues do
json.array! @venues do |venue|
json.id venue.id
json.name venue.name
json.category venue.category
json.instagram_link venue.instagram_link
json.cost venue.cost
json.rating venue.rating
json.votes do
json.array! @votes do |vote|
json.attendee_id vote.attendee_id
json.venue_id @room_venue_by_id[vote.rooms_venues_id].venue_id
end
end
end
end
end
5 changes: 5 additions & 0 deletions app/views/rooms/update.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
json.room do
json.id @room.id
json.name @room.name
json.status @room.status
end
3 changes: 3 additions & 0 deletions app/views/venues/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
json.array! @venues do |venue|
json.id venue.id
json.name venue.name
json.instagram_link venue.instagram_link
json.menu_link venue.menu_link
json.cost venue.cost
json.rating venue.rating
json.category venue.category
json.latitude venue.lat
json.longitude venue.lng
end
4 changes: 3 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
Bullet.alert = true
Bullet.rails_logger = true
end


config.hosts << /[a-z0-9\-]+\.ngrok\.app/

# Settings specified here will take precedence over those in config/application.rb.

# In the development environment your application's code is reloaded any time
Expand Down
12 changes: 10 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
Rails.application.routes.draw do
constraints format: 'json' do
# Rooms
post '/rooms', to: 'rooms#create'
post 'rooms/:invite', to: 'rooms#join'
post '/rooms/:id', to: 'rooms#update'
get '/rooms/:id', to: 'rooms#show'
# Venues
get '/venues', to: 'venues#index'
post '/venues', to: 'venues#create'

# Attendees
post '/attendees', to: 'attendees#create'
get '/venues', to: 'venues#index'

# Votes
post '/votes', to: 'votes#create'
end
end
2 changes: 2 additions & 0 deletions db/migrate/20250410174025_create_rooms_and_venues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ def change
t.integer :cost
t.integer :rating
t.integer :category
t.decimal :lat, :precision => 10, :scale => 6
t.decimal :lng, :precision => 10, :scale => 6

t.timestamps
end
Expand Down
2 changes: 2 additions & 0 deletions db/schema.rb

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

11 changes: 11 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,15 @@
FactoryBot::create(:venue)
end

3.times do
FactoryBot::create(:room,
venues: [Venue.first, Venue.second, Venue.third],
host_id: nil)
end

Room.all.each do |room|
host = FactoryBot::create(:attendee, room_id: room.id)
room.update(host_id: host.id)
end

# Seeds::UserSeeder.new.run
6 changes: 4 additions & 2 deletions spec/factories/rooms.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
FactoryBot.define do
factory :room do
invite_code { SecureRandom.hex(10) }
venue { venue }
name { Faker::Book.title }
invite_code { SecureRandom.hex(3) }
venues { [venue] }
host_id { attendee }
status { :open }
end
end
7 changes: 5 additions & 2 deletions spec/factories/venues.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
factory :venue do
name { Faker::Restaurant.name }
menu_link { 'http://google.com' }
cost { 100 }
rating { 5 }
instagram_link { 'http://google.com' }
cost { (1..5).to_a.sample }
rating { (3..5).sample }
category { Venue.categories.values.sample }
lat { Faker::Address.latitude }
lng { Faker::Address.longitude }
end
end
Loading