스케쥴 추가 기능 구현

This commit is contained in:
2025-04-19 00:07:28 +09:00
parent eb3bc33509
commit 32d0ac69c9
13 changed files with 141 additions and 63 deletions

View File

@@ -1,5 +1,6 @@
class ModalsController < ApplicationController
def open
@schedule = Schedule.new
render partial: "partials/modals_open", locals: { type: params[:type], close_button: params[:close_button] }
end
end

View File

@@ -1,53 +1,11 @@
class ModbusController < ApplicationController
def index
@schedule = Schedule.all
@modbus_running = Modbus::PollingService.running?
end
def destroy
schedule = Schedule.find_by(id: params[:id])
if schedule.destroy
redirect_to schedule_edit_modbus_index_path, notice: "스케줄이 삭제되었습니다."
else
redirect_to schedule_edit_modbus_index_path, alert: "스케줄 삭제에 실패했습니다."
end
end
def start
Modbus::PollingService.start
redirect_to modbus_index_path
redirect_to schedules_path
end
def stop
Modbus::PollingService.stop
redirect_to modbus_index_path
end
def schedule_edit
@schedule = Schedule.all
end
def schedule_edit_update
error_time = []
params[:schedule].each do |id, attributes|
schedule = Schedule.find_by(id: id)
unless schedule.update(
hour: attributes[:hour],
minute: attributes[:minute],
is_active: attributes[:is_active],
temperature: attributes[:temperature]
)
error_time << "#{schedule.hour}시 #{schedule.minute}분"
end
end
if error_time.any?
redirect_to modbus_index_path, alert: "#{error_time.join(', ')}의 온도 업데이트에 실패하였습니다."
else
redirect_to modbus_index_path, notice: "스케줄이 성공적으로 업데이트되었습니다."
end
redirect_to schedules_path
end
end

View File

@@ -0,0 +1,68 @@
class SchedulesController < ApplicationController
def index
@schedule = Schedule.order(:hour, :minute)
@modbus_running = Modbus::PollingService.running?
end
def new
@schedule = Schedule.new
end
def create
@schedule = Schedule.new(schedule_params)
if @schedule.save
redirect_to schedule_edit_schedules_path, notice: "스케쥴이 추가 되었습니다."
else
error_messages = @schedule.errors.full_messages.join(", ")
redirect_to schedule_edit_schedules_path, alert: "스케쥴 추가 실패: #{error_messages}"
end
end
def destroy
@schedule = Schedule.find_by(id: params[:id])
if @schedule.destroy
redirect_to schedule_edit_schedules_path, notice: "스케줄이 삭제되었습니다."
else
error_messages = @schedule.errors.full_messages.join(", ")
redirect_to schedule_edit_schedules_path, alert: "스케쥴 삭제 실패: #{error_messages}"
end
end
def schedule_edit
@schedule = Schedule.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 schedules_path, alert: error_messages.join("<br>")
else
redirect_to schedules_path, notice: "스케줄이 성공적으로 업데이트되었습니다."
end
end
private
def schedule_params
params.require(:schedule).permit(:hour, :minute, :is_active, :temperature)
end
end