1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-11-29 10:24:20 +01:00
fab-manager/app/controllers/api/i_calendar_controller.rb

53 lines
1.3 KiB
Ruby
Raw Normal View History

2019-11-27 17:05:19 +01:00
# frozen_string_literal: true
# API Controller for resources of type iCalendar
2023-02-24 17:26:55 +01:00
class API::ICalendarController < API::APIController
2019-11-27 17:05:19 +01:00
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
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
ICalendarImportWorker.perform_async([params[:id]])
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