Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Destroy #76

Open
wants to merge 40 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
e0e07da
finalized db structure. added db_relationship.txt file to rails
tylerpalef Aug 21, 2018
f95c2e6
Updates to the db_relationship. Removed owner, now users can create r…
tylerpalef Aug 21, 2018
11e6a29
Created controllers for reservation, restaurant, session, user. Creat…
tylerpalef Aug 21, 2018
af037da
Created routes for the user, restaurant, reservations.
tylerpalef Aug 21, 2018
7d1495b
updates to the application, session and user controller. No changes t…
tylerpalef Aug 21, 2018
47664f4
erb pages done to user, restaurant, and reservations. added user/show…
tylerpalef Aug 21, 2018
19c5f8c
added the new, create, edit, update functions to the controllers of r…
tylerpalef Aug 21, 2018
c920bd8
Made updates to the create, update, and edit methods of the reservati…
tylerpalef Aug 21, 2018
42d9111
Missing end in restaurant controller
davidma415 Aug 22, 2018
46a5220
Changedn aming of controllers and views, causing error
davidma415 Aug 22, 2018
def7f2f
Added bcrypt to gemfile and added a page to create users along with v…
davidma415 Aug 22, 2018
c6687c3
Finished login and sessions features
davidma415 Aug 22, 2018
c3ebe5d
Added functions to edit and show user, as well as links to respective…
davidma415 Aug 22, 2018
3ba3610
Merge pull request #1 from tylerpalef/users
davidma415 Aug 22, 2018
81df5c6
Finished nav field
davidma415 Aug 22, 2018
9421c1b
changes to the routes.rb file to include a create for reservations. C…
tylerpalef Aug 22, 2018
10b3334
updates to the routes.rb file in order to create views for new reserv…
tylerpalef Aug 22, 2018
4970b84
updated restaurants section... finished new, show, edit, _form, and i…
joeysaha Aug 22, 2018
122cd4a
Merge pull request #2 from tylerpalef/base_nav
davidma415 Aug 22, 2018
9437cc8
Merge pull request #3 from tylerpalef/restaurants
joeysaha Aug 22, 2018
ce6f1af
Changed landing page
davidma415 Aug 22, 2018
37a2cf7
Added relationship between restaurants and users, then ensured that o…
davidma415 Aug 22, 2018
c5ac662
changed some formatting in the index, added link to add new restaurant
davidma415 Aug 22, 2018
ae0a1f0
Merge pull request #4 from tylerpalef/edit_if_owner
davidma415 Aug 22, 2018
fd7aa15
Merge branch 'master' into reservation_add_form
davidma415 Aug 22, 2018
1674f40
Merge pull request #5 from tylerpalef/reservation_add_form
davidma415 Aug 22, 2018
48dafef
Fixed routing differences
davidma415 Aug 22, 2018
7ff72cf
Changed reservation controller
davidma415 Aug 22, 2018
164c05f
Fixed reservation form
davidma415 Aug 22, 2018
8dcb737
Added changes to reservatino form and controller, ensure logged in be…
davidma415 Aug 22, 2018
c9efa82
Added validations to reservation, changed controller of reservations,…
davidma415 Aug 22, 2018
e376ffe
updated reservations per user
joeysaha Aug 22, 2018
1d0156d
Merge pull request #6 from tylerpalef/reservations
joeysaha Aug 22, 2018
c2d4747
Finished adding the ability to see all reservations for restaurants t…
davidma415 Aug 22, 2018
6cf9bde
Merge pull request #7 from tylerpalef/restaurant_reservations
davidma415 Aug 22, 2018
e9ce40d
Finished displaying msg when reservation is made as well as changed f…
davidma415 Aug 23, 2018
cc44526
Merge pull request #8 from tylerpalef/reservation-msg
davidma415 Aug 23, 2018
59abbbf
Added validation for the date of the reservation
davidma415 Aug 23, 2018
5632827
Merge pull request #9 from tylerpalef/verify_reservation_date
davidma415 Aug 23, 2018
c2023bb
added destroy/delete methods to restaurant and reservation
joeysaha Aug 23, 2018
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
3 changes: 1 addition & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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'
Expand All @@ -41,4 +41,3 @@ group :development do
gem 'spring'
gem 'listen'
end

4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
arel (9.0.0)
bcrypt (3.1.12)
bindex (0.5.0)
builder (3.2.3)
byebug (10.0.2)
Expand Down Expand Up @@ -160,6 +161,7 @@ PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
byebug
jbuilder (~> 2.0)
jquery-rails
Expand All @@ -173,4 +175,4 @@ DEPENDENCIES
web-console

BUNDLED WITH
1.16.1
1.16.2
2 changes: 2 additions & 0 deletions app/assets/javascripts/reservation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/restaurant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/session.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
2 changes: 2 additions & 0 deletions app/assets/javascripts/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Place all the behaviors and hooks related to the matching controller here.
// All this logic will automatically be available in application.js.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/reservation.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the reservation controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/restaurant.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the restaurant controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/session.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the session controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/user.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the user controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
15 changes: 15 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,19 @@ 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

def ensure_logged_in
unless current_user
flash[:notice] = "Please log in"
redirect_to new_session_url
end
end

private

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end

helper_method :current_user
end
56 changes: 56 additions & 0 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class ReservationsController < ApplicationController
before_action :ensure_logged_in, only: [:new, :create, :edit, :update]
def new
@reservation = Reservation.new
@restaurant = Restaurant.find(params[:restaurant_id])
end

def create
@restaurant = Restaurant.find(params[:restaurant_id])
@reservation = Reservation.new(
date: params[:reservation][:date],
time: params[:reservation][:time],
party_size: params[:reservation][:party_size],
user_id: current_user.id,
restaurant: @restaurant
)

if @reservation.save
flash[:notice] = "Your reservation at #{@restaurant.name} has been made for #{@reservation.date} at #{@reservation.time.strftime("%I:%M%p")} for #{@reservation.party_size}."
redirect_to root_path
else
render :new
end
end

def index
@restaurant = Restaurant.find(params[:restaurant_id])
@reservations = Reservation.where(restaurant_id: @restaurant.id)
end

def edit
@reservation = User.find(params[:id])
end

def update
flash[:notice] = "Your reservation at #{@restaurant.name} has been changed and is now on #{@reservation.date} at #{@reservation.time.strftime("%I:%M%p")} for #{@reservation.party_size}."
@reservation = Reservation.find(params[:id])
@reservation.date = params[:reservation][:date]
@reservation.time = params[:reservation][:time]
@reservation.party_size = params[:reservation][:party_size]

if @reservation.save
redirect_to user_path
else
render :edit
end
end

def destroy
@reservation = Reservation.find(params[:id])
@reservation.destroy
flash[:notice] = "You've successfully deleted a reservation."
redirect_to user_path
end

end
64 changes: 64 additions & 0 deletions app/controllers/restaurants_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
class RestaurantsController < ApplicationController
before_action :ensure_logged_in, only: [:new, :create]
before_action :verify_owner_of_restaurant, only: [:edit, :update]
def new
@restaurant = Restaurant.new
end

def create
@restaurant = Restaurant.new(
name: params[:restaurant][:name],
opening_hour: params[:restaurant][:opening_hour],
closing_hour: params[:restaurant][:closing_hour],
max_capacity: params[:restaurant][:max_capacity],
user_id: current_user.id
)

if @restaurant.save
redirect_to root_path
else
flash.now[:alert] = @restaurant.errors.full_messages
render :new
end
end

def index
@restaurant = Restaurant.all
end

def show
@restaurant = Restaurant.find(params[:id])
end

def edit
@restaurant = Restaurant.find(params[:id])
end

def update
@restaurant = Restaurant.find(params[:id])
@restaurant.name = params[:restaurant][:name]
@restaurant.opening_hour = params[:restaurant][:opening_hour]
@restaurant.closing_hour = params[:restaurant][:closing_hour]
@restaurant.max_capacity = params[:restaurant][:max_capacity]

if @restaurant.save
redirect_to restaurant_path
else
render :edit
end
end

def verify_owner_of_restaurant
unless current_user.id == @restaurant.user_id
flash[:notice] = "You do not own this restaurant. Please login as the owner."
redirect_to new_session_url
end
end

def destroy
@restaurant = Restaurant.find(params[:id])
@restaurant.destroy
flash[:notice] = "You have successfully deleted your restaurant."
redirect_to restaurants_path
end
end
23 changes: 23 additions & 0 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class SessionsController < ApplicationController

def new
end

def create
user = User.find_by(username: params[:session][:username])

if user && user.authenticate(params[:session][:password])
session[:user_id] = user.id
redirect_to root_path
else
flash.now[:notice] = "Login failed, username and/or password are incorrect"
render :new
end
end

def destroy
session[:user_id] = nil
redirect_to root_path
end

end
51 changes: 51 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class UsersController < ApplicationController
before_action :user_session, only: [:edit, :show, :update]
def new
@user = User.new
end

def create
@user = User.new(
username: params[:user][:username],
first_name: params[:user][:first_name],
last_name: params[:user][:last_name],
email: params[:user][:email],
password: params[:user][:password],
password_confirmation: params[:user][:password_confirmation]
)

if @user.save
session[:user_id] = @user.id
redirect_to root_path
else
flash.now[:alert] = @user.errors.full_messages
render :new
end
end
#
def edit
end

def show
@resos = @user.reservations
end

def update
@user.first_name = params[:user][:first_name]
@user.last_name = params[:user][:last_name]
@user.username = params[:user][:username]
@user.email = params[:user][:email]
@user.password = params[:user][:password]
@user.password_confirmation = params[:user][:password_confirmation]

if @user.save
redirect_to user_path
else
render :edit
end
end

def user_session
@user = User.find(session[:user_id])
end
end
2 changes: 2 additions & 0 deletions app/helpers/reservation_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module ReservationHelper
end
2 changes: 2 additions & 0 deletions app/helpers/restaurant_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RestaurantHelper
end
2 changes: 2 additions & 0 deletions app/helpers/session_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module SessionHelper
end
2 changes: 2 additions & 0 deletions app/helpers/user_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UserHelper
end
14 changes: 14 additions & 0 deletions app/models/reservation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Reservation < ApplicationRecord
validates :date, :time, :party_size, presence: true
validates_numericality_of :party_size, :greater_than => 0
validate :date_cannot_be_in_the_past

def date_cannot_be_in_the_past
if date.present? && date.past?
errors.add(:date, "can't be in the past")
end
end

belongs_to :user
belongs_to :restaurant
end
9 changes: 9 additions & 0 deletions app/models/restaurant.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Restaurant < ApplicationRecord
validates :name, :opening_hour, :closing_hour, :max_capacity, presence: true
validates :max_capacity, numericality: { only_integer: true }
validates_numericality_of :max_capacity, :greater_than => 0

has_many :reservations
belongs_to :user

end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class User < ApplicationRecord
validates :first_name, :last_name, :username, :email, :password, :password_confirmation, presence: true
validates :username, uniqueness: true

has_secure_password
has_many :reservations
has_many :restaurants
end
20 changes: 19 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,26 @@
<%= javascript_include_tag 'application' %>
<%= csrf_meta_tags %>
</head>
<body>

<header>
<nav class="navbar">
<%= link_to 'Restaurants', restaurants_path %> |
<% if current_user %>
<%= link_to 'Profile', user_path %> |
<%= link_to 'Logout', session_path("current"), :method => :delete %>
<% else %>
<%= link_to 'Login', new_session_path %> |
<%= link_to 'Register', new_user_path %>
<% end %>

</nav>
</header>
<body>
<% if flash[:notice] %>
<div class="notice">
<%= flash[:notice] %>
</div>
<% end %>
<%= yield %>

</body>
Expand Down
32 changes: 32 additions & 0 deletions app/views/reservations/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% if reservation.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(reservation.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% reservation.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_for([restaurant, reservation]) do |f| %>

<div>
<%= f.label :date %>
<%= f.date_field :date %>
</div>

<div>
<%= f.label :time %>
<%= f.time_field :time %>
</div>

<div>
<%= f.label :party_size %>
<%= f.number_field :party_size %>
</div>

<div>
<%= f.submit "Save reservation" %>
</div>
<% end %>
3 changes: 3 additions & 0 deletions app/views/reservations/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>Edit Reservation</h1>
<%= link_to 'Back to reservation', reservation_path %>
<%= render 'form', reservation: @reservation %>
Loading