From d4c1e5aab07314e0aeba04d6dc0e3c226fff2b03 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sat, 2 Nov 2024 10:40:43 +0100 Subject: [PATCH 1/3] Rewrite table arrangements endpoints --- app/controllers/tables_arrangements_controller.rb | 7 +++++-- app/models/tables_arrangement.rb | 1 + 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index 75451ca..c10e912 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -2,10 +2,13 @@ class TablesArrangementsController < ApplicationController def index - render json: TablesArrangement.all.order(discomfort: :asc).limit(10) + render json: TablesArrangement.all.order(discomfort: :asc).limit(10).as_json(only: %i[id discomfort]) end def show - render json: TablesArrangement.find(params[:id]) + Seat.joins(:guest).where(tables_arrangement_id: params[:id]) + .pluck(:table_number, Arel.sql("guests.first_name || ' ' ||guests.last_name ")) + .group_by(&:first).transform_values { |table| table.map(&:last) } + .then { |result| render json: result } end end diff --git a/app/models/tables_arrangement.rb b/app/models/tables_arrangement.rb index c3c0797..0ceb0cd 100644 --- a/app/models/tables_arrangement.rb +++ b/app/models/tables_arrangement.rb @@ -2,4 +2,5 @@ class TablesArrangement < ApplicationRecord has_many :seats + has_many :guests, through: :seats end From 612cb9a789df7face36bc0e738b3920e81604492 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sat, 2 Nov 2024 11:23:00 +0100 Subject: [PATCH 2/3] Refine arrangement detail endpoint --- app/controllers/tables_arrangements_controller.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index c10e912..8260feb 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -7,8 +7,10 @@ class TablesArrangementsController < ApplicationController def show Seat.joins(:guest).where(tables_arrangement_id: params[:id]) - .pluck(:table_number, Arel.sql("guests.first_name || ' ' ||guests.last_name ")) - .group_by(&:first).transform_values { |table| table.map(&:last) } + .pluck(:table_number, Arel.sql("guests.first_name || ' ' || guests.last_name "), 'guests.id') + .group_by(&:first) + .transform_values { |table| table.map { |(_, name, id)| { id:, name: } } } + .map { |number, guests| { number:, guests: } } .then { |result| render json: result } end end From d6182392f61ad779e2e5a5bee8cac44df12ecd5c Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Sat, 2 Nov 2024 12:50:45 +0100 Subject: [PATCH 3/3] Return only the top 3 arrangements --- app/controllers/tables_arrangements_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/tables_arrangements_controller.rb b/app/controllers/tables_arrangements_controller.rb index 8260feb..a1d7420 100644 --- a/app/controllers/tables_arrangements_controller.rb +++ b/app/controllers/tables_arrangements_controller.rb @@ -2,7 +2,7 @@ class TablesArrangementsController < ApplicationController def index - render json: TablesArrangement.all.order(discomfort: :asc).limit(10).as_json(only: %i[id discomfort]) + render json: TablesArrangement.all.order(discomfort: :asc).limit(3).as_json(only: %i[id discomfort]) end def show