2024-05-03 11:59:44 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
2024-05-27 21:10:37 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/views/subreddits/put"
|
2024-05-03 11:59:44 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) SubredditValidateScheduleHTMX(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx, span := tracer.Start(r.Context(), "*Routes.SubredditValidateScheduleHTMX")
|
|
|
|
defer span.End()
|
|
|
|
|
2024-05-27 21:10:37 +07:00
|
|
|
var data put.ScheduleInputData
|
2024-05-03 11:59:44 +07:00
|
|
|
|
|
|
|
enabled, _ := strconv.Atoi(r.FormValue("enable_schedule"))
|
|
|
|
data.Disabled = enabled == 0
|
|
|
|
data.Value = r.FormValue("schedule")
|
|
|
|
|
|
|
|
if data.Value == "" {
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.ScheduleInput(data).Render(ctx, rw); err != nil {
|
2024-05-03 11:59:44 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render schedule input")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if data.Disabled {
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.ScheduleInput(data).Render(ctx, rw); err != nil {
|
2024-05-03 11:59:44 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render schedule input")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
scheduler, err := cronParser.Parse(data.Value)
|
|
|
|
if err != nil {
|
|
|
|
data.Error = fmt.Sprintf("Invalid schedule format: %s", err.Error())
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.ScheduleInput(data).Render(ctx, rw); err != nil {
|
2024-05-03 11:59:44 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render schedule input")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
next := scheduler.Next(time.Now())
|
|
|
|
|
2024-05-27 21:10:37 +07:00
|
|
|
data.Valid = fmt.Sprintf("Syntax is valid. Next run at: %s", next.Format("Monday, _2 January 2006 15:04:05 MST"))
|
2024-05-03 11:59:44 +07:00
|
|
|
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.ScheduleInput(data).Render(ctx, rw); err != nil {
|
2024-05-03 11:59:44 +07:00
|
|
|
log.New(ctx).Err(err).Error("failed to render schedule input")
|
|
|
|
}
|
|
|
|
}
|