diff --git a/Gemfile b/Gemfile index fecd89c7..3c07e3a5 100644 --- a/Gemfile +++ b/Gemfile @@ -13,7 +13,7 @@ gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby - +gem 'awesome_print' # Use jquery as the JavaScript library gem 'jquery-rails' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder @@ -22,7 +22,7 @@ gem 'jbuilder', '~> 2.0' gem 'sdoc', '~> 0.4.0', group: :doc # Use ActiveModel has_secure_password -# gem 'bcrypt', '~> 3.1.7' +gem 'bcrypt', '~> 3.1.7' # Use Unicorn as the app server # gem 'unicorn' @@ -42,4 +42,3 @@ group :development do # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end - diff --git a/Gemfile.lock b/Gemfile.lock index c701ada1..1859852b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,115 +1,125 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.0.0.1) - actionpack (= 5.0.0.1) - nio4r (~> 1.2) + actioncable (5.0.6) + actionpack (= 5.0.6) + nio4r (>= 1.2, < 3.0) websocket-driver (~> 0.6.1) - actionmailer (5.0.0.1) - actionpack (= 5.0.0.1) - actionview (= 5.0.0.1) - activejob (= 5.0.0.1) + actionmailer (5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.0.0.1) - actionview (= 5.0.0.1) - activesupport (= 5.0.0.1) + actionpack (5.0.6) + actionview (= 5.0.6) + activesupport (= 5.0.6) rack (~> 2.0) rack-test (~> 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.0.1) - activesupport (= 5.0.0.1) + actionview (5.0.6) + activesupport (= 5.0.6) builder (~> 3.1) erubis (~> 2.7.0) rails-dom-testing (~> 2.0) - rails-html-sanitizer (~> 1.0, >= 1.0.2) - activejob (5.0.0.1) - activesupport (= 5.0.0.1) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.0.6) + activesupport (= 5.0.6) globalid (>= 0.3.6) - activemodel (5.0.0.1) - activesupport (= 5.0.0.1) - activerecord (5.0.0.1) - activemodel (= 5.0.0.1) - activesupport (= 5.0.0.1) + activemodel (5.0.6) + activesupport (= 5.0.6) + activerecord (5.0.6) + activemodel (= 5.0.6) + activesupport (= 5.0.6) arel (~> 7.0) - activesupport (5.0.0.1) + activesupport (5.0.6) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) arel (7.1.4) - binding_of_caller (0.7.2) + awesome_print (1.8.0) + bcrypt (3.1.11) + binding_of_caller (0.8.0) debug_inspector (>= 0.0.1) - builder (3.2.2) - byebug (9.0.6) + builder (3.2.3) + byebug (9.1.0) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) coffee-script (2.4.1) coffee-script-source execjs - coffee-script-source (1.10.0) - concurrent-ruby (1.0.2) - debug_inspector (0.0.2) + coffee-script-source (1.12.2) + concurrent-ruby (1.0.5) + crass (1.0.3) + debug_inspector (0.0.3) erubis (2.7.0) execjs (2.7.0) - globalid (0.3.7) - activesupport (>= 4.1.0) - i18n (0.7.0) - jbuilder (2.6.0) - activesupport (>= 3.0.0, < 5.1) - multi_json (~> 1.2) - jquery-rails (4.2.1) + ffi (1.9.18) + globalid (0.4.1) + activesupport (>= 4.2.0) + i18n (0.9.3) + concurrent-ruby (~> 1.0) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + jquery-rails (4.3.1) rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - json (1.8.3) - loofah (2.0.3) + json (1.8.6) + loofah (2.1.1) + crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.6.4) - mime-types (>= 1.16, < 4) - method_source (0.8.2) - mime-types (3.1) - mime-types-data (~> 3.2015) - mime-types-data (3.2016.0521) - mini_portile2 (2.1.0) - minitest (5.9.1) - multi_json (1.12.1) - nio4r (1.2.1) - nokogiri (1.6.8.1) - mini_portile2 (~> 2.1.0) - rack (2.0.1) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.11.1) + multi_json (1.13.1) + nio4r (2.2.0) + nokogiri (1.8.1) + mini_portile2 (~> 2.3.0) + rack (2.0.3) rack-test (0.6.3) rack (>= 1.0) - rails (5.0.0.1) - actioncable (= 5.0.0.1) - actionmailer (= 5.0.0.1) - actionpack (= 5.0.0.1) - actionview (= 5.0.0.1) - activejob (= 5.0.0.1) - activemodel (= 5.0.0.1) - activerecord (= 5.0.0.1) - activesupport (= 5.0.0.1) - bundler (>= 1.3.0, < 2.0) - railties (= 5.0.0.1) + rails (5.0.6) + actioncable (= 5.0.6) + actionmailer (= 5.0.6) + actionpack (= 5.0.6) + actionview (= 5.0.6) + activejob (= 5.0.6) + activemodel (= 5.0.6) + activerecord (= 5.0.6) + activesupport (= 5.0.6) + bundler (>= 1.3.0) + railties (= 5.0.6) sprockets-rails (>= 2.0.0) - rails-dom-testing (2.0.1) - activesupport (>= 4.2.0, < 6.0) - nokogiri (~> 1.6.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) rails-html-sanitizer (1.0.3) loofah (~> 2.0) - railties (5.0.0.1) - actionpack (= 5.0.0.1) - activesupport (= 5.0.0.1) + railties (5.0.6) + actionpack (= 5.0.6) + activesupport (= 5.0.6) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - rake (11.3.0) - rdoc (4.2.2) - json (~> 1.4) - sass (3.4.22) - sass-rails (5.0.6) + rake (12.3.0) + rb-fsevent (0.10.2) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + rdoc (4.3.0) + sass (3.5.5) + 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) @@ -118,41 +128,43 @@ GEM sdoc (0.4.2) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) - spring (2.0.0) + spring (2.0.2) activesupport (>= 4.2) - sprockets (3.7.0) + sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.0) + sprockets-rails (3.2.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.3.12) - thor (0.19.1) - thread_safe (0.3.5) - tilt (2.0.5) - tzinfo (1.2.2) + sqlite3 (1.3.13) + thor (0.20.0) + thread_safe (0.3.6) + tilt (2.0.8) + tzinfo (1.2.4) thread_safe (~> 0.1) - uglifier (3.0.3) + uglifier (4.1.4) execjs (>= 0.3.0, < 3) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) railties (>= 4.0) sprockets-rails (>= 2.0, < 4.0) - websocket-driver (0.6.4) + websocket-driver (0.6.5) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.2) + websocket-extensions (0.1.3) PLATFORMS ruby DEPENDENCIES + awesome_print + bcrypt (~> 3.1.7) byebug coffee-rails (~> 4.1.0) jbuilder (~> 2.0) jquery-rails - rails (~> 5.0) + rails (~> 5.0.1) sass-rails (~> 5.0) sdoc (~> 0.4.0) spring @@ -161,4 +173,4 @@ DEPENDENCIES web-console (~> 2.0) BUNDLED WITH - 1.11.2 + 1.16.0 diff --git a/app/assets/javascripts/bookings.coffee b/app/assets/javascripts/bookings.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/bookings.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/restaurants.coffee b/app/assets/javascripts/restaurants.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/restaurants.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/sessions.coffee b/app/assets/javascripts/sessions.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/sessions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/javascripts/users.coffee b/app/assets/javascripts/users.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/users.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/bookings.scss b/app/assets/stylesheets/bookings.scss new file mode 100644 index 00000000..c9c87490 --- /dev/null +++ b/app/assets/stylesheets/bookings.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the bookings controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/restaurants.scss b/app/assets/stylesheets/restaurants.scss new file mode 100644 index 00000000..90372d50 --- /dev/null +++ b/app/assets/stylesheets/restaurants.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the restaurants controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/sessions.scss b/app/assets/stylesheets/sessions.scss new file mode 100644 index 00000000..ccb1ed25 --- /dev/null +++ b/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/users.scss b/app/assets/stylesheets/users.scss new file mode 100644 index 00000000..31a2eacb --- /dev/null +++ b/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e1..9a49a313 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -2,4 +2,12 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + private + + def current_user + @current_user ||= User.find(session[:user_id]) if session[:user_id] + end + + helper_method :current_user end diff --git a/app/controllers/bookings_controller.rb b/app/controllers/bookings_controller.rb new file mode 100644 index 00000000..df7b11b3 --- /dev/null +++ b/app/controllers/bookings_controller.rb @@ -0,0 +1,16 @@ +class BookingsController < ApplicationController + + def create + @restaurant = Restaurant.find(params[:restaurant_id]) + @booking = @restaurant.bookings.new + @booking.time = params[:booking][:time] + params[:hour].hour + @booking.n_people = params[:booking][:time] + @booking.user = current_user + + if @booking.save + flash[:notice] = "Reservation made at #{@restaurant.name} on #{@booking.time.month}/#{@booking.time.day} at #{@booking.time.hour}" + redirect_to root_path + end + end + +end diff --git a/app/controllers/restaurants_controller.rb b/app/controllers/restaurants_controller.rb new file mode 100644 index 00000000..f26b73ab --- /dev/null +++ b/app/controllers/restaurants_controller.rb @@ -0,0 +1,42 @@ +class RestaurantsController < ApplicationController + + def index + @restaurants = Restaurant.all + end + + def show + @restaurant = Restaurant.find(params[:id]) + @bookings = @restaurant.bookings + @booking = Booking.new + end + + def new + @restaurant = Restaurant.new + end + + def create + @restaurant = Restaurant.new + @restaurant.name = params[:restaurant][:name] + @restaurant.description = params[:restaurant][:description] + @restaurant.open_time = params[:restaurant][:open_time] + @restaurant.close_time = params[:restaurant][:close_time] + @restaurant.price = params[:restaurant][:price] + + if @restaurant.save + redirect_to restaurants_url + end + end + + def edit + + end + + def update + + end + + def destroy + + end + +end diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb new file mode 100644 index 00000000..4e2a0e29 --- /dev/null +++ b/app/controllers/sessions_controller.rb @@ -0,0 +1,22 @@ +class SessionsController < ApplicationController + + def create + user = User.find_by(email: params[:email]) + if user && user.authenticate(params[:password]) + session[:user_id] = user.id + flash[:notice] = ["Successfully logged in!"] + redirect_to restaurants_url + else + flash[:alert] = ["Username/password are incorrect, please try again!"] + render :new + end + end + + def destroy + session[:user_id] = nil + flash[:notice] = ["Successfully logged out!"] + redirect_to restaurants_url + end + + +end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 00000000..0a441132 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,26 @@ +class UsersController < ApplicationController + + def new + @user = User.new + end + + def create + @user = User.new + @user.email = params[:user][:email] + @user.password = params[:user][:password] + @user.password_confirmation = params[:user][:password_confirmation] + @user.kind = params[:user_kind] + + if @user.save + session[:user_id] = @user.id + if @user.kind == "owner" + redirect_to restaurant_form_url + else + redirect_to root_url + else + flash.now[:alert] = @user.errors.full_messages + render :new + end + end + +end diff --git a/app/helpers/bookings_helper.rb b/app/helpers/bookings_helper.rb new file mode 100644 index 00000000..c10c7c45 --- /dev/null +++ b/app/helpers/bookings_helper.rb @@ -0,0 +1,2 @@ +module BookingsHelper +end diff --git a/app/helpers/restaurants_helper.rb b/app/helpers/restaurants_helper.rb new file mode 100644 index 00000000..3937c4c7 --- /dev/null +++ b/app/helpers/restaurants_helper.rb @@ -0,0 +1,2 @@ +module RestaurantsHelper +end diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb new file mode 100644 index 00000000..309f8b2e --- /dev/null +++ b/app/helpers/sessions_helper.rb @@ -0,0 +1,2 @@ +module SessionsHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 00000000..2310a240 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/models/booking.rb b/app/models/booking.rb new file mode 100644 index 00000000..812f0095 --- /dev/null +++ b/app/models/booking.rb @@ -0,0 +1,4 @@ +class Booking < ApplicationRecord + belongs_to :restaurant + belongs_to :user +end diff --git a/app/models/restaurant.rb b/app/models/restaurant.rb new file mode 100644 index 00000000..f48f907d --- /dev/null +++ b/app/models/restaurant.rb @@ -0,0 +1,4 @@ +class Restaurant < ApplicationRecord + belongs_to :user + has_many :bookings +end diff --git a/app/models/user.rb b/app/models/user.rb new file mode 100644 index 00000000..3494a272 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,9 @@ +class User < ApplicationRecord + has_secure_password + + validates :email, presence: true + validates :email, uniqueness: { case_sensitive: false } + + has_one :restaurant + has_many :bookings +end diff --git a/app/views/bookings/_booking_form.html.erb b/app/views/bookings/_booking_form.html.erb new file mode 100644 index 00000000..9773d8b6 --- /dev/null +++ b/app/views/bookings/_booking_form.html.erb @@ -0,0 +1,17 @@ +<%= form_for [restaurant, booking] do |f| %> +
+ <%= f.label :time, "Date" %> + <%= f.date_field :time %> +
++ <%= label_tag (:at) %> + <%= time_field_tag :hour %> +
++ <%= f.label :n_people, "How many people?" %> + <%= f.number_field :n_people %> +
++ <%= f.submit %> +
+<% end %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index c32feae9..e60fd8e7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -7,8 +7,36 @@ <%= csrf_meta_tags %> +<<<<<<< HEAD +An error has occurred!
+