API with more options, includes #712

This commit is contained in:
Stella Rouzi 2015-10-25 00:36:23 +03:00
parent 28e55f3f50
commit ecef151f91
7 changed files with 86 additions and 38 deletions

View file

@ -1,15 +1,15 @@
module Api
module V1
class ConferencesController < Api::BaseController
load_resource find_by: :short_title
respond_to :json
def index
if params[:conference_id].blank?
conferences = Conference.all
else
conferences = Conference.find_all_by_guid(params[:conference_id])
end
render json: conferences, serializer: ConferencesArraySerializer
render json: @conferences, serializer: ConferencesArraySerializer
end
def show
render json: [@conference], serializer: ConferencesArraySerializer
end
end
end

View file

@ -1,13 +1,16 @@
module Api
module V1
class EventsController < Api::BaseController
load_resource :conference, find_by: :short_title
respond_to :json
def index
events = Event.includes(:conference, :track, :room, :event_type, event_users: :user)
unless params[:conference_id].blank?
events = events.where(conferences: { guid: params[:conference_id] })
if @conference
events = events.where(conference: @conference)
end
respond_with events.confirmed
end
end

View file

@ -1,15 +1,12 @@
module Api
module V1
class RoomsController < Api::BaseController
load_resource :conference, find_by: :short_title
respond_to :json
def index
if params[:conference_id].blank?
rooms = Room.all
else
conference = Conference.find_by_guid(params[:conference_id])
rooms = conference.rooms
end
@conference ? (rooms = @conference.rooms) : (rooms = Room.all)
respond_with rooms
end
end

View file

@ -1,16 +1,18 @@
module Api
module V1
class SpeakersController < Api::BaseController
load_resource :conference, find_by: :short_title
respond_to :json
def index
if params[:conference_id].blank?
users = User.joins(:event_users)
else
if @conference
users = User.joins(event_users: { event: :conference })
users = users.where(conferences: { guid: params[:conference_id] })
users = users.where(conferences: { short_title: @conference.short_title })
else
users = User.joins(:event_users)
end
users = users.where(event_users: {event_role: :speaker})
users = users.where(event_users: {event_role: :speaker}).uniq
render json: users, each_serializer: SpeakerSerializer
end
end

View file

@ -1,15 +1,12 @@
module Api
module V1
class TracksController < Api::BaseController
load_resource :conference, find_by: :short_title
respond_to :json
def index
if params[:conference_id].blank?
tracks = Track.all
else
tracks = Track.joins(:conference)
tracks = tracks.where(conferences: { guid: params[:conference_id] })
end
@conference ? (tracks = @conference.tracks) : (tracks = Track.all)
respond_with tracks
end
end

View file

@ -1,5 +1,53 @@
class ConferenceSerializer < ActiveModel::Serializer
attributes :guid, :name, :description, :year, :socialtag, :date_range, :url, :revision
attributes :short_title, :title, :description, :start_date, :end_date, :logo,
:difficulty_levels, :event_types, :rooms, :tracks,
:date_range, :revision
def difficulty_levels
object.difficulty_levels.map do |difficulty_level| { id: difficulty_level.id,
title: difficulty_level.title,
description: difficulty_level.description
}
end
end
def event_types
object.event_types.map do |event_type| { id: event_type.id,
title: event_type.title,
length: event_type.length,
description: event_type.description
}
end
end
def rooms
object.rooms.includes(:events).map do |room| { id: room.id,
size: room.size,
events: room.events.map do |event| { guid: event.title,
title: event.title,
subtitle: event.subtitle,
abstract: event.abstract,
description: event.description,
is_highlight: event.is_highlight,
require_registration: event.require_registration,
start_time: event.start_time,
event_type_id: event.event_type.id,
difficulty_level_id: event.difficulty_level_id,
track_id: event.track_id,
speaker_names: event.speaker_names
}
end
}
end
end
def tracks
object.tracks.map do |track| { 'id' => track.id,
'name' => track.name,
'description' => track.description
}
end
end
def name
object.title
@ -20,16 +68,18 @@ class ConferenceSerializer < ActiveModel::Serializer
# FIXME: adjusting the format the DIRTY way, for oSC13.
# If you think this is ugly, don't look at the methods below
def date_range
object.date_range_string.try(:split, ',').try(:first)
if defined? object.date_range_string
object.date_range_string.try(:split, ',').try(:first)
end
end
# FIXME: just giving suseconferenceclient something to play with
def description
'openSUSE Conference 2013 - Power to the Geeko'
end
# FIXME: same than the former
def url
'https://conference.opensuse.org/'
end
# # FIXME: just giving suseconferenceclient something to play with
# def description
# 'openSUSE Conference 2013 - Power to the Geeko'
# end
#
# # FIXME: same than the former
# def url
# 'https://conference.opensuse.org/'
# end
end

View file

@ -111,8 +111,7 @@ Osem::Application.routes.draw do
namespace :api, defaults: {format: 'json'} do
namespace :v1 do
resources :conferences, only: :index do
resources :conferences, only: :index
resources :conferences, only: [ :index, :show ] do
resources :rooms, only: :index
resources :tracks, only: :index
resources :speakers, only: :index