2015-05-05 03:10:25 +02:00
|
|
|
class API::EventsController < API::ApiController
|
2016-08-11 11:07:49 +02:00
|
|
|
before_action :set_event, only: [:show, :update, :destroy]
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
def index
|
|
|
|
@events = policy_scope(Event)
|
2016-06-27 10:55:51 +02:00
|
|
|
@page = params[:page]
|
2017-09-25 15:48:21 +02:00
|
|
|
@scope = params[:scope]
|
2016-06-29 16:09:27 +02:00
|
|
|
|
|
|
|
# filters
|
2016-08-11 11:07:49 +02:00
|
|
|
@events = @events.joins(:category).where('categories.id = :category', category: params[:category_id]) if params[:category_id]
|
2016-06-29 16:09:27 +02:00
|
|
|
@events = @events.joins(:event_themes).where('event_themes.id = :theme', theme: params[:theme_id]) if params[:theme_id]
|
|
|
|
@events = @events.where('age_range_id = :age_range', age_range: params[:age_range_id]) if params[:age_range_id]
|
|
|
|
|
2017-09-25 15:48:21 +02:00
|
|
|
if current_user and current_user.is_admin?
|
|
|
|
case params[:scope]
|
|
|
|
when 'future'
|
|
|
|
@events = @events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at DESC')
|
|
|
|
when 'future_asc'
|
|
|
|
@events = @events.where('availabilities.start_at >= ?', Time.now).order('availabilities.start_at ASC')
|
|
|
|
when 'passed'
|
|
|
|
@events = @events.where('availabilities.start_at < ?', Time.now).order('availabilities.start_at DESC')
|
|
|
|
else
|
|
|
|
@events = @events.order('availabilities.start_at DESC')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-06-29 16:09:27 +02:00
|
|
|
# paginate
|
2016-06-27 10:55:51 +02:00
|
|
|
@events = @events.page(@page).per(12)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# GET /events/upcoming/:limit
|
|
|
|
def upcoming
|
|
|
|
limit = params[:limit]
|
2016-07-25 16:16:25 +02:00
|
|
|
@events = Event.includes(:event_image, :event_files, :availability, :category)
|
2017-09-24 18:17:40 +02:00
|
|
|
.where('events.nb_total_places != -1 OR events.nb_total_places IS NULL')
|
2015-05-05 03:10:25 +02:00
|
|
|
.where('availabilities.start_at >= ?', Time.now)
|
2017-09-24 18:17:40 +02:00
|
|
|
.order('availabilities.start_at ASC').references(:availabilities)
|
|
|
|
.limit(limit)
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize Event
|
|
|
|
@event = Event.new(event_params.permit!)
|
|
|
|
if @event.save
|
|
|
|
render :show, status: :created, location: @event
|
|
|
|
else
|
|
|
|
render json: @event.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
authorize Event
|
2017-03-02 18:34:55 +01:00
|
|
|
begin
|
|
|
|
if @event.update(event_params.permit!)
|
|
|
|
render :show, status: :ok, location: @event
|
|
|
|
else
|
|
|
|
render json: @event.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordNotDestroyed => e
|
|
|
|
if e.record.class.name == 'EventPriceCategory'
|
|
|
|
render json: {error: ["#{e.record.price_category.name}: #{t('events.error_deleting_reserved_price')}"]}, status: :unprocessable_entity
|
|
|
|
else
|
|
|
|
render json: {error: [t('events.other_error')]}, status: :unprocessable_entity
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
2017-03-02 18:34:55 +01:00
|
|
|
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Event
|
2016-03-23 18:39:41 +01:00
|
|
|
if @event.safe_destroy
|
2015-05-05 03:10:25 +02:00
|
|
|
head :no_content
|
|
|
|
else
|
|
|
|
head :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_event
|
|
|
|
@event = Event.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
# Never trust parameters from the scary internet, only allow the white list through.
|
|
|
|
def event_params
|
2016-08-25 14:13:30 +02:00
|
|
|
# handle general properties
|
2015-05-05 03:10:25 +02:00
|
|
|
event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time,
|
2016-08-25 13:16:47 +02:00
|
|
|
:amount, :nb_total_places, :availability_id,
|
2016-07-25 16:16:25 +02:00
|
|
|
:all_day, :recurrence, :recurrence_end_at, :category_id, :event_theme_ids,
|
|
|
|
:age_range_id, event_theme_ids: [],
|
2016-08-25 13:16:47 +02:00
|
|
|
event_image_attributes: [:attachment],
|
|
|
|
event_files_attributes: [:id, :attachment, :_destroy],
|
2017-03-02 18:34:55 +01:00
|
|
|
event_price_categories_attributes: [:id, :price_category_id, :amount, :_destroy]
|
2016-08-25 13:16:47 +02:00
|
|
|
)
|
2016-08-25 14:13:30 +02:00
|
|
|
# handle dates & times (whole-day events or not, maybe during many days)
|
2016-01-19 10:45:56 +01:00
|
|
|
start_date = Time.zone.parse(event_preparams[:start_date])
|
|
|
|
end_date = Time.zone.parse(event_preparams[:end_date])
|
2015-05-05 03:10:25 +02:00
|
|
|
start_time = Time.parse(event_preparams[:start_time]) if event_preparams[:start_time]
|
|
|
|
end_time = Time.parse(event_preparams[:end_time]) if event_preparams[:end_time]
|
|
|
|
if event_preparams[:all_day] == 'true'
|
|
|
|
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, 0, 0, 0, start_date.zone)
|
|
|
|
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, 23, 59, 59, end_date.zone)
|
|
|
|
else
|
|
|
|
start_at = DateTime.new(start_date.year, start_date.month, start_date.day, start_time.hour, start_time.min, start_time.sec, start_date.zone)
|
|
|
|
end_at = DateTime.new(end_date.year, end_date.month, end_date.day, end_time.hour, end_time.min, end_time.sec, end_date.zone)
|
|
|
|
end
|
|
|
|
event_preparams.merge!(availability_attributes: {id: event_preparams[:availability_id], start_at: start_at, end_at: end_at, available_type: 'event'})
|
|
|
|
.except!(:start_date, :end_date, :start_time, :end_time, :all_day)
|
2016-08-25 14:13:30 +02:00
|
|
|
# convert main price to centimes
|
2016-11-29 15:23:36 +01:00
|
|
|
event_preparams.merge!(amount: (event_preparams[:amount].to_f * 100 if event_preparams[:amount].present?))
|
2016-08-25 14:13:30 +02:00
|
|
|
# delete non-complete "other" prices and convert them to centimes
|
2016-08-30 14:48:22 +02:00
|
|
|
unless event_preparams[:event_price_categories_attributes].nil?
|
|
|
|
event_preparams[:event_price_categories_attributes].delete_if { |price_cat| price_cat[:price_category_id].empty? or price_cat[:amount].empty? }
|
|
|
|
event_preparams[:event_price_categories_attributes].each do |price_cat|
|
2016-11-29 15:23:36 +01:00
|
|
|
price_cat[:amount] = price_cat[:amount].to_f * 100
|
2016-08-30 14:48:22 +02:00
|
|
|
end
|
2016-08-25 13:16:47 +02:00
|
|
|
end
|
2016-08-25 14:13:30 +02:00
|
|
|
# return the resulting params object
|
2016-08-25 13:16:47 +02:00
|
|
|
event_preparams
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
end
|