package systemconfig import ( "context" "errors" "strconv" ) var ( ErrDependencyUnavailable = errors.New("dependency unavailable") ErrInvalidConfig = errors.New("invalid config") ) type Service struct { repo *Repository } func NewService(repo *Repository) *Service { return &Service{repo: repo} } func (s *Service) List(ctx context.Context) ([]ConfigDTO, error) { if s.repo == nil { return nil, ErrDependencyUnavailable } return s.repo.List(ctx) } func (s *Service) BackupSchedule(ctx context.Context) (*BackupScheduleDTO, error) { if s.repo == nil { return nil, ErrDependencyUnavailable } values := map[string]string{} for _, item := range defaultConfigs { if item.Key == "backup.schedule_enabled" || item.Key == "backup.full_hour" || item.Key == "backup.full_minute" || item.Key == "backup.binlog_interval_minutes" { values[item.Key] = item.Value } } for key := range values { value, err := s.repo.FindValue(ctx, key) if err != nil { return nil, err } values[key] = value } hour, _ := strconv.Atoi(values["backup.full_hour"]) minute, _ := strconv.Atoi(values["backup.full_minute"]) interval, _ := strconv.Atoi(values["backup.binlog_interval_minutes"]) return &BackupScheduleDTO{ Enabled: values["backup.schedule_enabled"] == "true", FullHour: hour, FullMinute: minute, BinlogIntervalMinutes: interval, }, nil }