2024-05-04 22:17:50 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2024-05-05 22:34:05 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
2024-05-04 22:17:50 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/log"
|
|
|
|
"github.com/tigorlazuardi/redmage/views"
|
2024-06-03 16:19:34 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/views/schedulehistories"
|
2024-05-04 22:17:50 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
func (routes *Routes) PageScheduleHistory(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx, span := tracer.Start(req.Context(), "*Routes.PageScheduleHistory")
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
c := views.NewContext(routes.Config, req)
|
|
|
|
|
2024-06-03 16:19:34 +07:00
|
|
|
var data schedulehistories.Data
|
2024-05-05 22:34:05 +07:00
|
|
|
|
2024-06-03 16:19:34 +07:00
|
|
|
data.Params.FillFromQuery(req.URL.Query())
|
2024-06-04 21:08:36 +07:00
|
|
|
result, err := routes.API.ScheduleHistoryList(ctx, data.Params)
|
2024-05-05 22:34:05 +07:00
|
|
|
if err != nil {
|
|
|
|
log.New(ctx).Err(err).Error("Failed to list schedule histories")
|
|
|
|
code, message := errs.HTTPMessage(err)
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
data.Error = message
|
2024-06-03 16:19:34 +07:00
|
|
|
if err := schedulehistories.View(c, data).Render(ctx, rw); err != nil {
|
2024-05-05 22:34:05 +07:00
|
|
|
log.New(ctx).Err(err).Error("Failed to render schedule histories view")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-04 21:08:36 +07:00
|
|
|
data.ScheduleHistories = result
|
|
|
|
|
|
|
|
if latest, _ := routes.API.ScheduleHistoryLatest(ctx); latest != nil {
|
|
|
|
if first := data.ScheduleHistories.GetFirst(); first != nil {
|
|
|
|
if first.ID == latest.ID {
|
|
|
|
data.IsCurrent = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-05-04 22:17:50 +07:00
|
|
|
|
2024-06-03 16:19:34 +07:00
|
|
|
if err := schedulehistories.View(c, data).Render(ctx, rw); err != nil {
|
2024-05-04 22:17:50 +07:00
|
|
|
log.New(ctx).Err(err).Error("Failed to render schedule histories view")
|
|
|
|
}
|
|
|
|
}
|