39 lines
1.1 KiB
Ruby
39 lines
1.1 KiB
Ruby
# Copyright (C) 2024 Manuel Bustillo
|
|
|
|
class TablesArrangementsController < ApplicationController
|
|
def index
|
|
render json: TablesArrangement.all.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
|