Files
iotlcm/upstream-chan/internal/logic/channelconfig/channel_update.go
T

52 lines
1.6 KiB
Go

package channelconfig
import (
"context"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/dao"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/model/do"
"git.5iot.com/iotlcm-public/iotlcm-upstream-chan/internal/service"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/errors/gerror"
)
type sChannelUpdate struct{}
func NewChannelUpdate() *sChannelUpdate {
return &sChannelUpdate{}
}
func init() {
service.RegisterChannelUpdate(NewChannelUpdate())
}
func (s *sChannelUpdate) UpdateAccessToken(ctx context.Context, input *model.UpdateAccessTokenInput) error {
if input == nil {
return gerror.New("access token update input is required")
}
data := do.TSimChannels{
AccessToken: input.AccessToken,
AccessTokenCreated: input.AccessTokenCreated,
RefreshToken: input.RefreshToken,
}
if input.AccessTokenExpire != nil {
data.AccessTokenExpire = input.AccessTokenExpire
}
return dao.TSimChannels.Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
_, err := tx.Model(dao.TSimChannels.Table()).Where(dao.TSimChannels.Columns().ChanKey, input.ChanKey).Data(data).Update()
if err != nil || input.AccessTokenExpire != nil {
return err
}
// The generated DO field is a *gtime.Time, so it cannot carry
// gdb.Raw. Clear the nullable column explicitly in this transaction
// instead of letting a nil field be treated as an omitted update.
_, err = tx.Exec(
"UPDATE `t_sim_channels` SET `access_token_expire` = NULL WHERE `chan_key` = ?",
input.ChanKey,
)
return err
})
}