2024-05-03 23:40:05 +07:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aarondl/opt/omit"
|
|
|
|
"github.com/stephenafamo/bob"
|
|
|
|
"github.com/tigorlazuardi/redmage/models"
|
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (api *API) ScheduleStatusUpsert(ctx context.Context, params ScheduleSetParams) (schedule *models.ScheduleStatus, err error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "*API.CreateNewScheduleStatus")
|
|
|
|
defer span.End()
|
|
|
|
return api.scheduleStatusUpsert(ctx, api.db, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *API) scheduleStatusUpsert(ctx context.Context, exec bob.Executor, params ScheduleSetParams) (schedule *models.ScheduleStatus, err error) {
|
|
|
|
ctx, span := tracer.Start(ctx, "*API.createNewScheduleStatus")
|
|
|
|
defer span.End()
|
|
|
|
now := time.Now()
|
2024-05-28 00:30:29 +07:00
|
|
|
api.lockf(func() {
|
|
|
|
schedule, err = models.ScheduleStatuses.Upsert(ctx, exec, true, []string{"subreddit"}, []string{
|
|
|
|
"subreddit",
|
|
|
|
"status",
|
|
|
|
"error_message",
|
|
|
|
"updated_at",
|
|
|
|
}, &models.ScheduleStatusSetter{
|
|
|
|
Subreddit: omit.FromCond(params.Subreddit, params.Subreddit != ""),
|
|
|
|
Status: omit.From(params.Status.Int8()),
|
|
|
|
ErrorMessage: omit.From(params.ErrorMessage),
|
|
|
|
CreatedAt: omit.From(now.Unix()),
|
|
|
|
UpdatedAt: omit.From(now.Unix()),
|
|
|
|
})
|
2024-05-03 23:40:05 +07:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-05-28 00:30:29 +07:00
|
|
|
return schedule, errs.Wrapw(err, "failed to upsert schedule status", "params", params)
|
2024-05-03 23:40:05 +07:00
|
|
|
}
|
2024-05-28 00:30:29 +07:00
|
|
|
return schedule, err
|
2024-05-03 23:40:05 +07:00
|
|
|
}
|