2019-11-27 17:05:19 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type iCalendar
|
|
|
|
class API::ICalendarController < API::ApiController
|
|
|
|
before_action :authenticate_user!, except: %i[index events]
|
|
|
|
before_action :set_i_cal, only: [:destroy]
|
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
|
|
|
@i_cals = ICalendar.all
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
authorize ICalendar
|
|
|
|
@i_cal = ICalendar.new(i_calendar_params)
|
|
|
|
if @i_cal.save
|
|
|
|
render :show, status: :created, location: @i_cal
|
|
|
|
else
|
|
|
|
render json: @i_cal.errors, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize ICalendar
|
|
|
|
@i_cal.destroy
|
|
|
|
head :no_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def events
|
2019-12-02 16:49:20 +01:00
|
|
|
start_date = ActiveSupport::TimeZone[params[:timezone]]&.parse(params[:start])
|
|
|
|
end_date = ActiveSupport::TimeZone[params[:timezone]]&.parse(params[:end])&.end_of_day
|
|
|
|
|
|
|
|
@events = ICalendarEvent.where(i_calendar_id: params[:id])
|
|
|
|
.where('dtstart >= ? AND dtend <= ?', start_date, end_date)
|
|
|
|
.joins(:i_calendar)
|
2019-11-27 17:05:19 +01:00
|
|
|
end
|
|
|
|
|
2019-12-02 12:19:30 +01:00
|
|
|
def sync
|
2019-12-03 12:16:07 +01:00
|
|
|
ICalendarImportWorker.perform_async([params[:id]])
|
2019-12-02 15:53:24 +01:00
|
|
|
render json: { processing: [params[:id]] }, status: :created
|
2019-12-02 12:19:30 +01:00
|
|
|
end
|
|
|
|
|
2019-11-27 17:05:19 +01:00
|
|
|
private
|
|
|
|
|
|
|
|
def set_i_cal
|
|
|
|
@i_cal = ICalendar.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def i_calendar_params
|
2019-11-27 17:39:19 +01:00
|
|
|
params.require(:i_calendar).permit(:name, :url, :color, :text_color, :text_hidden)
|
2019-11-27 17:05:19 +01:00
|
|
|
end
|
|
|
|
end
|