wedding-planner/app/controllers/tables_arrangements_controller.rb
Manuel Bustillo a42f938530
All checks were successful
Check usage of free licenses / build-static-assets (pull_request) Successful in 55s
Add copyright notice / copyright_notice (pull_request) Successful in 1m28s
Run unit tests / unit_tests (pull_request) Successful in 1m57s
Assign a color to every group and expose it via API
2024-11-03 14:41:09 +01:00

22 lines
713 B
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
Seat.joins(guest: :group)
.where(tables_arrangement_id: params[:id])
.pluck(
:table_number,
Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id',
'groups.color'
)
.group_by(&:first)
.transform_values { |table| table.map { |(_, name, id, color)| { id:, name:, color: } } }
.map { |number, guests| { number:, guests: } }
.then { |result| render json: result }
end
end