Skip to content

Commit

Permalink
feat: finalize reservations controller
Browse files Browse the repository at this point in the history
  • Loading branch information
dgpangan committed Feb 19, 2023
1 parent afbae26 commit c67711f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 57 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@ Style/Documentation:
Enabled: false

Metrics/MethodLength:
Max: 20

Metrics/AbcSize:
Max: 20
37 changes: 12 additions & 25 deletions app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,30 @@ class ReservationsController < ApplicationController
LOOKUP_ITEMS = YAML.load_file('lib/lookup.yml').deep_symbolize_keys

def create
data = JSON.parse(request.raw_post).smash
payload = parse_data(data)
guest = create_guest(payload[:guest_data])
reservation = create_reservation(guest, payload[:reservation_data])
data = parse_data(JSON.parse(request.raw_post).smash)
Guest.upsert(data[:guest])
guest = Guest.find_by(email: data[:guest][:email])
data[:reservation][:guest_id] = guest.id
Reservation.upsert(data[:reservation])
reservation = Reservation.find_by(code: data[:reservation][:code])
render json: { guest:, reservation: }
end

private

def parse_data(data)
guest_data = {}
reservation_data = {}
guest = {}
reservation = {}

data.each do |key, value|
record = LOOKUP_ITEMS[key.to_sym]
next if record.blank?

reservation_data[record[:property].to_sym] = value if record[:model] == 'reservation'
guest_data[record[:property].to_sym] = value if record[:model] == 'guest'
reservation[record[:property].to_sym] = value if record[:model] == 'reservation'
guest[record[:property].to_sym] = value if record[:model] == 'guest'
end

{ reservation_data:, guest_data: }
end

def create_guest(guest_data)
Guest.find_or_create_by(email: guest_data[:email]) do |g|
guest_data.each do |key, value|
g[key] = value
end
end
end

def create_reservation(guest, reservation_data)
guest.reservations.find_or_create_by(code: reservation_data[:code]) do |r|
reservation_data.each do |key, value|
r[key] = value
end
end
reservation[:guest_id] = guest[:id]
{ reservation:, guest: }
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230219221143_add_index_to_guests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddIndexToGuests < ActiveRecord::Migration[7.0]
def change
add_index :guests, :email, unique: true
end
end
7 changes: 7 additions & 0 deletions db/migrate/20230219221354_add_index_to_reservations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddIndexToReservations < ActiveRecord::Migration[7.0]
def change
add_index :reservations, :code, unique: true
end
end
64 changes: 32 additions & 32 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c67711f

Please sign in to comment.