diff --git a/app/controllers/rooms_controller.rb b/app/controllers/rooms_controller.rb index 3c12009..5f3423c 100644 --- a/app/controllers/rooms_controller.rb +++ b/app/controllers/rooms_controller.rb @@ -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 diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb new file mode 100644 index 0000000..10747ad --- /dev/null +++ b/app/controllers/votes_controller.rb @@ -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 diff --git a/app/models/room_venue.rb b/app/models/room_venue.rb index 4de3703..1e61a6c 100644 --- a/app/models/room_venue.rb +++ b/app/models/room_venue.rb @@ -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 diff --git a/app/models/vote.rb b/app/models/vote.rb index 4c58e4f..2690f5b 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,2 +1,3 @@ class Vote < ApplicationRecord + has_one :room_venue end diff --git a/app/views/rooms/create.json.jbuilder b/app/views/rooms/create.json.jbuilder index ab7724c..9dcebc1 100644 --- a/app/views/rooms/create.json.jbuilder +++ b/app/views/rooms/create.json.jbuilder @@ -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 diff --git a/app/views/rooms/show.json.jbuilder b/app/views/rooms/show.json.jbuilder new file mode 100644 index 0000000..8c202ec --- /dev/null +++ b/app/views/rooms/show.json.jbuilder @@ -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 diff --git a/app/views/rooms/update.json.jbuilder b/app/views/rooms/update.json.jbuilder new file mode 100644 index 0000000..9c266c9 --- /dev/null +++ b/app/views/rooms/update.json.jbuilder @@ -0,0 +1,5 @@ +json.room do + json.id @room.id + json.name @room.name + json.status @room.status +end diff --git a/app/views/venues/index.json.jbuilder b/app/views/venues/index.json.jbuilder index af9b519..20c88bf 100644 --- a/app/views/venues/index.json.jbuilder +++ b/app/views/venues/index.json.jbuilder @@ -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 diff --git a/config/environments/development.rb b/config/environments/development.rb index 0f23ad2..21175ad 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 2da4c75..6fd7f97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/20250410174025_create_rooms_and_venues.rb b/db/migrate/20250410174025_create_rooms_and_venues.rb index 3e6e409..3fe974c 100644 --- a/db/migrate/20250410174025_create_rooms_and_venues.rb +++ b/db/migrate/20250410174025_create_rooms_and_venues.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 5c33104..1d5e9ed 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -46,6 +46,8 @@ t.integer "cost" t.integer "rating" t.integer "category" + t.decimal "lat", precision: 10, scale: 6 + t.decimal "lng", precision: 10, scale: 6 t.datetime "created_at", null: false t.datetime "updated_at", null: false end diff --git a/db/seeds.rb b/db/seeds.rb index 2541e65..342d880 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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 diff --git a/spec/factories/rooms.rb b/spec/factories/rooms.rb index af87d5a..694093c 100644 --- a/spec/factories/rooms.rb +++ b/spec/factories/rooms.rb @@ -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 diff --git a/spec/factories/venues.rb b/spec/factories/venues.rb index 1addbf0..3fd265f 100644 --- a/spec/factories/venues.rb +++ b/spec/factories/venues.rb @@ -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