45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package telegram
|
|
|
|
import (
|
|
"context"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Repository interface {
|
|
Get(ctx context.Context) (*Settings, error)
|
|
Update(ctx context.Context, s *Settings) error
|
|
}
|
|
|
|
type gormRepository struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func NewRepository(db *gorm.DB) Repository {
|
|
return &gormRepository{db: db}
|
|
}
|
|
|
|
func (r *gormRepository) Get(ctx context.Context) (*Settings, error) {
|
|
var s Settings
|
|
if err := r.db.WithContext(ctx).First(&s, "id = 1").Error; err != nil {
|
|
return nil, err
|
|
}
|
|
return &s, nil
|
|
}
|
|
|
|
func (r *gormRepository) Update(ctx context.Context, s *Settings) error {
|
|
s.ID = 1
|
|
updates := map[string]any{
|
|
"enabled": s.Enabled,
|
|
"chat_id": s.ChatID,
|
|
"notify_new_order": s.NotifyNewOrder,
|
|
"notify_status_change": s.NotifyStatusChange,
|
|
}
|
|
// bot_token is only overwritten when explicitly provided (see service.go):
|
|
// a blank value in the update request means "keep the existing secret".
|
|
if s.BotToken != "" {
|
|
updates["bot_token"] = s.BotToken
|
|
}
|
|
return r.db.WithContext(ctx).Model(&Settings{}).Where("id = 1").Updates(updates).Error
|
|
}
|