From b86cfcc203d71ec0272cc6dbf0747ee525cbae97 Mon Sep 17 00:00:00 2001 From: sean-garwood Date: Sat, 12 Oct 2024 21:15:02 -0400 Subject: [PATCH] feat: book a flight * 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 --- app/assets/stylesheets/flight_table.css | 4 ---- app/controllers/bookings_controller.rb | 28 +++++++++++++++++++------ app/controllers/flights_controller.rb | 1 + app/models/booking.rb | 16 ++++++++++++++ app/models/flight.rb | 2 ++ app/views/flights/_results.html.erb | 4 ++-- app/views/flights/_search.html.erb | 3 ++- app/views/layouts/application.html.erb | 2 -- 8 files changed, 45 insertions(+), 15 deletions(-) diff --git a/app/assets/stylesheets/flight_table.css b/app/assets/stylesheets/flight_table.css index 5e49d08..f981d5c 100644 --- a/app/assets/stylesheets/flight_table.css +++ b/app/assets/stylesheets/flight_table.css @@ -5,8 +5,4 @@ tr:nth-child(odd) { background-color: #f9f9f9; } - - /* better font */ - font-family: 'Open Sans', - sans-serif; } diff --git a/app/controllers/bookings_controller.rb b/app/controllers/bookings_controller.rb index 524d027..7fa4305 100644 --- a/app/controllers/bookings_controller.rb +++ b/app/controllers/bookings_controller.rb @@ -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 diff --git a/app/controllers/flights_controller.rb b/app/controllers/flights_controller.rb index 2d24a9c..b7ad98f 100644 --- a/app/controllers/flights_controller.rb +++ b/app/controllers/flights_controller.rb @@ -1,5 +1,6 @@ class FlightsController < ApplicationController def index + @booking = Booking.new if flight_params.empty? @flights = Flight.all else diff --git a/app/models/booking.rb b/app/models/booking.rb index 21520da..f147c99 100644 --- a/app/models/booking.rb +++ b/app/models/booking.rb @@ -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 diff --git a/app/models/flight.rb b/app/models/flight.rb index e0024b8..8192b6a 100644 --- a/app/models/flight.rb +++ b/app/models/flight.rb @@ -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) diff --git a/app/views/flights/_results.html.erb b/app/views/flights/_results.html.erb index 4a7181c..9db38cd 100644 --- a/app/views/flights/_results.html.erb +++ b/app/views/flights/_results.html.erb @@ -1,7 +1,7 @@

Flight results

-<%= form_with model: @booking, url: new_booking_path do |form| %> +<%= form_with model: @booking, action: :new do |form| %> @@ -44,6 +44,6 @@ <% end %>
- <%= form.submit "Book Selected Flight" %> + <%= form.submit "Book Selected Flight", data: { turbo: false } %> <% end %> <%= link_to "Show all", flights_path %> diff --git a/app/views/flights/_search.html.erb b/app/views/flights/_search.html.erb index 76d69a7..d9b00ee 100644 --- a/app/views/flights/_search.html.erb +++ b/app/views/flights/_search.html.erb @@ -3,7 +3,8 @@
Depart from: <%= form.select :departure_airport, Airport.departure_codes, include_blank: true %>
-
Arrive at: +
+ <%= form.label :arrival_airport_id %> <%= form.select :arrival_airport, Airport.arrival_codes, include_blank: true %>
Required seats: diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 1a75d79..718e8e2 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -15,8 +15,6 @@ <%= javascript_importmap_tags %> -

<%= notice %>

-

<%= alert %>

<% flash.each do |k, message| %>
<%= message %>