Skip to content

Commit

Permalink
feat: book a flight
Browse files Browse the repository at this point in the history
* decrement available_seats on flight after_create
  booking

TODO:

* don't show:
  * flights w/o available_seats
  * flights that have departed
* instead of redirect_to root, should go to a
  booking/new page
  * this should be its own separate form that
    lists visible attrs of the flight (num
    passengers, depart date, arr/depts, etc)
  * should be a form with fillable fields for num
    seats requested.
  * the rest is coming from the
    logged in user (their email, eg) and the
    booking.flight association

fix: @booking, strong params

TODO: validate selected flight has more than 0 available seats

* successfully submit/create booking w/o validations
  • Loading branch information
sean-garwood committed Oct 14, 2024
1 parent 18d6d20 commit b86cfcc
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 15 deletions.
4 changes: 0 additions & 4 deletions app/assets/stylesheets/flight_table.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@
tr:nth-child(odd) {
background-color: #f9f9f9;
}

/* better font */
font-family: 'Open Sans',
sans-serif;
}
28 changes: 22 additions & 6 deletions app/controllers/bookings_controller.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,30 @@
class BookingsController < ApplicationController
def new
# designate the @flight instance to the one that was selected on the
# previous page, flights#index
@booking = Booking.new
end

def create
@booking = Booking.new(params[:user_])
@passenger = current_user
@booking.save ? flash[:notice] = "Booking created!" : flash[:alert] = "Booking failed!"
redirect_to root_path
@booking = Booking.new(booking_params)
@booking.passenger = current_user
if @booking.save
flash[:notice] = "Booking created!"
redirect_to root_path
else
flash[:alert] = "Booking failed!"
redirect_to root_path, status: :unprocessable_entity
end
end

private

def booking_params
params.require(:booking).permit(:flight_id, :passenger_id)
end

def logged_in_user
unless current_user
flash[:danger] = "Please log in."
redirect_to root_path, status: :unauthorized
end
end
end
1 change: 1 addition & 0 deletions app/controllers/flights_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FlightsController < ApplicationController
def index
@booking = Booking.new
if flight_params.empty?
@flights = Flight.all
else
Expand Down
16 changes: 16 additions & 0 deletions app/models/booking.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
class Booking < ApplicationRecord
belongs_to :passenger, class_name: "User", foreign_key: "passenger_id"
belongs_to :flight

validates :passenger_id, :flight_id, presence: true
# ensure flight has avaialble seats > 0
validate :decrement_seats

after_create :decrement_seats

accepts_nested_attributes_for :passenger

private

# TODO: determine passengers arg
def decrement_seats(passengers = 1)
flight.available_seats -= passengers
flight.save
end
end
2 changes: 2 additions & 0 deletions app/models/flight.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Flight < ApplicationRecord
belongs_to :arrival_airport, class_name: "Airport", foreign_key: "arrival_airport_id"
has_many :bookings

accepts_nested_attributes_for :bookings

scope :arrival_airports, ->(arrival_airport_code) do
if arrival_airport_code.present?
joins(:arrival_airport).where("airports.code = ?", arrival_airport_code)
Expand Down
4 changes: 2 additions & 2 deletions app/views/flights/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<h2>
Flight results
</h2>
<%= form_with model: @booking, url: new_booking_path do |form| %>
<%= form_with model: @booking, action: :new do |form| %>
<table class="flight-results">
<thead>
<tr>
Expand Down Expand Up @@ -44,6 +44,6 @@
<% end %>
</tbody>
</table>
<%= form.submit "Book Selected Flight" %>
<%= form.submit "Book Selected Flight", data: { turbo: false } %>
<% end %>
<%= link_to "Show all", flights_path %>
3 changes: 2 additions & 1 deletion app/views/flights/_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<div>Depart from:
<%= form.select :departure_airport, Airport.departure_codes, include_blank:
true %></div>
<div>Arrive at:
<div>
<%= form.label :arrival_airport_id %>
<%= form.select :arrival_airport, Airport.arrival_codes,
include_blank: true %></div>
<div>Required seats:
Expand Down
2 changes: 0 additions & 2 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<%= javascript_importmap_tags %>
</head>
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<div class="flash-messages">
<% flash.each do |k, message| %>
<div class="flash <%= k %>"><%= message %></div>
Expand Down

0 comments on commit b86cfcc

Please sign in to comment.