2019-01-17 09:53:28 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Event
|
2015-05-05 03:10:25 +02:00
|
|
|
class API::EventsController < API::ApiController
|
2019-01-17 09:53:28 +01:00
|
|
|
before_action :set_event, only: %i[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]
|
|
|
|
|
2020-09-29 08:40:07 +02:00
|
|
|
if current_user&.admin? || current_user&.manager?
|
2019-01-17 09:53:28 +01:00
|
|
|
@events = case params[:scope]
|
|
|
|
when 'future'
|
2019-12-02 11:57:25 +01:00
|
|
|
@events.where('availabilities.start_at >= ?', DateTime.current).order('availabilities.start_at DESC')
|
2019-01-17 09:53:28 +01:00
|
|
|
when 'future_asc'
|
2019-12-02 11:57:25 +01:00
|
|
|
@events.where('availabilities.start_at >= ?', DateTime.current).order('availabilities.start_at ASC')
|
2019-01-17 09:53:28 +01:00
|
|
|
when 'passed'
|
2019-12-02 11:57:25 +01:00
|
|
|
@events.where('availabilities.start_at < ?', DateTime.current).order('availabilities.start_at DESC')
|
2019-01-17 09:53:28 +01:00
|
|
|
else
|
|
|
|
@events.order('availabilities.start_at DESC')
|
|
|
|
end
|
2017-09-25 15:48:21 +02:00
|
|
|
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')
|
|
|
|
.order('availabilities.start_at ASC').references(:availabilities)
|
|
|
|
.limit(limit)
|
2020-09-29 09:39:32 +02:00
|
|
|
|
|
|
|
@events = case Setting.get('upcoming_events_shown')
|
|
|
|
when 'until_start'
|
|
|
|
@events.where('availabilities.start_at >= ?', DateTime.current)
|
|
|
|
when '2h_before_end'
|
|
|
|
@events.where('availabilities.end_at >= ?', DateTime.current + 2.hours)
|
|
|
|
else
|
|
|
|
@events.where('availabilities.end_at >= ?', DateTime.current)
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
2019-01-17 09:53:28 +01:00
|
|
|
def show; end
|
2015-05-05 03:10:25 +02:00
|
|
|
|
|
|
|
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
|
2020-01-29 17:20:19 +01:00
|
|
|
res = EventService.update(@event, event_params.permit!, params[:edit_mode])
|
|
|
|
render json: { action: 'update', total: res.length, updated: res.select { |r| r[:status] }.length, details: res }, status: :ok, location: @event
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize Event
|
2020-01-07 17:18:49 +01:00
|
|
|
res = EventService.delete(params[:id], params[:mode])
|
|
|
|
if res.all? { |r| r[:status] }
|
|
|
|
render json: { deleted: res.length, details: res }, status: :ok
|
2015-05-05 03:10:25 +02:00
|
|
|
else
|
2020-01-07 17:18:49 +01:00
|
|
|
render json: { total: res.length, deleted: res.select { |r| r[:status] }.length, details: res }, status: :unprocessable_entity
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-01-17 09:53:28 +01:00
|
|
|
# Use callbacks to share common setup or constraints between actions.
|
|
|
|
def set_event
|
|
|
|
@event = Event.find(params[:id])
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
|
2019-01-17 09:53:28 +01:00
|
|
|
# Never trust parameters from the scary internet, only allow the white list through.
|
|
|
|
def event_params
|
|
|
|
# handle general properties
|
|
|
|
event_preparams = params.required(:event).permit(:title, :description, :start_date, :start_time, :end_date, :end_time,
|
|
|
|
:amount, :nb_total_places, :availability_id, :all_day, :recurrence,
|
|
|
|
:recurrence_end_at, :category_id, :event_theme_ids, :age_range_id,
|
|
|
|
event_theme_ids: [],
|
|
|
|
event_image_attributes: [:attachment],
|
2019-03-28 11:30:25 +01:00
|
|
|
event_files_attributes: %i[id attachment _destroy],
|
2019-01-21 11:26:42 +01:00
|
|
|
event_price_categories_attributes: %i[id price_category_id amount _destroy])
|
2019-01-17 09:53:28 +01:00
|
|
|
EventService.process_params(event_preparams)
|
|
|
|
end
|
2015-05-05 03:10:25 +02:00
|
|
|
end
|