81 lines
2.5 KiB
Ruby
81 lines
2.5 KiB
Ruby
class SchedulesController < ApplicationController
|
|
def index
|
|
@controllers = Controller.all
|
|
end
|
|
|
|
def view
|
|
@controller = Controller.find(params[:id])
|
|
@schedule = Schedule.where(controller_id: params[:id]).order(:hour, :minute)
|
|
end
|
|
|
|
def new
|
|
@schedule = Schedule.new
|
|
end
|
|
|
|
def create
|
|
@schedule = Schedule.new(schedule_params)
|
|
|
|
if @schedule.save
|
|
redirect_to schedule_edit_schedule_path(@schedule.controller_id), notice: "스케쥴이 추가 되었습니다."
|
|
else
|
|
error_messages = @schedule.errors.full_messages.join(", ")
|
|
redirect_to schedule_edit_schedule_path(@schedule.controller_id), alert: "스케쥴 추가 실패: #{error_messages}"
|
|
end
|
|
end
|
|
|
|
def destroy
|
|
@schedule = Schedule.find_by(id: params[:id])
|
|
|
|
if @schedule.destroy
|
|
redirect_to schedule_edit_schedule_path(@schedule.controller_id), notice: "스케줄이 삭제되었습니다."
|
|
else
|
|
error_messages = @schedule.errors.full_messages.join(", ")
|
|
redirect_to schedule_edit_schedule_path(@schedule.controller_id), alert: "스케쥴 삭제 실패: #{error_messages}"
|
|
end
|
|
end
|
|
|
|
def reset
|
|
Schedule.where(controller_id: params[:id]).destroy_all
|
|
ActiveRecord::Base.connection.execute("DELETE FROM sqlite_sequence WHERE name='schedules'")
|
|
|
|
redirect_to schedule_edit_schedule_path(params[:id]), notice: "스케줄이 초기화되었습니다."
|
|
end
|
|
|
|
def schedule_edit
|
|
@controller = Controller.find(params[:id])
|
|
@schedule = Schedule.where(controller_id: params[:id]).order(:hour, :minute)
|
|
end
|
|
|
|
def schedule_edit_update
|
|
error_messages = []
|
|
|
|
params[:schedule].each do |id, attributes|
|
|
schedule = Schedule.find_by(id: id)
|
|
next unless schedule
|
|
|
|
unless schedule.update(
|
|
hour: attributes[:hour],
|
|
minute: attributes[:minute],
|
|
is_active: attributes[:is_active],
|
|
temperature: attributes[:temperature]
|
|
)
|
|
|
|
error_detail = schedule.errors.full_messages.join(", ")
|
|
time_label = "#{attributes[:hour]}시 #{attributes[:minute]}분"
|
|
error_messages << "#{time_label} - #{error_detail}"
|
|
end
|
|
end
|
|
|
|
if error_messages.any?
|
|
redirect_to schedule_edit_schedule_path(params[:controller_id]), alert: error_messages.join("<br>")
|
|
else
|
|
redirect_to schedule_edit_schedule_path(params[:controller_id]), notice: "스케줄이 성공적으로 업데이트되었습니다."
|
|
end
|
|
end
|
|
|
|
private
|
|
def schedule_params
|
|
params.require(:schedule).permit(:controller_id, :hour, :minute, :is_active, :temperature)
|
|
end
|
|
end
|