From deebcbc95a825ab2f55de291071aa5700a45d903 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 3 Oct 2024 23:24:41 +0300 Subject: [PATCH] New dev-friendly schedule endpoint --- app/controllers/api/schedules_controller.rb | 9 +++++++++ app/models/event.rb | 1 + app/models/slot.rb | 1 + app/views/api/schedules/show.json.jbuilder | 22 +++++++++++++++++++++ config/routes.rb | 1 + 5 files changed, 34 insertions(+) create mode 100644 app/controllers/api/schedules_controller.rb create mode 100644 app/views/api/schedules/show.json.jbuilder diff --git a/app/controllers/api/schedules_controller.rb b/app/controllers/api/schedules_controller.rb new file mode 100644 index 0000000..b3fa278 --- /dev/null +++ b/app/controllers/api/schedules_controller.rb @@ -0,0 +1,9 @@ +class Api::SchedulesController < Api::ApplicationController + include ::CurrentConferenceAssigning + include ::PublicApiExposing + before_action :require_current_conference! + + def show + @halls = Conference.last.halls.includes(:translations, slots: {approved_event: [:participants_with_personal_profiles, :proposition]}) + end +end diff --git a/app/models/event.rb b/app/models/event.rb index a170803..873942a 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -20,6 +20,7 @@ class Event < ActiveRecord::Base scope :ranked, -> { where.not(ranked: nil).where.not(votes: nil) } scope :approved, -> { where(propositions: {status: Proposition.statuses[:approved]})} + scope :approved_joined, -> { joins(:proposition).merge(Proposition.approved) } validates :conference, presence: true validates :title, presence: true, length: {maximum: 65} diff --git a/app/models/slot.rb b/app/models/slot.rb index 06bcf33..c06b4d6 100644 --- a/app/models/slot.rb +++ b/app/models/slot.rb @@ -1,4 +1,5 @@ class Slot < ActiveRecord::Base belongs_to :hall belongs_to :event, required: false + belongs_to :approved_event, -> { joins(:proposition).approved_joined }, class_name: 'Event', foreign_key: 'event_id' end diff --git a/app/views/api/schedules/show.json.jbuilder b/app/views/api/schedules/show.json.jbuilder new file mode 100644 index 0000000..e2d13b7 --- /dev/null +++ b/app/views/api/schedules/show.json.jbuilder @@ -0,0 +1,22 @@ +@halls.each do |hall| + json.set! hall.name do + json.days do + hall.slots.to_a.sort_by(&:starts_at).group_by { |slot| slot.starts_at.to_date }.each do |day, slots| + json.set! day do + json.array! slots do |slot| + next unless slot.approved_event + json.starts_at slot.starts_at + json.starts_at_human l(slot.starts_at, format: '%a, %H:%M') + json.title slot.approved_event.title + json.speakers do + json.array! slot.approved_event.participants_with_personal_profiles do |participant| + json.name participant.name + json.email participant.public_email + end + end + end + end + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index df10dba..b5b983d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -32,6 +32,7 @@ Rails.application.routes.draw do resources :event_types, only: :index resources :halls, only: :index resources :slots, only: :index + resource :schedule, only: :show resources :volunteers end end