wedding-planner/app/controllers/tables_arrangements_controller.rb
Manuel Bustillo 55e6cfcd36
Some checks failed
Check usage of free licenses / check-licenses (pull_request) Failing after 1m53s
Run unit tests / unit_tests (pull_request) Failing after 3m11s
Add copyright notice / copyright_notice (pull_request) Successful in 3m16s
Fix order of Ruby's magic string comment and Copyright assignment
2024-12-28 18:37:47 +01:00

41 lines
1.1 KiB
Ruby

# Copyright (C) 2024 Manuel Bustillo
# frozen_string_literal: true
class TablesArrangementsController < ApplicationController
def index
render json: TablesArrangement.order(discomfort: :asc).limit(3).as_json(only: %i[id name discomfort])
end
def show
Guest.joins(:seats, :group)
.where(seats: { tables_arrangement_id: params[:id] })
.select('guests.*', 'groups.color', 'seats.table_number')
.group_by(&:table_number)
.map { |number, guests| format(number:, guests:) }
.then { |result| render json: { id: params[:id], tables: result } }
end
private
def format(number:, guests:)
{
number: number,
discomfort: discomfort(guests: guests),
guests: guests.as_json(only: %i[id name color])
}
end
def discomfort(guests:)
table = Tables::Table.new(guests)
table.min_per_table = TableSimulatorJob::MIN_PER_TABLE
table.max_per_table = TableSimulatorJob::MAX_PER_TABLE
calculator = Tables::DiscomfortCalculator.new(table:)
{
discomfort: calculator.calculate,
breakdown: calculator.breakdown
}
end
end