From bf278693b3e6497f4ba15e29a8aa536e71ee97e2 Mon Sep 17 00:00:00 2001 From: Petko Bordjukov Date: Thu, 8 Oct 2015 02:34:19 +0300 Subject: [PATCH] Add a simple JSON API --- app/controllers/api/event_types_controller.rb | 8 ++++++++ app/controllers/api/halls_controller.rb | 8 ++++++++ app/controllers/api/slots_controller.rb | 8 ++++++++ app/controllers/api/speakers_controller.rb | 8 ++++++++ app/controllers/api/tracks_controller.rb | 8 ++++++++ app/models/conference.rb | 1 + app/models/event.rb | 1 + app/views/api/event_types/index.jbuilder | 13 +++++++++++++ app/views/api/halls/index.jbuilder | 1 + app/views/api/slots/index.jbuilder | 1 + app/views/api/speakers/index.jbuilder | 4 ++++ app/views/api/tracks/index.jbuilder | 13 +++++++++++++ config/routes.rb | 5 +++++ 13 files changed, 79 insertions(+) create mode 100644 app/controllers/api/event_types_controller.rb create mode 100644 app/controllers/api/halls_controller.rb create mode 100644 app/controllers/api/slots_controller.rb create mode 100644 app/controllers/api/speakers_controller.rb create mode 100644 app/controllers/api/tracks_controller.rb create mode 100644 app/views/api/event_types/index.jbuilder create mode 100644 app/views/api/halls/index.jbuilder create mode 100644 app/views/api/slots/index.jbuilder create mode 100644 app/views/api/speakers/index.jbuilder create mode 100644 app/views/api/tracks/index.jbuilder diff --git a/app/controllers/api/event_types_controller.rb b/app/controllers/api/event_types_controller.rb new file mode 100644 index 0000000..875b988 --- /dev/null +++ b/app/controllers/api/event_types_controller.rb @@ -0,0 +1,8 @@ +class Api::EventTypesController < Api::ApplicationController + include ::CurrentConferenceAssigning + before_filter :require_current_conference! + + def index + @event_types = current_conference.event_types.includes(:translations) + end +end diff --git a/app/controllers/api/halls_controller.rb b/app/controllers/api/halls_controller.rb new file mode 100644 index 0000000..98c397a --- /dev/null +++ b/app/controllers/api/halls_controller.rb @@ -0,0 +1,8 @@ +class Api::HallsController < Api::ApplicationController + include ::CurrentConferenceAssigning + before_filter :require_current_conference! + + def index + @halls = current_conference.halls + end +end diff --git a/app/controllers/api/slots_controller.rb b/app/controllers/api/slots_controller.rb new file mode 100644 index 0000000..4a02389 --- /dev/null +++ b/app/controllers/api/slots_controller.rb @@ -0,0 +1,8 @@ +class Api::SlotsController < Api::ApplicationController + include ::CurrentConferenceAssigning + before_filter :require_current_conference! + + def index + @slots = current_conference.slots + end +end diff --git a/app/controllers/api/speakers_controller.rb b/app/controllers/api/speakers_controller.rb new file mode 100644 index 0000000..871bb64 --- /dev/null +++ b/app/controllers/api/speakers_controller.rb @@ -0,0 +1,8 @@ +class Api::SpeakersController < Api::ApplicationController + include ::CurrentConferenceAssigning + before_filter :require_current_conference! + + def index + @speakers = PersonalProfile.joins(user: [:events]).where(conference: current_conference) + end +end diff --git a/app/controllers/api/tracks_controller.rb b/app/controllers/api/tracks_controller.rb new file mode 100644 index 0000000..e3e5263 --- /dev/null +++ b/app/controllers/api/tracks_controller.rb @@ -0,0 +1,8 @@ +class Api::TracksController < Api::ApplicationController + include ::CurrentConferenceAssigning + before_filter :require_current_conference! + + def index + @tracks = current_conference.tracks.includes(:translations) + end +end diff --git a/app/models/conference.rb b/app/models/conference.rb index c1ec945..742dd04 100644 --- a/app/models/conference.rb +++ b/app/models/conference.rb @@ -15,6 +15,7 @@ class Conference < ActiveRecord::Base has_many :volunteer_teams has_one :call_for_participation, dependent: :destroy has_many :participants, class_name: 'User', through: :events + has_many :slots, through: :events accepts_nested_attributes_for :tracks, :halls, :event_types, :volunteer_teams, reject_if: :all_blank, allow_destroy: true diff --git a/app/models/event.rb b/app/models/event.rb index 26f1957..c3ac8fd 100644 --- a/app/models/event.rb +++ b/app/models/event.rb @@ -3,6 +3,7 @@ class Event < ActiveRecord::Base belongs_to :conference belongs_to :track + has_one :slot has_many :participations, dependent: :destroy has_many :pending_participations, ->() { pending }, class_name: 'Participation' diff --git a/app/views/api/event_types/index.jbuilder b/app/views/api/event_types/index.jbuilder new file mode 100644 index 0000000..3848b38 --- /dev/null +++ b/app/views/api/event_types/index.jbuilder @@ -0,0 +1,13 @@ +json.array! @event_types do |event_type| + json.extract! event_type, :id + json.name do + event_type.translations.each do |translation| + json.set! translation.locale, translation.name + end + end + json.description do + event_type.translations.each do |translation| + json.set! translation.locale, translation.description + end + end +end diff --git a/app/views/api/halls/index.jbuilder b/app/views/api/halls/index.jbuilder new file mode 100644 index 0000000..0707f08 --- /dev/null +++ b/app/views/api/halls/index.jbuilder @@ -0,0 +1 @@ +json.array! @halls, :id, :name diff --git a/app/views/api/slots/index.jbuilder b/app/views/api/slots/index.jbuilder new file mode 100644 index 0000000..4cd9ba5 --- /dev/null +++ b/app/views/api/slots/index.jbuilder @@ -0,0 +1 @@ +json.array! @slots, :hall_id, :event_id, :starts_at, :ends_at diff --git a/app/views/api/speakers/index.jbuilder b/app/views/api/speakers/index.jbuilder new file mode 100644 index 0000000..427c242 --- /dev/null +++ b/app/views/api/speakers/index.jbuilder @@ -0,0 +1,4 @@ +json.array! @speakers do |speaker| + json.extract! speaker, :user_id, :twitter, :github, :biography, :public_email, :organisation, :last_name, :first_name + json.picture speaker.picture.serializable_hash +end diff --git a/app/views/api/tracks/index.jbuilder b/app/views/api/tracks/index.jbuilder new file mode 100644 index 0000000..7fc65f9 --- /dev/null +++ b/app/views/api/tracks/index.jbuilder @@ -0,0 +1,13 @@ +json.array! @tracks do |track| + json.extract! track, :id + json.name do + track.translations.each do |translation| + json.set! translation.locale, translation.name + end + end + json.description do + track.translations.each do |translation| + json.set! translation.locale, translation.description + end + end +end diff --git a/config/routes.rb b/config/routes.rb index 97b66fb..08149b9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,11 @@ Rails.application.routes.draw do namespace :api do resources :conferences, only: [] do resources :events, only: :index + resources :speakers, only: :index + resources :tracks, only: :index + resources :event_types, only: :index + resources :halls, only: :index + resources :slots, only: :index end end