Skip to content

Commit

Permalink
reservations now working
Browse files Browse the repository at this point in the history
  • Loading branch information
srinjoyc committed Apr 1, 2018
1 parent f5b65ac commit 5f2d7b5
Show file tree
Hide file tree
Showing 20 changed files with 577 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ group :development do
gem 'database_cleaner'
gem 'factory_girl'
gem 'faker'
gem 'rails_admin', '~> 1.3'
end

gem 'simplecov', require: false, group: :test
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
73 changes: 71 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,53 @@ GEM
arel (8.0.0)
builder (3.2.3)
byebug (10.0.0)
coffee-rails (4.2.2)
coffee-script (>= 2.2.0)
railties (>= 4.0.0)
coffee-script (2.4.1)
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.0.5)
crass (1.0.3)
database_cleaner (1.6.2)
diff-lcs (1.3)
docile (1.3.0)
erubi (1.7.1)
execjs (2.7.0)
factory_girl (4.9.0)
activesupport (>= 3.0.0)
faker (1.8.7)
i18n (>= 0.7)
ffi (1.9.23)
font-awesome-rails (4.7.0.3)
railties (>= 3.2, < 5.2)
globalid (0.4.1)
activesupport (>= 4.2.0)
haml (5.0.4)
temple (>= 0.8.0)
tilt
i18n (0.9.5)
concurrent-ruby (~> 1.0)
jquery-rails (4.3.1)
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
jquery-ui-rails (5.0.5)
railties (>= 3.2.16)
json (2.1.0)
kaminari (1.1.1)
activesupport (>= 4.1.0)
kaminari-actionview (= 1.1.1)
kaminari-activerecord (= 1.1.1)
kaminari-core (= 1.1.1)
kaminari-actionview (1.1.1)
actionview
kaminari-core (= 1.1.1)
kaminari-activerecord (1.1.1)
activerecord
kaminari-core (= 1.1.1)
kaminari-core (1.1.1)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -68,13 +101,16 @@ GEM
mini_mime (1.0.0)
mini_portile2 (2.3.0)
minitest (5.11.3)
nested_form (0.3.2)
nio4r (2.3.0)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
pg (0.21.0)
pp (0.1.1)
puma (3.11.3)
rack (2.0.4)
rack-pjax (1.0.0)
nokogiri (~> 1.5)
rack (>= 1.1)
rack-test (0.8.3)
rack (>= 1.0, < 3)
rails (5.1.5)
Expand All @@ -94,6 +130,19 @@ GEM
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.3)
loofah (~> 2.0)
rails_admin (1.3.0)
builder (~> 3.1)
coffee-rails (~> 4.0)
font-awesome-rails (>= 3.0, < 5)
haml (>= 4.0, < 6)
jquery-rails (>= 3.0, < 5)
jquery-ui-rails (~> 5.0)
kaminari (>= 0.14, < 2.0)
nested_form (~> 0.3)
rack-pjax (>= 0.7)
rails (>= 4.0, < 6)
remotipart (~> 1.3)
sass-rails (>= 4.0, < 6)
railties (5.1.5)
actionpack (= 5.1.5)
activesupport (= 5.1.5)
Expand All @@ -104,6 +153,7 @@ GEM
rb-fsevent (0.10.3)
rb-inotify (0.9.10)
ffi (>= 0.5.0, < 2)
remotipart (1.3.1)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
Expand All @@ -122,8 +172,24 @@ GEM
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
ruby_dep (1.5.0)
sass (3.5.6)
sass-listen (~> 4.0.0)
sass-listen (4.0.0)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
sass-rails (5.0.7)
railties (>= 4.0.0, < 6)
sass (~> 3.1)
sprockets (>= 2.8, < 4.0)
sprockets-rails (>= 2.0, < 4.0)
tilt (>= 1.1, < 3)
shoulda-matchers (3.1.2)
activesupport (>= 4.0.0)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
simplecov-html (~> 0.10.0)
simplecov-html (0.10.2)
spring (2.0.2)
activesupport (>= 4.2)
spring-watcher-listen (2.0.1)
Expand All @@ -136,8 +202,10 @@ GEM
actionpack (>= 4.0)
activesupport (>= 4.0)
sprockets (>= 3.0.0)
temple (0.8.0)
thor (0.20.0)
thread_safe (0.3.6)
tilt (2.0.8)
tzinfo (1.2.5)
thread_safe (~> 0.1)
websocket-driver (0.6.5)
Expand All @@ -154,11 +222,12 @@ DEPENDENCIES
faker
listen (>= 3.0.5, < 3.2)
pg (~> 0.18)
pp
puma (~> 3.7)
rails (~> 5.1.1)
rails_admin (~> 1.3)
rspec-rails (>= 3.5.0)
shoulda-matchers (~> 3.1)
simplecov
spring
spring-watcher-listen (~> 2.0.0)
tzinfo-data
Expand Down
37 changes: 33 additions & 4 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ReservationsController < ApplicationController
before_action :set_reservation, only: [:show, :update, :destroy]

before_action :verify_guest
# GET /reservations
def index
@reservations = Reservation.all
Expand All @@ -15,9 +15,35 @@ def show

# POST /reservations
def create
@reservation = Reservation.new(reservation_params)
request = create_reservation_params

guest = Guest.find_by_email(request[:guest_email])
restaurant = Restaurant.find(request[:restaurant_id])
table = Reservation.get_table(restaurant.id, request[:reservation_time].to_time, request[:guest_count])
shift = restaurant.get_shift request[:reservation_time].to_time

if table.nil? || shift.nil?
msg = ""
msg += "has no tables available at that time" if table.nil?
msg += "is not open at that time. #{RestaurantShift.restaurant_shift_string restaurant.id}" if shift.nil?
error = {
restaurant: msg
}
render json: error, status: :unprocessable_entity
return
end

@reservation = Reservation.new({
restaurant_id: restaurant.id,
guest_id: guest.id,
restaurant_table_id: table.id,
restaurant_shift_id: shift.id,
guest_count: request[:guest_count],
reservation_time: request[:reservation_time].to_time
})

if @reservation.save
Reservation.reserve_table @reservation, table
render json: @reservation, status: :created, location: @reservation
else
render json: @reservation.errors, status: :unprocessable_entity
Expand All @@ -44,8 +70,11 @@ def set_reservation
@reservation = Reservation.find(params[:id])
end

# Only allow a trusted parameter "white list" through.
def reservation_params
params.require(:reservation).permit(:restaurant_id, :guest_id, :restaurant_table_id, :restaurant_shift_id, :guest_count, :reservation_time)
params.require(:reservation).permit(:restaurant_id, :guest_id, :restaurant_table_id, :restaurant_shift_id, :guest_count, :reservation_time, :guest_email)
end

def create_reservation_params
params.require(:reservation).permit(:restaurant_id, :guest_email, :guest_count,:reservation_time)
end
end
15 changes: 13 additions & 2 deletions app/controllers/restaurants_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,19 @@ class RestaurantsController < ApplicationController

# GET /restaurants
def index
@restaurants = Restaurant.all

@restaurants =
Restaurant.joins(:restaurant_shifts,:restaurant_tables)
.select("restaurant_shifts.start_time,
restaurant_shifts.end_time,
restaurant_shifts.name as shift_name,
restaurant_tables.name as table_name,
restaurant_tables.minimum_count,
restaurant_tables.maximum_count,
restaurants.id,
restaurants.name,
restaurants.email,
restaurants.phone_number
")
render json: @restaurants
end

Expand Down
5 changes: 5 additions & 0 deletions app/controllers/static_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class StaticController < ApplicationController
def index
render file: Rails.root.join('public', 'index.html')
end
end
29 changes: 28 additions & 1 deletion app/models/reservation.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
require 'date'

class Reservation < ApplicationRecord
belongs_to :restaurant
belongs_to :guest
belongs_to :restaurant_table
has_one :restaurant_table
belongs_to :restaurant_shift

validates_associated :restaurant, :guest, :restaurant_table, :restaurant_shift
validates :guest_count, :reservation_time, :restaurant_id, :restaurant_shift_id, :restaurant_table_id, presence: true
before_save :valid_guest_count

def self.get_table restaurant_id, time, guest_count
# get reservations 1 hour before and 1 hour after the given time
puts time
reserved_table_ids = ReservedTable.joins(:restaurant_table)
.where(restaurant_id: restaurant_id)
.where(reservation_time: time - 3600...time + 3600)
.pluck(:restaurant_table_id)

available_tables = RestaurantTable.where(restaurant_id: restaurant_id)
.where('minimum_count <= ? AND maximum_count >= ?', guest_count, guest_count)
.where.not(id: reserved_table_ids)
available_tables.first
end

def self.reserve_table reservation, table
ReservedTable.create!({
reservation_id: reservation.id,
reservation_time: reservation.reservation_time,
restaurant_id: reservation.restaurant_id,
restaurant_table_id: table.id,
})
end

private

def valid_guest_count
guest_count > 0
end

end
5 changes: 5 additions & 0 deletions app/models/reserved_table.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class ReservedTable < ApplicationRecord
belongs_to :restaurant
belongs_to :restaurant_table
belongs_to :reservation
end
14 changes: 13 additions & 1 deletion app/models/restaurant.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
class Restaurant < ApplicationRecord
has_many :restaurant_tables, :dependent => :destroy
has_many :restaurant_shifts, :dependent => :destroy
has_many :reservations, :dependent => :destroy
validates :name, :email, :phone_number, presence: true
validates :email, uniqueness: true, format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }
validates :email, uniqueness: true,
format: { with: /\A[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}\z/i }

def get_shift time
hour = time.hour
puts "-----------------"
puts hour
shift = self.restaurant_shifts.where("start_time >= ? AND end_time >= ?", hour, hour + 1).first
end

end
10 changes: 9 additions & 1 deletion app/models/restaurant_shift.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
class RestaurantShift < ApplicationRecord
belongs_to :restaurant

validates_associated :restaurant
validates :start_time, :end_time, :name, :restaurant_id, presence: true
before_save :has_no_conflicts


def self.restaurant_shift_string restaurant_id
shift_times = restaurant.restaurant_shifts
msg = ""
msg += shift_times.map { |shift|
"The #{shift.name} is from #{shift.start_time} to #{shift.end_time}"
}
end
private
def has_no_conflicts
return true
Expand Down
5 changes: 4 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ module BookingSystem
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1

config.session_store :cookie_store
config.middleware.use ActionDispatch::Cookies
config.middleware.use ActionDispatch::Session::CookieStore, config.session_options
config.middleware.use Rack::MethodOverride
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
Expand Down
41 changes: 41 additions & 0 deletions config/initializers/rails_admin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
RailsAdmin.config do |config|

### Popular gems integration

## == Devise ==
# config.authenticate_with do
# warden.authenticate! scope: :user
# end
# config.current_user_method(&:current_user)

## == Cancan ==
# config.authorize_with :cancan

## == Pundit ==
# config.authorize_with :pundit

## == PaperTrail ==
# config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

## == Gravatar integration ==
## To disable Gravatar integration in Navigation Bar set to false
# config.show_gravatar = true

config.actions do
dashboard # mandatory
index # mandatory
new
export
bulk_delete
show
edit
delete
show_in_app

## With an audit adapter, you can add:
# history_index
# history_show
end
end
Loading

0 comments on commit 5f2d7b5

Please sign in to comment.