Redmage/server/routes/subreddit_validate_schedule.go

54 lines
1.4 KiB
Go
Raw Permalink Normal View History

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"
)
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
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 {
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 {
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 {
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-27 21:10:37 +07:00
if err := put.ScheduleInput(data).Render(ctx, rw); err != nil {
log.New(ctx).Err(err).Error("failed to render schedule input")
}
}